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.Common.Utils;
using ChuPiao.AutoPrint.Services.AutoPrint;
using ChuPiao.AutoPrint.Configuration;

namespace ChuPiao.AutoPrint.Desktop.Dialogs
{
    public partial class DlgMachineEdit : Form
    {
        private M_MachineEntity _machine = null;

        public DlgMachineEdit(M_MachineEntity machine)
        {
            InitializeComponent();

            _machine = machine;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.textBoxSalt.Text.Trim()))
                {
                    MessageService.ShowMessage("Salt不得为空！");
                    return;
                }

                if (string.IsNullOrEmpty(this.txtZxPassword.Text.Trim()))
                {
                    MessageService.ShowMessage("彩票机密码不得为空！");
                    return;
                }

                MachineXmlEntity machine = AutoPrintServiceManager.MachineXmlEntityDic[_machine.M_Number];
                machine.M_ZxPassword = this.txtZxPassword.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_Salt = this.textBoxSalt.Text.Trim();
                machine.M_AllowPaperout = this.chkAllowPaperout.Checked;
                machine.M_HasJc = this.checkBox_hasJc.Checked;
                machine.M_IsSimulate = this.checkBox_isSimulate.Checked;
                machine.M_IsAutoClearTotal = this.checkBox_autoClearTotal.Checked;
                machine.M_IsAutoEncash = this.checkBox_isAutoEncash.Checked;
                
                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
            {

                this.txtKeyboardBps.Text = _machine.M_VirtualKeyboardPortBps.ToString();
                this.txtKeyboardPort.Text = _machine.M_VirtualKeyboardPort.ToString();
                this.txtNumber.Text = _machine.M_Number;
                this.txtPrinterBps.Text = _machine.M_VirtualPrinterPortBps.ToString();
                this.txtPrinterPort.Text = _machine.M_VirtualPrinterPort.ToString();
                this.txtZxPassword.Text = _machine.M_ZxPassword;
                this.textBoxSalt.Text = _machine.M_Salt;
                this.chkAllowPaperout.Checked = _machine.M_AllowPaperout;
                this.checkBox_hasJc.Checked = _machine.M_HasJc;
                this.checkBox_isSimulate.Checked = _machine.M_IsSimulate;
                this.checkBox_autoClearTotal.Checked = _machine.M_IsAutoClearTotal;
                this.checkBox_isAutoEncash.Checked = _machine.M_IsAutoEncash;
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("彩票机信息对话框初始化失败。错误信息：", exception.Message));
            }
        }

        private void chkAllowPaperout_CheckedChanged(object sender, EventArgs e)
        {
            _machine.M_AllowPaperout = this.chkAllowPaperout.Checked;
        }

        private void checkBox_hasJc_CheckedChanged(object sender, EventArgs e)
        {
            _machine.M_HasJc = this.checkBox_hasJc.Checked;
        }

        private void checkBox_isSimulate_CheckedChanged(object sender, EventArgs e)
        {
            _machine.M_IsSimulate = this.checkBox_isSimulate.Checked;
        }

        private void checkBox_autoClearTotal_CheckedChanged(object sender, EventArgs e)
        {
            _machine.M_IsAutoClearTotal = this.checkBox_autoClearTotal.Checked;
        }

        private void checkBox_isAutoEncash_CheckedChanged(object sender, EventArgs e)
        {
            _machine.M_IsAutoEncash = this.checkBox_isAutoEncash.Checked;
        }
    }
}