using ProductionControl.UIExtend;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProductionControl
{
    public partial class FrmInput : Form
    {
        private TextBoxRemind remind = null;
        private string[] listData;

        public string inputData = "";

        public FrmInput(List<string> list, string subject = null,string defaultData="")
        {
            InitializeComponent();
            if (list == null)
            {
                this.cobList.Visible = false;
                this.textBox1.Text = defaultData;
                remind = new TextBoxRemind();
                remind.InitAutoCompleteCustomSource(textBox1);
            }
            else
            {
                this.textBox1.Visible = false;
                this.cobList.DropDownStyle = ComboBoxStyle.DropDown;//可输入下拉框
                 
                //this.cobList.DataSource = list;
                //cobList.AutoCompleteSource = AutoCompleteSource.ListItems;
                //cobList.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.listData=list.ToArray();
                cobList.Items.AddRange(listData);//比使用DataSource速度要快一些
                cobList.TextUpdate += cobList_TextUpdate;//重新绑定事件
                cobList.KeyDown += CobList_KeyDown;
                this.cobList.Text = defaultData;
                cobList.Focus();
                cobList.SelectAll();
            }

            //
            if (!string.IsNullOrWhiteSpace(subject)) 
                this.Text = subject; 
        }

        private void CobList_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (cobList.Items.Count ==1)
                    cobList.Text= cobList.Items[0].ToString();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (textBox1.Visible)
            {
                inputData = this.textBox1.Text.Trim();
                if (inputData.Trim() == "") return;

                //记忆
                remind.Remind(inputData); 
            }
            else
            {
                if (cobList.Items.Count <1)
                    return;
                string tmp = this.cobList.Text.Trim();
                var text = listData.FirstOrDefault(x => x== tmp);//忽略大小写
                if (string.IsNullOrEmpty(text))
                    return;
                inputData = text; 
            }
            this.DialogResult = DialogResult.OK;    
        }
        private void cobList_TextUpdate(object sender, EventArgs e)
        {
            string str = cobList.Text;  //获取cb_material控件输入内 
            //清空combobox
            cobList.DataSource = null;
            cobList.Items.Clear();

            var workOrderFiltered = listData.Where(x => x.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();//忽略大小写
            cobList.Items.AddRange(workOrderFiltered);//比使用DataSource速度要快一些
 
            // 不存在符合条件时
            //设置光标位置，若不设置：光标位置始终保持在第一列，造成输入关键词的倒序排列
            cobList.Cursor = Cursors.Default; //保持鼠标指针原来状态，有时候鼠标指针会被下拉框覆盖，所以要进行一次设置 
            if (workOrderFiltered.Length > 0)
            {
                if (!cobList.DroppedDown)
                    cobList.DroppedDown = true; // 自动弹出下拉框 
            }
            //else if (cobList.DroppedDown)
            //    cobList.DroppedDown = false;
            cobList.SelectionStart = str.Length;  // 设置光标位置，若不设置：光标位置始终保持在第一列，造成输入关键词的倒序排列
        }
 

        private void FrmInput_Load(object sender, EventArgs e)
        {

        }

        private void FrmInput_Shown(object sender, EventArgs e)
        {
            if(textBox1.Visible)
                textBox1.Focus();
        }
    }
}
