using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ChuPiao.AutoPrint.Common;
using ChuPiao.AutoPrint.Machine;
using ChuPiao.AutoPrint.Services;
using ChuPiao.AutoPrint.Services.AutoEncash;
using ChuPiao.Common.Enums;
using ChuPiao.Common.Models;
using ChuPiao.AutoPrint.Desktop.ZxEncashTicket;

namespace ChuPiao.AutoPrint.Desktop.AutoEncash
{
    public class AutoEncashTabPage:TabPage
    {
        AutoEncashToolStrip autoEncashToolStrip;
        public AutoEncashTabPage()
        {
            this.Text = "打印兑奖票";
            this.Dock = DockStyle.Fill;

            ToolStripContainer toolStripContainer = new ToolStripContainer();
            toolStripContainer.Dock = DockStyle.Fill;

            autoEncashToolStrip = new AutoEncashToolStrip();
            //autoEncashToolStrip.toolStripComboBoxLotType.SelectedIndexChanged += new EventHandler(toolStripComboBoxLotType_SelectedIndexChanged);
            
            autoEncashToolStrip.toolStripButtonSelect.Click += new EventHandler(toolStripButtonSelect_Click);
            autoEncashToolStrip.toolStripButtonLoad.Click += new EventHandler(toolStripButtonLoad_Click);
            autoEncashToolStrip.toolStripButtonLoadBig.Click += new EventHandler(toolStripButtonLoadBig_Click);
            autoEncashToolStrip.toolStripButtonStart.Click += new EventHandler(toolStripButtonStart_Click);
            autoEncashToolStrip.toolStripButtonStop.Click += new EventHandler(toolStripButtonStop_Click);
            //autoEncashToolStrip.toolStripButtonEncashConfig.Click += new EventHandler(toolStripButtonEncashConfig_Click);
            toolStripContainer.TopToolStripPanel.Controls.Add(autoEncashToolStrip);

            TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
            tableLayoutPanel.Dock = DockStyle.Fill;
            tableLayoutPanel.AutoScroll = true;
            tableLayoutPanel.AutoSize = true;
            tableLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            tableLayoutPanel.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.OutsetDouble;
            tableLayoutPanel.ColumnCount = 2;
            tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            //tableLayoutPanel.RowCount = 2;
            //tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            //tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));

            foreach (KeyValuePair<string, ILotMachine> keyValuePair in LotMachineService.LotMachineList)
            {
                AutoEncashService service = new AutoEncashService();
                service.LotMachine = keyValuePair.Value;
                service.StatusChanged += new EventHandler<MachineStatusArgs>(service_StatusChanged);

                service.EncashTicketLoaded += new EventHandler(service_EncashTicketLoaded);
                AutoEncashView view = new AutoEncashView();
                view.Service = service;
                view.CheckedChanged += new EventHandler(view_CheckedChanged);


                view.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
                tableLayoutPanel.Controls.Add(view);

                LotMachineViewList.Add(view);
            }

            toolStripContainer.ContentPanel.Controls.Add(tableLayoutPanel);

            Controls.Add(toolStripContainer);
            if (this.autoEncashToolStrip.toolStripComboBoxLotType.Items.Count>0)
            {
                this.autoEncashToolStrip.toolStripComboBoxLotType.SelectedIndex = 0;
            }
        }

        

        //void toolStripButtonEncashConfig_Click(object sender, EventArgs e)
        //{
        //    try
        //    {

        //    }
        //    catch (Exception exception)
        //    {
        //        MessageService.ShowMessage(string.Concat("兑奖配置异常。错误信息：", exception.Message));
        //    }
        //}

