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 UniMarc
{
    public partial class Check_Copy_Sub_Search : Form
    {
        Form cc;
        public Check_Copy_Sub_Search(Form _cc)
        {
            InitializeComponent();
            cc = _cc;
        }

        public void Init(string[] ary, string SearchText)
        {
            this.Text = string.Format("검색어 : [{0}]", SearchText);

            // Province, Area, lib_name, Code
            string[] grid = { "", "", "", "", "", "" };
            for (int a = 0; a < ary.Length; a++)
            {
                if (a % 6 == 0) grid[0] = ary[a];
                if (a % 6 == 1) grid[1] = ary[a];
                if (a % 6 == 2) grid[2] = string.Format("{0}_{1}_{2}", grid[0], grid[1], ary[a]);
                if (a % 6 == 3) grid[3] = ary[a];
                if (a % 6 == 4) grid[4] = ary[a];
                if (a % 6 == 5)
                {
                    grid[5] = ary[a];
                    dataGridView1.Rows.Add(grid);
                }
            }
        }

        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)
        {
            int row = e.RowIndex;
            SetCode(row);
            this.Close();
        }

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

        void SetCode(int row)
        {
            string Province = dataGridView1.Rows[row].Cells["Province"].Value.ToString();
            string Area = dataGridView1.Rows[row].Cells["Area"].Value.ToString();
            string Code = dataGridView1.Rows[row].Cells["Code"].Value.ToString();
            string URL = dataGridView1.Rows[row].Cells["URL"].Value.ToString();
            string lib_name = dataGridView1.Rows[row].Cells["lib_name"].Value.ToString();
            string searcher = dataGridView1.Rows[row].Cells["dvc_searcher"].Value.ToString();

            string lib_Category = string.Format("{0}_{1}", Province, Area);

            if (cc is Check_copy)
            {
                var cc2 = cc as Check_copy;
                cc2.lib_Category = lib_Category;
                cc2.Code = Code;
                cc2.URL = URL;
                cc2.tb_SearchTarget.Text = lib_name;
                cc2.SearcherNo = searcher;
            }
            else if (cc is Check_copyWD)
            {
                var cc2 = cc as Check_copyWD;
                cc2.lib_Category = lib_Category;
                cc2.Code = Code;
                cc2.URL = URL;
                cc2.tb_SearchTarget.Text = lib_name;
                cc2.SearcherNo = searcher;
                cc2.groupBox1.Enabled = int.TryParse(searcher, out int vno) && vno > 0;
                cc2.chkRetryErrData.Enabled = cc2.groupBox1.Enabled;
            }

            if (lib_name.Contains("Kolasys.net"))
            {
                Check_Copy_Login ccl = new Check_Copy_Login(this);
                ccl.Show();
            }
        }

        public void SetLogin(string id, string pw)
        {
            if (cc is Check_copy)
            {
                var cc2 = cc as Check_copy;
                cc2.lbl_ID.Text = id;
                cc2.lbl_PW.Text = pw;
            }
            else if (cc is Check_copyWD)
            {
                var cc2 = cc as Check_copyWD;
                cc2.lbl_ID.Text = id;
                cc2.lbl_PW.Text = pw;
            }
        
        }
    }
}
