using System;
using System.Windows.Forms;
using ChuPiao.AutoPrint.Services;
using ChuPiao.Common.Enums;
using ChuPiao.Common.Models;
using ChuPiao.Common.Utils;

namespace ChuPiao.AutoPrint.Desktop.Dialogs
{
    public partial class DlgComputerEdit : Form
    {
        private M_ComputerEntity computer;
        public DlgComputerEdit(M_ComputerEntity mycomputer)
        {
            InitializeComponent();
            computer = mycomputer;
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            try
            {
                
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("保存计算机信息异常。错误信息：",exception.Message));
                this.DialogResult = DialogResult.Cancel;
            }
        }

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

        private void DlgComputer_Load(object sender, System.EventArgs e)
        {
            try
            {
               

              

               

                //读取 电脑是否接了打印机 的设置
                bool result = true;
                string strtmp = LocalConfigUtil.GetConfig("hasComputerPrinter");
                if (!string.IsNullOrEmpty(strtmp))
                {
                    bool.TryParse(strtmp, out result);
                }
                chkHasComputerPrinter.CheckedChanged-=new EventHandler(chkHasComputerPrinter_CheckedChanged);
                chkHasComputerPrinter.Checked = result;
                chkHasComputerPrinter.CheckedChanged += new EventHandler(chkHasComputerPrinter_CheckedChanged);
                LoggingService.Info(string.Format("出票电脑是否接了打印机：{0}", result ? "是" : "否"));



                checkBox1.CheckedChanged -= new EventHandler(checkBox1_CheckedChanged);
                checkBox1.Checked = BeepUtil.notUseBeep;
                checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);
                LoggingService.Info(string.Format("出票电脑蜂鸣器不发声：{0}", result ? "是" : "否"));

                textBox1.Text = BeepUtil.fileToPlay;

            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("控制电脑注册信息对话框初始化失败。错误信息：",exception.Message));
            }
        }

        private void cboPrinterPortType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    EnumDescription portTypeDesc = this.cboPrinterPortType.SelectedItem as EnumDescription;
            //    if(portTypeDesc!=null)
            //    {
            //        PrinterPortType portType = (PrinterPortType) portTypeDesc.EnumValue;
            //        switch (portType)
            //        {
            //            case PrinterPortType.ParallelPort:
            //                this.txtPrinterPortBps.Text = "19200";
            //                this.txtPrinterPort.Text = "lpt1";
            //                break;
            //            case PrinterPortType.ComPort:
            //                string address = System.Configuration.ConfigurationManager.AppSettings.Get("Address");
            //                if (address.Trim() == "天津竞彩")
            //                {
            //                    this.txtPrinterPortBps.Text = "19200";
            //                }
            //                else
            //                {
            //                    this.txtPrinterPortBps.Text = "115200";
            //                }
                            
            //                this.txtPrinterPort.Text = "com1";
            //                break;
            //            case PrinterPortType.UsbPort:
            //                this.txtPrinterPortBps.Text = "0";
            //                this.txtPrinterPort.Text = "";
            //                break;
            //        }
            //    }
            //}
            //catch (Exception exception)
            //{
            //    LoggingService.Info(string.Concat("设置端口默认波特率失败。错误信息：", exception.Message));
            //}
        }

        private void chkHasComputerPrinter_CheckedChanged(object sender, EventArgs e)
        {
            LocalConfigUtil.SaveConfig("hasComputerPrinter", chkHasComputerPrinter.Checked.ToString());
           

            LoggingService.Info(string.Format("把【出票电脑是否接了打印机】设置成了：{0}", chkHasComputerPrinter.Checked ? "是" : "否"));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            LocalConfigUtil.SaveConfig("notUseBeep", checkBox1.Checked.ToString());
            BeepUtil.notUseBeep = checkBox1.Checked;
            LoggingService.Info(string.Format("把【蜂鸣器不发声】设置成了：{0}", checkBox1.Checked ? "是" : "否"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                LocalConfigUtil.SaveConfig("fileToPlay", openFileDialog1.FileName);
                BeepUtil.fileToPlay = openFileDialog1.FileName;
                LoggingService.Info(string.Format("把【蜂鸣器不发声】设置成了：{0}", checkBox1.Checked ? "是" : "否"));
            }


        }

    }
}