using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
//using System.Net.Sockets;
//using System.Net;
using SCAN.Data.DataAccess;
using SCAN.Data.DataModel;
using System.Threading;
//using TowerLightSignal;
using Bt.ScanLib;
using Bt.CommLib;
using CsvReadWrite;
using Bt;
using System.Threading;
//using StateObjects;
using SCAN.lib;

namespace SCAN
{
    public partial class Form_WeightDataToScan : Form
    {
        [System.Runtime.InteropServices.DllImport("coredll.dll")]
        static extern short GetUserDefaultUILanguage();

        //private System.Net.Sockets.TcpClient objSck;

        //private NetworkStream stream;

        //private Int32 remotePort;

        //private string remoteHost;

        //private Socket client;

        //private Socket listener;

        //private Socket handler;

        //private static ManualResetEvent connectDone = new ManualResetEvent(false);

        //private static ManualResetEvent receiveDone = new ManualResetEvent(false);
        //// The response from the remote device.
        //private static String response = String.Empty;

        //private bool IsClose = true;

        //private bool IsProcess = true;

        //private string wightBefore = string.Empty;

        //public string MASTER_FILE = "\\FlashDisk\\BT_FILES\\drv1\\HT_WeightDataToPrint\\in\\Master.csv";

        private lib.BtScanLib btScanLib;

        public List<List<string>> Masters;

        private String weightPerQty = "0";

        private Int32 qtyShare = 0;

        List<string> tmp = null;


        //TowerlightSignal objTowerlight = new TowerlightSignal();

        public event ScanEventHandler OnScan;

        public Form_WeightDataToScan()
        {
            InitializeComponent();

            btScanLib = lib.BtScanLib.getInstance();
            btScanLib.OnScan += new SCAN.lib.ScanEventHandler(btScanLib_OnScan);
            btScanLib.Debug = false;
            btScanLib.Enable();

            Clear();
            
        }

        private void Clear()
        {
            tbxItem.Text = string.Empty;
            tbxItem.Focus();
            tbxItem.BackColor = Color.GreenYellow;
            tbxQty.Text = string.Empty;
            tbxQty.BackColor = Color.White;
            tbxWeightPlan.Text = string.Empty;
            tbxWeightPlan.BackColor = Color.White;
            tbxWeightAct.Text = string.Empty;
            tbxWeightAct.BackColor = Color.White;
            lblResult.BackColor = Color.Gray;
            lblResult.Text = string.Empty;

            Form_Login.weightData = string.Empty;

            timer1.Enabled = false;
        }


        private void Form_WeightDataToScan_Load(object sender, EventArgs e)
        {
            if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width == 240)
            {
                resolution.ScreenSize.VGAtoQVGA(this);
            }

            // フォームの最大化・最小化ボタン非表示
            this.MaximizeBox = !this.MaximizeBox;
            this.MinimizeBox = !this.MinimizeBox;


