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 FBS0000.Holiday
{
    public partial class fImpKunTae : FCOMMON.fBase
    {
        public fImpKunTae()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
        }

        private void fImpKunTae_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText() == false)
            {
                FCOMMON.Util.MsgE("텍스트 자료가 없습니다");
                return;
            }

            if (this.listView1.Items.Count > 0)
            {
                if (FCOMMON.Util.MsgQ("현재 내용을 삭제할까요?") == DialogResult.Yes)
                {
                    this.listView1.Items.Clear();
                }
            }

            listView1.SuspendLayout();
            var lines = Clipboard.GetText().Replace("\r", "").Split('\n');
            foreach (var line in lines)
            {
                var buffer = line.Split('\t');
                if (buffer.Length < 4) continue;

                var vdate = buffer[0].Trim();
                if (DateTime.TryParse(vdate, out DateTime val_date) == false) continue;

                var vno = buffer[1].Trim();
                if (string.IsNullOrEmpty(vno)) continue;

                var vcate = buffer[2].Trim();
                if (string.IsNullOrEmpty(vcate)) continue;

                var drday = buffer[3].Trim();
                if (float.TryParse(drday, out float val_drday) == false) continue;

                string drtime = "0";
                if (buffer.Length > 4) drtime = buffer[4].Trim();
                float.TryParse(drtime, out float val_drtime);

                string crday = "0";
                if (buffer.Length > 5) crday = buffer[5].Trim();
                float.TryParse(crday, out float val_crday);

                string crtime = "0";
                if (buffer.Length > 6) crtime = buffer[6].Trim();
                float.TryParse(crtime, out float val_crtime);

                var lv = this.listView1.Items.Add(val_date.ToString("yyyy-MM-dd"));
                lv.SubItems.Add(vno);
                lv.SubItems.Add(vcate);
                lv.SubItems.Add(drday.ToString());
                lv.SubItems.Add(drtime.ToString());
                lv.SubItems.Add(crday.ToString());
                lv.SubItems.Add(crtime.ToString());
                lv.Checked = true;

            }
            listView1.ResumeLayout();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var items = this.listView1.CheckedItems;
            if (items.Count < 1)
            {
                FCOMMON.Util.MsgE("선택된 아이템이 없습니다");
                return;
            }

            if (FCOMMON.Util.MsgQ($"{items.Count} 건의 자료를 입력할까요?") != DialogResult.Yes) return;

            this.progressBar1.Maximum = items.Count;
            this.progressBar1.Value = 0;
            var ta = new dsMSSQLTableAdapters.HolydayTableAdapter();
            foreach (ListViewItem item in items)
            {
                ta.InsertShort(item.SubItems[2].Text, item.SubItems[0].Text, item.SubItems[0].Text, double.Parse(item.SubItems[4].Text),
                    "PASTE", "PASTE", item.SubItems[1].Text, FCOMMON.info.Login.no, DateTime.Now,
                    double.Parse(item.SubItems[3].Text), FCOMMON.info.Login.gcode,
                    double.Parse(item.SubItems[5].Text), double.Parse(item.SubItems[6].Text), "CLIPBOARD");
                this.progressBar1.Value += 1;
            }
        }
    }
}
