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 Bt.ScanLib;
using Bt;
using CsvReadWrite;
using System.Data.SqlClient;

namespace HT_SKT_Stock
{
    public partial class FrmLogin : Form
    {

        public static FrmLogin _Form_LoginInstance;
        public static string strConnString;
        public static string strQty;
        public String AppPath = "";

        public int ScanMode = 0;
        public int ScanStep = 0;
        public int StepMax = 0;
        public string userName = "";
        public string DevNO = "HT01";

        public List<List<string>> userList;
        public List<List<string>> mngList;
        public List<List<string>> Settings;
        private BtScanLib btScanLib;

        //--------------------------------------------------------------
        // DLLImport
        //--------------------------------------------------------------
        [DllImport("coredll.dll", EntryPoint = "DeleteObject")]
        public static extern bool DeleteObject(IntPtr hObject);

        public FrmLogin()
        {
            InitializeComponent();

            // Enable BTScanLib
            btScanLib = BtScanLib.getInstance();
            btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
            btScanLib.Debug = false;
            btScanLib.Enable();
            
        }

        void btScanLib_OnScan(string result)
        {
            //Users.User user;
            //if (!userList.TryGetValue(result, out user))
            //{
            //    // Alert
            //    lib.BtSysLib.vibrateOn(100, 1);
            //    lib.BtSysLib.buzzerOn(400, 1);

            //    lblError.Visible = true;
            //    return;
            //}
            //lblUid.Text = result;
            //cmbUsername.SelectedIndex = cmbUsername.Items.IndexOf(user.Name);
            //lblError.Visible = false;
            //txtPassword.Focus();


            cmbUserID.SelectedIndex = cmbUserID.Items.IndexOf(result);
            txtPassword.Focus();


        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string tgtPass = userList[cmbUserID.SelectedIndex][1].Replace("\r", "");

                if (tgtPass == txtPassword.Text)
                {
                    this.userName = userList[cmbUserID.SelectedIndex][0].Replace("\r", "");
                    Form frmD = new FrmMainMenu();
                    frmD.Show();
                    this.Hide();
                }
                else
                {
                    PassErr err = new PassErr();
                    err.ShowDialog();
                    txtPassword.Text = "";
                }
            }
            catch (Exception ee)
            {
            }
            

        }

        private void FrmLogin_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;

            _Form_LoginInstance = this;

            this.Text = "Stock System V.1.0.3";

            //strConnString = "Server=202.151.188.68;UID=sa;PASSWORD=v6f,l6-46@2021;database=wms-skt; " +
            //" Connect Timeout=600;";

            strConnString = DBConstr.settingDB(); 
                        
            String path = this.GetType().Assembly.GetModules()[0].FullyQualifiedName;
            Int32 en = path.LastIndexOf("\\");
            AppPath = path.Substring(0, en);

            cmbUserID.Focus();

            SetUserInfo();

            //scan setting
            FrmLogin._Form_LoginInstance.ScanMode = 0;
            FrmLogin._Form_LoginInstance.StepMax = 1;
            FrmLogin._Form_LoginInstance.ScanStep = 0;
            //Bt.ScanLib.Control.btScanEnable();

            //Bt.ScanLib.Utility.btScanSetServiceEnable(0);
        }

        private void SetUserInfo()
        {
            try
            {

           

            userList = new List<List<string>>();
                       
             SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();

            DataSet ds = new DataSet();
            DataTable dt;
            String strSQL;

            //strConnString = "Server=202.151.188.68;UID=sa;PASSWORD=v6f,l6-46@2021;database=wms-skt; " +
            //" Connect Timeout=600;";

            strSQL = "EXEC sp_get_user @status = 0";

            objConn.ConnectionString = strConnString;
            objConn.Open();
            objCmd.Connection = objConn;
            objCmd.CommandText = strSQL;
            objCmd.CommandType = CommandType.Text;
                                    
            dtAdapter.SelectCommand = objCmd;

            dtAdapter.Fill(ds);
            dt = ds.Tables[0];

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {

                string strId = dt.Rows[i]["id"].ToString();
                string strPassword = dt.Rows[i]["password"].ToString();

                var userRead = new List<string> { strId, strPassword };

                userList.Add(userRead);
                             
            }

            dtAdapter = null;
            objConn.Close();
            objConn = null;
        

            
            //set comboBox
            cmbUserID.Items.Clear();
            txtPassword.Text = "";
            for (int i = 0; i < userList.Count; i++)
            {
                cmbUserID.Items.Add(userList[i][0].Replace("\r", ""));
            }

            }
            catch (Exception)
            {

            }

            List<string> tmp = null;
            mngList = new List<List<string>>();
            tmp = null;
            using (var csv = new CsvReader("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\MngInfo.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    mngList.Add(tmp);

                }

            }//using end

        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }

            
        }

        
    }
}