using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;

namespace FlyDockTool
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        

            string CurrentDevice= ConfigurationManager.AppSettings["CurrentDevice"];
            apiOptions.CurrentDevice = CurrentDevice;
            apiOptions.MacAddress = ConfigurationManager.AppSettings[CurrentDevice].Split(',')[0];
            apiOptions.MachineCode = ConfigurationManager.AppSettings[CurrentDevice].Split(',')[1];
            apiOptions.MachineID = ConfigurationManager.AppSettings[CurrentDevice].Split(',')[2];
            lb_currentDeviceInfo.Text = CurrentDevice;
            btn_start_or_stop_Click(null,null);//启动数据接收

            //测试代码  测试完毕后注释调一下两行代码
            //string resultDataStr = File.ReadAllText(@"D:\工作\接受数据工具 list\尿常规U120\尿常规U120.txt", Encoding.GetEncoding("GB18030"));
            //deviceAnalysis.CreateInstance().deviceAnalysisOption(resultDataStr);


        }

        private void btn_start_or_stop_Click(object sender, EventArgs e)
        {
            if (btn_start_or_stop.Text == "启动")
            {
                //if (MessageBox.Show("确定启动数据接收?","提示",MessageBoxButtons.OKCancel)==DialogResult.OK)
                //{
                deviceAnalysis.CreateInstance().startDataReceive();
                deviceAnalysis.CreateInstance().dataOutPutShow += new EventHandler(onReData);
                btn_start_or_stop.Text = "停止";
                timer_dataShow.Enabled = true;
                //txt_msg.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+" | 设备:"+ apiOptions.MacAddress+ " 数据接收已启动...\r\n");
                DoData("设备:" + apiOptions.MacAddress + " 数据接收已启动...\r\n");
                lb_info.Text = "数据接收已启动...";
                //}
            }
            else if (btn_start_or_stop.Text == "停止")
            {
                if (MessageBox.Show("确定停止数据接收?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    deviceAnalysis.CreateInstance().stopDataReceive();
                    btn_start_or_stop.Text = "启动";
                    timer_dataShow.Enabled = false;
                    // txt_msg.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " | "+ apiOptions.MacAddress+ " 数据接收已停止...\r\n");
                    DoData("设备:" + apiOptions.MacAddress + " 数据接收已停止...\r\n");
                    lb_info.Text = "数据接收已停止...";
                }
            }

        }


        private delegate void DoDataDelegate(object txtContent);
        /// <summary>
        /// 异步赋值
        /// </summary>
        /// <param name="number"></param>
        private void DoData(object txtContent)
        {
            if (txt_msg.InvokeRequired)
            {
                DoDataDelegate d = DoData;
                txt_msg.Invoke(d, txtContent);
            }
            else
            {
                txt_msg.AppendText("\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收到数据 : " + txtContent.ToString() + "\r\n");
            }
        }

        /// <summary>
        /// 显示串口或者TCP接收到的数据
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        void onReData(object o, EventArgs e)
        {
            //txt_msg.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收到数据 : " + o.ToString()+ "\r\n");
            DoData(o);
        }

        bool isClose = false;
        private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isClose)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出该软件?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                isClose = true;
                Application.Exit();
            }
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            baseMsgModel bmm = apiOptions.CreateInstance().getMacAddressInfo();
            if (bmm.code == 200)
            {
                List<deviceInfo> diList = JsonConvert.DeserializeObject<List<deviceInfo>>(bmm.data.ToString());
               string showStr= "计算机名称(MacAddress):" + diList[0].MacAddress+"\r\n";
                showStr += "设备型号(MachineCode):" + diList[0].MachineCode + "\r\n";
                showStr += "设备编号(MachineID):" + diList[0].MachineID + "\r\n";
                MessageBox.Show(showStr);
            }
            else
                MessageBox.Show("未查到设备编号,请先在配置文件中配置MacAddress和MachineCode");

        }
    }
}