using AR;
using Org.BouncyCastle.Utilities;
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;
using System.Windows.Forms.VisualStyles;

namespace UniMarc
{
    public partial class Check_Copy_Sub_Selector : Form
    {
        string[] Items;
        public string SelectedValue = string.Empty;
        public Check_Copy_Sub_Selector(string[] arrays, string search)
        {
            InitializeComponent();
            this.KeyPreview = true;
            tbSearch.Text = search;
            this.Items = arrays;
            this.KeyDown += (s1, e1) => {
                if (e1.KeyCode == Keys.Escape) this.Close();
            };
            this.tbSearch.ImeMode = ImeMode.Hangul;
            this.tbSearch.KeyDown += (s1, e1) => {
                if (e1.KeyCode == Keys.Enter) btFind.PerformClick();
            };
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Skill_Grid sg = new Skill_Grid();
            sg.Print_Grid_Num(sender, e);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            int row = e.RowIndex;

            SetCode(e.RowIndex);
            this.Close();
        }

        void SetCode(int rowindex)
        {
            var drow = dv1.Rows[rowindex];
            var title = drow.Cells[0].Value?.ToString() ?? string.Empty;
            this.SelectedValue = title;
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (dv1.Rows.Count <= 0) return;
                SetCode(dv1.CurrentRow.Index);
                this.Close();
            }
            if (e.KeyCode == Keys.Escape)
            {
                SelectedValue = string.Empty;
                this.Close();
            }
            
        }


        private void btFind_Click(object sender, EventArgs e)
        {
            var search = tbSearch.Text.Trim();
            this.dv1.Rows.Clear();
            foreach (var item in this.Items)
            {
                if (search.isEmpty() || item.ToLower().Contains(search.ToLower()))
                    this.dv1.Rows.Add(item);
            }
            if (search.isEmpty() == true)
            {
                tbSearch.Focus();
                tbSearch.SelectAll();
            }
            else if (dv1.Rows.Count > 0)
            {
                dv1.Focus();
            }
            else
            {
                tbSearch.Focus();
                tbSearch.SelectAll();
            }
                
        }

        private void Check_Copy_Sub_Selector_Load(object sender, EventArgs e)
        {
            this.Show();
            Application.DoEvents();
            this.btFind.PerformClick(); 
        }
    }
}