        //void toolStripComboBoxLotType_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    //加载彩种期号列表
        //    try
        //    {
        //        M_LotTypeEntity lotTypeEntity = autoEncashToolStrip.toolStripComboBoxLotType.SelectedItem as M_LotTypeEntity;
        //        if (lotTypeEntity != null)
        //        {
        //            if(lotTypeEntity.L_LotId==(int)Lottype.Jz || lotTypeEntity.L_LotId==(int)Lottype.Jl)
        //            {
        //                autoEncashToolStrip.toolStripLabelAbbdate.Visible = true;
        //                autoEncashToolStrip.toolStripTextBoxAbbdate.Visible = true;
        //                autoEncashToolStrip.toolStripLabelExpect.Visible = false;
        //                autoEncashToolStrip.toolStripComboBoxExpect.Visible = false;
        //            }
        //            else
        //            {
        //                autoEncashToolStrip.toolStripLabelAbbdate.Visible = false;
        //                autoEncashToolStrip.toolStripTextBoxAbbdate.Visible = false;
        //                autoEncashToolStrip.toolStripLabelExpect.Visible = true;
        //                autoEncashToolStrip.toolStripComboBoxExpect.Visible = true;
        //                L_ExpectService expectService = new L_ExpectService();
        //                List<L_ExpectEntity> list = expectService.GetNewlyExpectList(lotTypeEntity.L_LotId);
        //                if (list != null && list.Count > 0)
        //                {
        //                    this.autoEncashToolStrip.toolStripComboBoxExpect.Items.Clear();
        //                    int selectedIndex = 0;
        //                    int i = 0;
        //                    foreach (L_ExpectEntity entity in list)
        //                    {
        //                        if (entity.E_Active == 1)
        //                        {
        //                            selectedIndex = i;
        //                        }
        //                        this.autoEncashToolStrip.toolStripComboBoxExpect.Items.Add(entity);
        //                        i++;
        //                    }
        //                    this.autoEncashToolStrip.toolStripComboBoxExpect.SelectedIndex = selectedIndex;
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception exception)
        //    {
        //        MessageService.ShowError(string.Concat("加载自动兑奖期号列表异常。错误信息：",exception.Message));
        //    }
        //}
        void view_CheckedChanged(object sender, EventArgs e)
        {
            UpdateToolBarStatus();
        }
        void service_StatusChanged(object sender, MachineStatusArgs e)
        {
            WorkbenchSingleton.SafeThreadCall(new Action(UpdateToolBarStatus));
        }
        void service_EncashTicketLoaded(object sender, EventArgs e)
        {
            WorkbenchSingleton.SafeThreadCall(new Action(UpdateToolBar_LoadButton));
        }

        void toolStripButtonStop_Click(object sender, EventArgs e)
        {
            int selectedMachineNum = 0;
            foreach (AutoEncashView lotMachineView in LotMachineViewList)
            {
                if (lotMachineView.IsSelected)
                {
                    selectedMachineNum++;
                    lotMachineView.Stop();
                }
            }
            if (selectedMachineNum == 0)
            {
                MessageService.ShowMessage("请选择机器号", "操作提示");
            }
        }

        void toolStripButtonStart_Click(object sender, EventArgs e)
        {
            //EncashOption encashOption = EncashOption.FirstEncash;
            //EnumDescription encashOptionDesc=this.autoEncashToolStrip.toolStripComboBoxOption.SelectedItem as EnumDescription;
            //if(encashOptionDesc!=null)
            //{
            //    encashOption = (EncashOption) encashOptionDesc.EnumValue;
            //}
            int selectedMachineNum = 0;
            foreach (AutoEncashView lotMachineView in LotMachineViewList)
            {
                if (lotMachineView.IsSelected)
                {
                    //lotMachineView.Service.EncashOption = (int)encashOption;
                    selectedMachineNum++;
                    lotMachineView.Start();
                }
            }
            if (selectedMachineNum == 0)
            {
                MessageService.ShowMessage("请选择机器号", "操作提示");
            }
        }

        void toolStripButtonLoadBig_Click(object sender, EventArgs e)
        {
            ////用户验证
            //if (DialogResult.OK != new UserLogin(true).ShowDialog())
            //{
            //    return;
            //}

            load(true);
        }


        void toolStripButtonLoad_Click(object sender, EventArgs e)
        {
            load(false);            
        }

