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 AR;

namespace Project.Dialog
{
    public partial class fManualPrint : Form
    {
        bool ReprintMode = false;
        public fManualPrint()
        {
            InitializeComponent();
            this.FormClosed += FManualPrint_FormClosed;
        }
        public fManualPrint(string sid, string lot, string qty, string mfg, string rid, string spy, string pn)
        {
            InitializeComponent();
            this.FormClosed += FManualPrint_FormClosed;
            this.tbSID.Text = sid;
            this.tbVLot.Text = lot;
            this.tbQty.Text = qty;
            this.tbDate.Text = mfg;
            this.tbRID.Text = rid;
            this.tbSPY.Text = spy;
            this.tbPN.Text = pn;
            chkDelinfo.Enabled = false;
            chkDelinfo.Checked = false;
            tbBarcodeInput.Enabled = false;
            this.ReprintMode = true;
        }

        private void fManualPrint_Load(object sender, EventArgs e)
        {
            var lockvalue = true;
            PUB.iLock[(int)eAxis.PZ_PICK].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.Z_THETA].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.PX_PICK].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");

            PUB.iLock[(int)eAxis.PL_MOVE].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.PR_MOVE].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");

            if (ReprintMode) button1.Focus();
            else tbBarcodeInput.Focus();
        }

        private void FManualPrint_FormClosed(object sender, FormClosedEventArgs e)
        {
            //PUB.LockPKT.set((int)eILockPKX.MPrint, false, "COMMINTERLOCK");
            var lockvalue = false;
            PUB.iLock[(int)eAxis.PZ_PICK].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.Z_THETA].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.PX_PICK].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");

            PUB.iLock[(int)eAxis.PL_MOVE].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.PR_MOVE].set((int)eILock.MPrint, lockvalue, "COMMINTERLOCK");
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            //프린트해야한다
            string zpl = "";
            string qrdata = "";

            var Printer = radLeft.Checked ? PUB.PrinterL : PUB.PrinterR;
            var sid = tbSID.Text.Trim();
            var rid = tbRID.Text.Trim();
            var lot = tbVLot.Text.Trim();
            var mfg = tbDate.Text.Trim();
            var qty = tbQty.Text.Trim();
            var spy = tbSPY.Text.Trim();
            var pan = tbPN.Text.Trim();

            if (int.TryParse(qty, out int vqty) == false)
            {
                UTIL.MsgE("Please enter quantity as a number");
                tbQty.SelectAll();
                tbQty.Focus();
                return;
            }

            zpl = Printer.makeZPL_210908(new Class.Reel
            {
                SID = sid,
                venderLot = lot,
                venderName = spy,
                qty = vqty,
                id = rid,
                mfg = mfg,
                PartNo = pan,
            }, SETTING.Data.DrawOutbox, out qrdata);

            var cnt = (int)nudCnt.Value;
            for (int i = 0; i < cnt; i++)
            {
                var prn = Printer.Print(zpl);
                if (prn.result == false)
                {
                    //인쇄실패시 처리하지 않음
                    PUB.log.AddE(prn.errmessage);
                    UTIL.MsgE("Cannot proceed further due to printing failure");
                    break;
                }
                else
                {
                    if (i == 0) //첫장만 로깅
                        PUB.log.Add("User print completed" + string.Join("|", new string[] { sid, lot, spy, qty, rid, mfg, pan }));
                }
                System.Threading.Thread.Sleep(100);
            }

            if (ReprintMode) DialogResult = DialogResult.OK;
            else
            {
                //자료를 삭제해야한다
                if (chkDelinfo.Checked)
                {
                    ClearData(tbSID, tbVLot, tbQty, tbDate, tbRID, tbSPY, tbPN);
                    tbSID.Focus();
                }

            }

        }
        void ClearData(params TextBox[] tbs)
        {
            foreach (var tb in tbs)
            {
                tb.Text = "";
                tb.Focus();
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //sid
            ClearData(tbSID);
        }


        private void button2_Click(object sender, EventArgs e)
        {
            //vlot
            ClearData(tbVLot);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //qty
            ClearData(tbQty);
        }


        private void button4_Click(object sender, EventArgs e)
        {
            //date
            ClearData(tbDate);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //rid
            ClearData(tbRID);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //spy
            ClearData(tbSPY);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //pn
            ClearData(tbPN);
        }

        private void tbSID_Click(object sender, EventArgs e)
        {
            var tb = sender as TextBox;
            var rlt = AR.UTIL.InputBox("Please enter a value", tb.Text, AR.UTIL.eInputbox.TouchFullSingleLine);
            if (rlt.Item1) tb.Text = rlt.Item2;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            tbSPY.Text = "N/A";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            tbPN.Text = "N/A";
        }

        private void tbBarcodeInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var buf = tbBarcodeInput.Text.Trim().Split(';');
                if (buf.Length != 7)
                {
                    UTIL.MsgE("Barcode data does not contain 7 elements");
                    tbBarcodeInput.SelectAll();
                    return;
                }
                if (int.TryParse(buf[3], out var code) == false)
                {
                    UTIL.MsgE("The 4th data (quantity) is not a number\nOnly Amkor STD barcodes are allowed");
                    return;
                }
                tbSID.Text = buf[0];
                tbVLot.Text = buf[1];
                tbSPY.Text = buf[2];
                tbQty.Text = buf[3];
                tbRID.Text = buf[4];
                tbDate.Text = buf[5];
                tbPN.Text = buf[6];
                tbBarcodeInput.SelectAll();
                tbBarcodeInput.Focus();
            }
        }
    }
}
