﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace GKeyBank.UI
{
    /// <summary>
    /// Combobox with all cultures
    /// </summary>
    public class ComboBoxCulture : kComboBox
    {
        public List<KeyValuePair<string, string>> SourceList { get; set; }

        private int Compare1(KeyValuePair<string, string> a, KeyValuePair<string, string> b)
        {
            return a.Key.CompareTo(b.Key);
        }

        public ComboBoxCulture()
        {
            DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            Items.Clear();
            SourceList = new List<KeyValuePair<string, string>>();
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
                SourceList.Add(new KeyValuePair<string, string>(ci.Name, ci.EnglishName + " [" + ci.NativeName + ", " + ci.Name + "]"));
        }

        private string selectedelement = "";
        public string SelectedElement
        {
            get { return selectedelement; }
            set
            {
                selectedelement = value;
                if (DataSource != null && ValueMember.Length > 0) SelectedValue = Convert.ToString(value);
            }
        }

        protected override void InitLayout()
        {
            base.InitLayout();
            DataSource = null;
            DisplayMember = "";
            ValueMember = "";
            Items.Clear();
            if (!DesignMode)
            {
                DisplayMember = "Value";
                ValueMember = "Key";
                SourceList.Sort(Compare1);
                DataSource = SourceList;
            }
        }

        protected override void OnSelectedValueChanged(EventArgs e)
        {
            // get the selected item in the combobox
            if (SelectedItem != null)
            {
                KeyValuePair<string, string> selectedpair = (KeyValuePair<string, string>)SelectedItem;
                SelectedElement = selectedpair.Key;
            }
            else SelectedElement = "";
            base.OnSelectedValueChanged(e);
        }
    }
}