        private void load(bool isbig)
        {
            try
            {
                this.autoEncashToolStrip.toolStripButtonLoad.Enabled = false;
                M_LotTypeEntity lotTypeEntity =
                this.autoEncashToolStrip.toolStripComboBoxLotType.SelectedItem as M_LotTypeEntity;
                //EnumDescription encashLoadTypeDesc = this.autoEncashToolStrip.toolStripComboBoxType.SelectedItem as EnumDescription;

                if (lotTypeEntity != null)
                {
                    int selectedMachineNum = 0;
                    //if (lotTypeEntity.L_LotId == (int)Lottype.Jz || lotTypeEntity.L_LotId == (int)Lottype.Jl || lotTypeEntity.L_LotId == (int)Lottype.Jm)
                    //{

                    string abbdate = autoEncashToolStrip.toolStripTextBoxAbbdate.Text.Trim();
                    foreach (AutoEncashView lotMachineView in LotMachineViewList)
                    {
                        if (lotMachineView.IsSelected)
                        {
                            selectedMachineNum++;

                            lotMachineView.Service.LoadEncashTicketsByAbbdate(lotTypeEntity, abbdate, isbig);
                            //lotMachineView.Service.LoadEncashTicketsByAbbdate(lotTypeEntity, abbdate, (EncashLoadType)encashLoadTypeDesc.EnumValue);//EncashLoadType.ByMacNumber
                        }
                    }
                    //}
                    //else
                    //{
                    //    L_ExpectEntity expectEntityFrom = this.autoEncashToolStrip.toolStripComboBoxExpectFrom.SelectedItem as L_ExpectEntity;
                    //    L_ExpectEntity expectEntityTo = this.autoEncashToolStrip.toolStripComboBoxExpectTo.SelectedItem as L_ExpectEntity;

                    //    if (expectEntityFrom.E_Expect.CompareTo(expectEntityTo.E_Expect) > 0)
                    //    {
                    //        MessageService.ShowMessage("期号查询条件前者不能大于后者！", "操作提示");
                    //        this.autoEncashToolStrip.toolStripButtonLoad.Enabled = true;
                    //        return;
                    //    }

                    //    foreach (AutoEncashView lotMachineView in LotMachineViewList)
                    //    {
                    //        if (lotMachineView.IsSelected)
                    //        {
                    //            selectedMachineNum++;

                    //            //lotMachineView.Service.LoadEncashTickets(lotTypeEntity, expectEntityFrom,expectEntityTo, (EncashLoadType)encashLoadTypeDesc.EnumValue);//EncashLoadType.ByMacNumber

                    //        }
                    //    }
                    //}
                    if (selectedMachineNum == 0)
                    {
                        MessageService.ShowMessage("请选择机器号", "操作提示");
                    }

                }
            }
            catch (Exception ex)
            {
                this.autoEncashToolStrip.toolStripButtonLoad.Enabled = true;
                MessageService.ShowError(string.Concat("加载票异常。错误信息：", ex));
            }
        }

        void toolStripButtonSelect_Click(object sender, EventArgs e)
        {
            foreach (AutoEncashView lotMachineView in LotMachineViewList)
            {
                lotMachineView.DeSelect();
            }
        }

        public List<AutoEncashView> LotMachineViewList = new List<AutoEncashView>();

        private void UpdateToolBarStatus()
        {
            bool hasSelected = false;
            bool isEnabledStart = false;
            bool isEnabledStop = false;
            foreach (AutoEncashView lotMachineView in LotMachineViewList)
            {
                if (lotMachineView.IsSelected)
                {
                    hasSelected = true;
                    switch ((MachineStatusEnum)lotMachineView.Service.LotMachine.MachineInfo.M_Status)
                    {
                        case MachineStatusEnum.Stopping:
                            isEnabledStart = false;
                            isEnabledStop = false;
                            break;
                        case MachineStatusEnum.Stopped:
                            isEnabledStart = true;
                            break;
                        case MachineStatusEnum.Encashing:
                            isEnabledStop = true;
                            break;
                    }
                }
            }
            if (!hasSelected)
            {
                autoEncashToolStrip.toolStripButtonStart.Enabled = false;
                autoEncashToolStrip.toolStripButtonStop.Enabled = false;
                autoEncashToolStrip.toolStripButtonLoad.Enabled = false;
            }
            else
            {
                autoEncashToolStrip.toolStripButtonLoad.Enabled = true;

                autoEncashToolStrip.toolStripButtonStart.Enabled = isEnabledStart;
                autoEncashToolStrip.toolStripButtonStop.Enabled = isEnabledStop;
            }
        }
        private void UpdateToolBar_LoadButton()
        {
            this.autoEncashToolStrip.toolStripButtonLoad.Enabled = true;
        }
    }
}
