using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ChuPiao.AutoPrint.Services;
using ChuPiao.Common.Enums;
using ChuPiao.Common.Models;
using ChuPiao.Office.Services;
using ChuPiao.AutoPrint.Configuration;
using ChuPiao.AutoPrint.Services.AutoPrint;

namespace ChuPiao.AutoPrint.Desktop.Dialogs
{
    public partial class DlgMachine : Form
    {
        public DlgMachine()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {


                MachineXmlEntity machine = new MachineXmlEntity();
                machine.M_IsAutoClearTotal = false;
                machine.M_IsAutoEncash = false;
                machine.M_IsSimulate = false;
                machine.M_HasJc = true;
                machine.M_AllowPaperout = false;
                machine.M_Number = this.txtNumber.Text.Trim();
                machine.M_VirtualKeyboardPort = this.txtKeyboardPort.Text.Trim();
                machine.M_VirtualKeyboardPortBps = int.Parse(this.txtKeyboardBps.Text.Trim());
                machine.M_VirtualPrinterPort = this.txtPrinterPort.Text.Trim();
                machine.M_VirtualPrinterPortBps = int.Parse(this.txtPrinterBps.Text.Trim());
                machine.M_ZxPassword = this.txtZxPassword.Text.Trim();
                machine.M_Salt = this.textBoxSalt.Text.Trim();

                //输入验证
                if(string.IsNullOrEmpty(machine.M_Number))
                {
                    MessageService.ShowMessage("站点编号不得为空！");
                    return;
                }
                if (string.IsNullOrEmpty(machine.M_Salt))
                {
                    MessageService.ShowMessage("Salt不得为空！");
                    return;
                }
                if (string.IsNullOrEmpty(machine.M_ZxPassword))
                {
                    MessageService.ShowMessage("彩票机密码不得为空！");
                    return;
                }

                if (AutoPrintServiceManager.MachineXmlEntityDic.ContainsKey(machine.M_Number))
                {
                    MessageService.ShowMessage("站点编号已存在！");
                    return;
                }

                AutoPrintServiceManager.MachineXmlEntityDic.Add(machine.M_Number,machine);
                MachineXmlConfig.SaveMachineList(new List<MachineXmlEntity>(AutoPrintServiceManager.MachineXmlEntityDic.Values));

                MessageService.ShowMessage("添加彩票机信息成功，重启软件后生效。");
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("添加彩票机信息异常。错误信息：", exception.Message));
                //this.DialogResult = DialogResult.Cancel;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void DlgMachine_Load(object sender, EventArgs e)
        {
            try
            {
                
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("彩票机注册对话框初始化失败。错误信息：", exception.Message));
            }
        }

    }
}