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 System.Threading;

using Bt;


namespace SCAN
{
    public partial class Form_Menu : Form
    {
        public static Form_Menu _Form_MenuInstance;

        public static Form frmB;

        public Form_Menu()
        {
            InitializeComponent();
        }

        private void Form_Menu_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_MenuInstance = this;

            //WLANOpen();

        }

        private void btnAppClose_Click(object sender, EventArgs e)
        {
            try
            {
                Bluetooth.btBluetoothClose();
                Form_Login.IsAppExit = true;
                //WLANClose();
                //Thread.Sleep(500);
                //WLANOpen();
                                                
                Thread.Sleep(500);

                Application.Exit();
                //this.Dispose();
                //this.Close();

            }
            catch (Exception ex)
            {

            }
        }

        //private void WLANopen()
        //{
        //    Int32 ret = 0;
        //    String disp = "";

        //    String strValueSet = "";
        //    UInt32 ulValueSet = 0;
        //    IntPtr pstrValueSet;
        //    IntPtr plValueSet;
        //    UInt32 statusGet = 0;
        //    UInt32 typeSet = 0;
        //    UInt32 levelRssiGet = 0;
        //    UInt32 levelSnrGet = 0;

        //    plValueSet = Marshal.AllocCoTaskMem(Marshal.SizeOf(ulValueSet));

        //    try
        //    {
        //        // Set the required properties
        //        // SSID (infrastructure)
        //        strValueSet = "bttest";
        //        pstrValueSet = Marshal.StringToBSTR(strValueSet);
        //        ret = Wlan.btWLANSetProperty((UInt32)LibDef.BTWLAN_PROPID.BT_WLAN_PROP_SSID_INFRA, pstrValueSet);
        //        Marshal.FreeBSTR(pstrValueSet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANSetProperty error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }
        //        // Network mode (infrastructure/Ad Hoc)
        //        ulValueSet = LibDef.BT_WLAN_BSS_INFRASTRUCTURE;
        //        Marshal.WriteInt32(plValueSet, (Int32)ulValueSet);
        //        ret = Wlan.btWLANSetProperty((UInt32)LibDef.BTWLAN_PROPID.BT_WLAN_PROP_NETMODE, plValueSet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANSetProperty error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }
        //        // Encryption mode
        //        ulValueSet = LibDef.BT_WLAN_ENC_WEP;
        //        Marshal.WriteInt32(plValueSet, (Int32)ulValueSet);
        //        ret = Wlan.btWLANSetProperty((UInt32)LibDef.BTWLAN_PROPID.BT_WLAN_PROP_ENCRYPTION_INFRA, plValueSet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANSetProperty error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }
        //        // Authentication mode
        //        ulValueSet = LibDef.BT_WLAN_AUTH_OPEN;
        //        Marshal.WriteInt32(plValueSet, (Int32)ulValueSet);
        //        ret = Wlan.btWLANSetProperty((UInt32)LibDef.BTWLAN_PROPID.BT_WLAN_PROP_AUTHENTICATION_INFRA, plValueSet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANSetProperty error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }
        //        // WEP key character string 1 (infrastructure)
        //        strValueSet = "1234567890123";
        //        pstrValueSet = Marshal.StringToBSTR(strValueSet);
        //        ret = Wlan.btWLANSetProperty((UInt32)LibDef.BTWLAN_PROPID.BT_WLAN_PROP_WEP_KEY1, pstrValueSet);
        //        Marshal.FreeBSTR(pstrValueSet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANSetProperty error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }
        //        // WEP key index (infrastructure)
        //        ulValueSet = 1;
        //        Marshal.WriteInt32(plValueSet, (Int32)ulValueSet);
        //        ret = Wlan.btWLANSetProperty((UInt32)LibDef.BTWLAN_PROPID.BT_WLAN_PROP_WEP_KEYINDEX_INFRA, plValueSet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANSetProperty error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }

        //        // Open processing
        //        ret = Wlan.btWLANOpen();
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANOpen error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }

        //        disp = "Confirm the connection with the access point.";
        //        //MessageBox.Show(disp);

        //        ret = Wlan.btWLANGetStatus(ref statusGet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANGetStatus error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }

        //        typeSet = LibDef.BT_WLAN_TYPE_RSSI;
        //        ret = Wlan.btWLANGetSignalLevel(typeSet, ref levelRssiGet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANGetSignalLevel error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }

        //        typeSet = LibDef.BT_WLAN_TYPE_SNR;
        //        ret = Wlan.btWLANGetSignalLevel(typeSet, ref levelSnrGet);
        //        if (ret != LibDef.BT_OK)
        //        {
        //            disp = "btWLANGetSignalLevel error ret[" + ret + "]";
        //            //MessageBox.Show(disp, "Error");
        //            return;
        //        }

        //        disp =
        //            "Status:" + statusGet + "\r\n" +
        //            "RSSI level:" + levelRssiGet + "\r\n" +
        //            "SNR level:" + levelSnrGet + "\r\n";
        //        //MessageBox.Show(disp, "Open wireless LAN");
        //    }
        //    catch (Exception ex)
        //    {
        //        //MessageBox.Show(ex.ToString());
        //    }
        //    finally
        //    {
        //        Marshal.FreeCoTaskMem(plValueSet);
        //    }
        //}

        private void WLANOpen()
        {

            Int32 ret = 0;
            String disp = "";

            try
            {
                ret = Wlan.btWLANOpen();
                if (ret != LibDef.BT_OK)
                {
                    disp = "btWLANOpen error ret[" + ret + "]";
                    //MessageBox.Show(disp, "Error");
                    return;
                }
                disp = "Wireless LAN has been open.\r\n";
                //MessageBox.Show(disp, "Close wireless LAN");
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }

        }

        private void WLANClose()
        {

            Int32 ret = 0;
            String disp = "";

            try
            {
                ret = Wlan.btWLANClose();
                if (ret != LibDef.BT_OK)
                {
                    disp = "btWLANClose error ret[" + ret + "]";
                    //MessageBox.Show(disp, "Error");
                    return;
                }
                disp = "Wireless LAN has been closed.\r\n";
                //MessageBox.Show(disp, "Close wireless LAN");
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }

        }

        private void btnMatching_Click(object sender, EventArgs e)
        {
            //if (connectbluetooth())
            //{
            //    Form frmE = new Form_WeightDataToPrintB();
            //    frmE.Show();
            //    this.Hide();

                
            //}

            if (Form_Login.IsCloseShare == false)
            {
                //Form frmB = new Form_WeightDataToPrintB();
                frmB = new Form_WeightDataToPrintB();
                frmB.Show();
                frmB.Hide();
            }

            Form frmE = new Form_WeightDataToScan();
            frmE.Show();

            this.Hide();



            //connectbluetooth();
            //Form frmE = new Form_WeightDataToPrintB();
            //frmE.Show();
            //this.Hide();
            

        }



        

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

                case "F1":

                    btnMatching_Click(sender, e);

                    break;
                case "F2":

                    btnIPWeight_Click(sender, e);

                    break;

                case "F3":

                    btnSetting_Click(sender, e);

                    break;

                case "F4":

                    btnAppClose_Click(sender, e);

                    break;
            }

        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            Form frmE = new Form_SettingsB();
            frmE.Show();
            this.Hide();
        }

        private bool connectbluetooth()
        {

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

            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);
            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 btnIPWeight_Click(object sender, EventArgs e)
        {
            Form frmE = new Form_SettingsIP();
            frmE.Show();
            this.Hide();
        }


    }
}