using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChuPiao.AutoPrint.Machine;
using ChuPiao.AutoPrint.Services;
using ChuPiao.Common.Enums;
using ChuPiao.Common.Models;
using ChuPiao.Office.Services;

namespace ChuPiao.AutoPrint.Desktop.AutoEncash
{
    public partial class EncashConfigDialog : Form
    {
        private M_MachineEntity _machine = null;
        public EncashConfigDialog(M_MachineEntity machine)
        {
            InitializeComponent();
            _machine = machine;
        }

        private void EncashConfigDialog_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "兑奖参数配置";

                M_MachineConfigTemplateService machineConfigTemplateService = new M_MachineConfigTemplateService();
                EnumDescription[] machineTypes = EnumDescription.GetFieldTexts(typeof(MachineType));
                foreach (EnumDescription machineType in machineTypes)
                {
                    if (machineType.EnumValue == _machine.M_Type)
                    {
                        M_MachineConfigEntity machineConfig = machineConfigTemplateService.GetBasicMachineConfig(machineType.FieldName);
                        this.txtBeforeEncashKeys.Text = machineConfig.C_BeforeEncashKeys;
                        this.txtAfterEncashKeys.Text = machineConfig.C_AfterEncashKeys;
                        this.txtEncashInterval.Text = machineConfig.C_EncashInterval.ToString();
                        this.txtEncashKeyInterval.Text = machineConfig.C_EncashKeyInterval.ToString();
                    }
                }
                
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("兑奖配置对话框初始化失败。错误信息：", exception.Message));
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                M_MachineConfigTemplateService machineConfigTemplateService = new M_MachineConfigTemplateService();
                EnumDescription[] machineTypes = EnumDescription.GetFieldTexts(typeof(MachineType));
                foreach (EnumDescription machineType in machineTypes)
                {
                    if (machineType.EnumValue == _machine.M_Type)
                    {
                        string beforeEncashKeys = this.txtBeforeEncashKeys.Text.Trim();
                        if(string.IsNullOrEmpty(beforeEncashKeys))
                        {
                            MessageService.ShowMessage("进入兑奖界面键码不得为空","操作提示");
                            return;
                        }
                        string afterEncashKeys = this.txtAfterEncashKeys.Text.Trim();
                        if(string.IsNullOrEmpty(afterEncashKeys))
                        {
                            MessageService.ShowMessage("兑奖确认键码不得为空","操作提示");
                            return;
                        }
                        if (string.IsNullOrEmpty(this.txtEncashKeyInterval.Text))
                        {
                            MessageService.ShowMessage("兑奖键码间隔不得为空", "操作提示");
                            return;
                        }
                        if (string.IsNullOrEmpty(this.txtEncashInterval.Text))
                        {
                            MessageService.ShowMessage("两张票的间隔不得为空", "操作提示");
                            return;
                        }
                        int encashKeyInterval = 300;
                        int encashInterval = 5000;
                        bool result = false;
                        result = int.TryParse(this.txtEncashKeyInterval.Text, out encashKeyInterval);
                        if(!result)
                        {
                            MessageService.ShowMessage("兑奖键码间隔必须为整数", "操作提示");
                            return;
                        }
                        result = int.TryParse(this.txtEncashInterval.Text, out encashInterval);
                        if (!result)
                        {
                            MessageService.ShowMessage("两张票的间隔必须为整数", "操作提示");
                            return;
                        }
                        
                        _machine.MachineConfigEntity.C_BeforeEncashKeys = beforeEncashKeys;
                        _machine.MachineConfigEntity.C_AfterEncashKeys = afterEncashKeys;
                        _machine.MachineConfigEntity.C_EncashKeyInterval = encashKeyInterval;
                        _machine.MachineConfigEntity.C_EncashInterval = encashInterval;

                        result= machineConfigTemplateService.SaveBasicMachineConfig(machineType.FieldName,
                                                                            _machine.MachineConfigEntity);
                        if(result)
                        {
                            ReloadEncashConfig();
                            MessageService.ShowMessage("兑奖配置保存成功", "操作提示");
                        }
                    }
                }
                this.DialogResult = DialogResult.OK;

            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("兑奖配置保存失败。错误信息：", exception.Message));
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void ReloadEncashConfig()
        {
            try
            {
                M_MachineConfigTemplateService machineConfigTemplateService = new M_MachineConfigTemplateService();
                EnumDescription[] machineTypes = EnumDescription.GetFieldTexts(typeof(MachineType));

                foreach (ILotMachine lotMachine in LotMachineService.LotMachineList.Values)
                {
                    foreach (EnumDescription machineType in machineTypes)
                    {
                        if (machineType.EnumValue == lotMachine.MachineInfo.M_Type)
                        {
                            M_MachineConfigEntity machineConfig =
                                machineConfigTemplateService.GetBasicMachineConfig(machineType.FieldName);
                            //List<M_MachineModeConfigEntity> machineModeConfigs =
                            //    machineConfigTemplateService.GetBasicMachineModeConfigs(machineType.FieldName);

                            lotMachine.MachineInfo.MachineConfigEntity = machineConfig;
                            LoggingService.Info(string.Concat(lotMachine.MachineInfo.M_Number, "重新加载兑奖配置完成"));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                LoggingService.Warn(string.Concat("重新加载兑奖配置异常", exception.Message));
            }
        }
    }
}