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 SCAN.Data.DataAccess;
using Bt.ScanLib;

using System.Runtime.InteropServices;
using CsvReadWrite;
using SCAN.lib;
using Bt.CommLib;
using Bt.ScanLib;

using Bt;

namespace SCAN
{
    public partial class Form_SettingsB : Form
    {
        public static Form_SettingsB _Form_SettingsB;

        private lib.BtScanLib btScanLib;

        public Form_SettingsB()
        {
            InitializeComponent();

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


        private void btnMainMenu_Click_1(object sender, EventArgs e)
        {
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();
            Bluetooth.btBluetoothClose();
            Form_Menu._Form_MenuInstance.Show();
            this.Close();
        }

        void btScanLib_OnScan(string result)
        {
            textBoxAddress.Text = result;
        }

        private void buttonConnect_Click_1(object sender, EventArgs e)
        {
            List<string> tmp = null;
            List<string> row = null;

            try
            {

                row = new List<string>();

                row.Add(textBoxAddress.Text);

                using (var csv = new CsvWriter("\\FlashDisk\\BT_FILES\\drv1\\HT_WeightDataToPrint\\in\\Settings.csv"))
                {

                    csv.WriteRow(row);
                }

                Form_Login._Form_LoginInstance.Settings = new List<List<string>>();
                tmp = null;
                using (var csv = new CsvReader("\\FlashDisk\\BT_FILES\\drv1\\HT_WeightDataToPrint\\in\\Settings.csv"))
                {//mysql
                    //setting list comName, WatchCnt, RetryCnt;
                    //SettingList[8]no 0 timer interval, 1 share id, 2, share pass, 3 upload path 4 share dir
                    while ((tmp = csv.ReadRow()) != null)
                    {
                        Form_Login._Form_LoginInstance.Settings.Add(tmp);

                    }

                }//using end
                if (connectbluetooth())
                {
                    lblError.ForeColor = Color.Green;
                    lblError.Text = "Connect Correct";
                }
                else
                {
                    lblError.ForeColor = Color.Red;
                    lblError.Text = "Connect Error";
                }

            }
            catch (Exception)
            {


            }
        }

        private bool connectbluetooth()
        {

            string sName = "";
            //string sAddr = Form_Login._Form_LoginInstance.Settings[0][2];

            string sAddr = textBoxAddress.Text;

            Bluetooth.btBluetoothClose();
            Bluetooth.btBluetoothOpen();
            Bluetooth.btBluetoothSPPDisconnect();
            //BiCom.btBiComDisconnect();

            LibDef.BT_BLUETOOTH_TARGET target = new LibDef.BT_BLUETOOTH_TARGET();
            target.name = sName;
            target.addr = sAddr;

            Int32 ret;
            //ret = BiCom.btBiComConnect(target, 30000);
            ret = Bluetooth.btBluetoothSPPConnect(target, 30000);

            //MessageBox.Show(ret.ToString() + " | " + LibDef.BT_OK.ToString());

            if (ret != LibDef.BT_OK)
            {
                MessageBox.Show("Error Connect Bluetooth");
                //btScanLib.OnScan -= btScanLib_OnScan;
                //btScanLib.Disable();
                //BiCom.btBiComDisconnect();
                Bluetooth.btBluetoothSPPDisconnect();
                return false;
            }
            return true;

                        

        }


        private void Form_SettingsB_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_SettingsB = this;

            lblError.Text = "";
            textBoxAddress.Text = Form_Login._Form_LoginInstance.Settings[0][0];

        }

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

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

                    break;

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

                    break;

            }

        }
    }
}