            Masters = new List<List<string>>();
            tmp = null;
            using (var csv = new CsvReader(Form_Login.MASTER_FILE))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Masters.Add(tmp);

                }

            }//using end


            //Test
            //foreach (var item in Masters)
            //{
            //    Int32 i = 0;
            //    bool IsCheck = false;
            //    foreach (var itemdetail in item)
            //    {
            //        if (i == 1)
            //        {

            //            if (itemdetail == "A")
            //            {
            //                IsCheck = true;
            //            }
            //            else
            //            {
            //                IsCheck = false;
            //            }
            //        }
            //        else if (i == 2)
            //        {
            //            if (IsCheck == true)
            //            {
            //                weightPerQty = itemdetail;
            //                tbxItem.Text = "A";
            //                tbxItem.BackColor = Color.White;
            //                tbxQty.Focus();
            //                tbxQty.BackColor = Color.GreenYellow;
            //                return;
            //            }
            //        }

            //        i = i + 1;
            //    }
            //}


            Form_Login.weightData = string.Empty;



        }

        void btScanLib_OnScan(string result)
        {
            if (tbxItem.Text.Trim() == string.Empty)
            {

                foreach (var item in Masters)
                {
                    Int32 i = 0;
                    bool IsCheck = false;
                    foreach (var itemdetail in item)
                    {
                        if (i == 1)
                        {

                            if (itemdetail == result)
                            {
                                IsCheck = true;
                            }
                            else
                            {
                                IsCheck = false;
                            }
                        }
                        else if (i == 2)
                        {
                            if (IsCheck == true)
                            {
                                weightPerQty = itemdetail;
                                tbxItem.Text = result;
                                tbxItem.BackColor = Color.White;
                                tbxQty.Focus();
                                tbxQty.BackColor = Color.GreenYellow;
                                return;
                            }
                        }

                        i = i + 1;
                    }
                }

                tbxItem.Text = string.Empty;
                
            }
            else if (tbxQty.Text.Trim() == string.Empty)
            {
                if (IsNumeric(result.Trim()))
                {
                    tbxQty.Text = result.Trim();

                    //tbxWeightPlan.Text = Convert.ToString(Convert.ToInt32(tbxQty.Text) * Convert.ToDecimal(weightPerQty));
                    tbxWeightPlan.Text = (Convert.ToInt32(tbxQty.Text) * Convert.ToDecimal(weightPerQty) * 1000).ToString("#,###");

                    qtyShare = Convert.ToInt32(tbxQty.Text);

                    Form_Login.IsContinue = true;

                    tbxWeightAct.Text = string.Empty;

                    Form_Login.weightData = string.Empty;

                    timer1.Enabled = true;
                }
                else
                {
                    qtyShare = 0;
                    tbxQty.Text = string.Empty;
                    Form_Login.IsContinue = false;
                }
                
            }


        }

        public bool IsNumeric(string value)
        {
            return value.All(char.IsNumber);
        }

      

        private void Form_WeightDataToScan_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode.ToString())
            {

                case "F1":
                    btnMainMenu_Click_1(sender, e);

                    break;

                case "F4":

                    btnClear_Click(sender, e);

                    break;
            }

        }

        private void btnMainMenu_Click_1(object sender, EventArgs e)
        {
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();

            //BiCom.btBiComDisconnect();
            Form_Menu._Form_MenuInstance.Show();
            Form_Login.weightData = string.Empty;
            Form_Login.IsCloseShare = true;
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
           
            Clear();
        }

        private void tbxQty_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode.ToString())
            {
                case "Return":

                    if (tbxQty.Text.Trim() != string.Empty)
                    {
                        if (IsNumeric(tbxQty.Text.Trim()))
                        {                            
                            //tbxWeightPlan.Text = Convert.ToString(Convert.ToInt32(tbxQty.Text) * Convert.ToDecimal(weightPerQty));
                            tbxWeightPlan.Text = (Convert.ToInt32(tbxQty.Text) * Convert.ToDecimal(weightPerQty) * 1000).ToString("#,###");

                            qtyShare = Convert.ToInt32(tbxQty.Text);

                            Form_Login.IsContinue = true;

                            tbxWeightAct.Text = string.Empty;

                            Form_Login.weightData = string.Empty;

                            timer1.Enabled = true;
                        }
                        else
                        {
                            qtyShare = 0;
                            tbxQty.Text = string.Empty;
                            Form_Login.IsContinue = false;
                        }


                    }

                    break;

                case "F1":
                    btnMainMenu_Click_1(sender, e);

                    break;

                case "F4":

                    btnClear_Click(sender, e);

                    break;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            String strtbxWeightAct = "0";            

            if (!Form_Login.weightData.Contains("0.00") && (!Form_Login.weightData.Contains("0.000") && (Form_Login.weightData.Trim() != string.Empty)))
            {
                if (Form_Login.weightData.Contains("kg"))
                {
                    //tbxWeightAct.Text = Form_Login.weightData.Substring(0, Form_Login.weightData.Length - 3);
                    strtbxWeightAct = Form_Login.weightData.Substring(0, Form_Login.weightData.Length - 3);
                }
                else
                {
                    //tbxWeightAct.Text = Form_Login.weightData;
                    strtbxWeightAct = Form_Login.weightData;
                }
                

                timer1.Enabled = false;

                Decimal weightAct = 0;
                Decimal weightPlanFrom = 0;
                Decimal weightPlanTo = 0;
                Decimal weightPlan = 0;
                Decimal weightDifferent = 0;
                String strWeightCheck = "0.5";
                Decimal weightCheck = Convert.ToDecimal(strWeightCheck);


                try
                {
                    weightAct = Convert.ToDecimal(strtbxWeightAct) * 1000;
                    //weightPlanFrom = Convert.ToDecimal(tbxWeightPlan.Text) - Convert.ToDecimal(0.0005 * qtyShare);
                    //weightPlanTo = Convert.ToDecimal(tbxWeightPlan.Text) + Convert.ToDecimal(0.0005 * qtyShare);
                    weightPlan = Convert.ToDecimal(tbxWeightPlan.Text);
                }
                catch (Exception)
                {
                    
                    
                }

                weightDifferent = (weightPlan / qtyShare) - (weightAct / qtyShare);

                if ((weightDifferent > 0) && (weightDifferent <= weightCheck))
                {
                    weightAct = weightPlan;
                }

                tbxWeightAct.Text = weightAct.ToString("#,###");

                //if ((weightAct >= weightPlanFrom) && (weightAct <= weightPlanTo) && (weightAct != 0) && (weightPlan != 0))
                if ((weightAct == weightPlan) && (weightAct != 0) && (weightPlan != 0))
                {
                    lblResult.Text = "OK";
                    lblResult.BackColor = Color.Green;
                }
                else
                {
                    lblResult.Text = "NG";
                    lblResult.BackColor = Color.Red;
                }

            }
            


        }

        private void tbxItem_TextChanged(object sender, EventArgs e)
        {

        }
    }
}