using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RmSplit
{
    public partial class SelectPersonForm : Form
    {
        SortedDictionary<String, int> list = null;
        String searchString = "";
        List<string> slist = new List<string>();
        int textLine;
        int rin=-1;

        public int RIN { get { return rin; } }

        public SelectPersonForm(SortedDictionary<String, int> nameList)
        {
            InitializeComponent();
            list = nameList;
            initializeNameList();
        }

        private void initializeNameList()
        {
            // Initialize search labels
            searchLabel.Text = "";
            selectedLabel.Text = "";
            // Fill the name box with an array of name strings
            foreach (KeyValuePair<String, int> kvp in list)
            {
                slist.Add(kvp.Key);
            }
            textLine = 0;
            displayTextAtLine();
            nameBox.Lines = slist.ToArray();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            // return the rin of the selected name
            if (selectedLabel.Text.Length > 0)
            {
                rin = list[selectedLabel.Text];
            } else {
                rin = -1;
            }
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            // just to be safe, set the return value to ""
            rin = -1;
            Close();
        }

        private void nameBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                // Back-space
                if (searchString.Length > 0)
                {
                    searchString = searchString.Substring(0, searchString.Length - 1);
                }
                e.Handled = true;
            }
        }

        private void nameBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
            slist.Clear();
            if (ch == (char)8)
            {
                // backspace still passed even though I handled it in key-down.  Skip processing
            }
            else if (ch == (char)21)
            {
                // Ctrl-U
                searchString = "";
            }
            else
            {
                if (ch >= 'a' || ch <= 'z')
                    ch = Char.ToUpper(ch, System.Globalization.CultureInfo.InvariantCulture);
                searchString += ch;
            }
            // update the display of the search string
            searchLabel.Text = searchString;
            // update the display in the searchable list
            foreach (KeyValuePair<String, int> kvp in list)
            {
                if (searchString.Length == 0 || kvp.Key.ToUpperInvariant().Contains(searchString))
                    slist.Add(kvp.Key);
            }
            nameBox.Lines = slist.ToArray();
            if(slist.Count == 1)
                selectedLabel.Text = slist[0];
            if (slist.Count > 0)
            {
                textLine = 0;
                displayTextAtLine();
            }
            else
            {
                textLine = -1;
                selectedLabel.Text = "";
            }
            e.Handled = true;
        }

        private void nameBox_SelectionChanged(object sender, EventArgs e)
        {
            textLine = nameBox.GetLineFromCharIndex(nameBox.SelectionStart);
            displayTextAtLine();
        }

        private void displayTextAtLine()
        {
            selectedLabel.Text = slist[textLine];
        }

        private void HelpButton_Click(object sender, EventArgs e)
        {
            new SelectPersonHelp().Show();
        }
    }
}
