using MesControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SystemFramework;

namespace MisDataName
{
    public partial class SecondForm : Form
    {
      

        public SecondForm(Point Position)
        {
            InitializeComponent();
            InitFormDynamicControls();
            InitMainSignalEvent();
            FormPositionInit(Position);
            InitOtherFormAction();
            SyncForm_Init();
            
        }
        Form MainForm;
        public SecondForm(object  mainForm)
        {
            MainForm = (Form)mainForm;
            InitializeComponent();
            InitFormDynamicControls();
            InitMainSignalEvent();
            Point Position = new Point(MainForm.Left + MainForm.Width, MainForm.Top);

            FormPositionInit(Position);
            InitOtherFormAction();
            SyncForm_Init();

        }

        void WorkStation_RefreshWorkStationStatus(string OpName,  bool value)
        {
            try
            {
                PlcWorkStationSignal plcWorkStationSignal = (PlcWorkStationSignal)Controls.Find("plcWorkStationSignal_" + OpName, true)[0];
                plcWorkStationSignal.PlcConnectStatusChange(value);
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }
        }
        
        
        void WriteWorkStationSignal(string OpName,string SignalName,bool value)
        {
            try
            { 
                if (Array.IndexOf(SignalNames, "TagType_"+SignalName)!=-1)
                {
                    PlcWorkStationSignal plcWorkStationSignal = (PlcWorkStationSignal)Controls.Find("plcWorkStationSignal_" + OpName, true)[0];
                    plcWorkStationSignal.SignalChange("TagType_"+SignalName, value);
                    if (SignalName=="14")
                    {
                        WorkStation_RefreshWorkStationStatus(OpName, value);
                    }

                }
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }

        }
        

        private void SecondForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            { 
                MisData.PlcChange =null;
                if (MisData.SyncSecondForm != null)
                {
                    SyncForm_Cancel();
                }
                CancelOtherFormAction();
                MainForm.BeginInvoke(MainMenuRefresh, "ToolStripMenuItem_VWSStatus");
                this.Dispose();
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }
        }
        
        
        void SyncForm_Init()
        {
            try
            { 
            MisData.SyncSecondForm = new SyncOtherForm(FormSync);
            MisData.MainFormActive = new MisDataName.SyncOtherForm(MainFormActive);
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }
        }
        
        void SyncForm_Cancel()
        {
            MisData.SyncSecondForm = null;
            MisData.MainFormActive = null;
        }

        void FormPositionInit(Point  Position)
        {
            try
            { 
                this.Location = (Point)new Size(Position);
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }

        }

        void FormSync(object  Position)
        {
            try
            { 
                this.Location = (Point)Position;
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }
        }

        void MainFormActive(object obj)
        {
         
            if(Convert .ToBoolean (obj ))
            {
                this.TopMost = true;
                this.TopMost = false;
            }
         
        }
        
        void InitOtherFormAction()
        {
            try
            { 
                MisData.SecondFormClose = new MainAct(this.Close);
                MisData.OtherFormHide = new MainAct(HideThisForm);
                MisData.OtherFormShow = new MainAct(ShowThisForm);
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }
        }

        void CancelOtherFormAction()
        {
            MisData.SecondFormClose = null;
            MisData.OtherFormHide = null;
            MisData.OtherFormShow = null;

        }

        void ShowThisForm()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        void HideThisForm()
        {
            this.Hide();
        }
        


        private void SecondForm_LocationChanged(object sender, EventArgs e)
        {
            try
            { 
                Point MainPosation=(Point)MisData.QueryMainFormPosition(null);

                if(Math.Abs ( MainPosation .X -this .Left)<25&& Math.Abs(MainPosation.Y  - this.Top ) < 20)
                {
                    if (MisData.SyncSecondForm == null)
                    {
                        SyncForm_Init();
                        MainForm.Invoke(MisData.MainActived);
                        FormSync(MainPosation);
                   
                    }
                }
                else
                {
                    if (MisData.SyncSecondForm != null)
                    {
                        SyncForm_Cancel();
                    }
                }
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }


        }

        private void timer_ControlInternalChange_Tick(object sender, EventArgs e)
        {
            if(PlcConnectTimer!=null)
            {
                PlcConnectTimer();
            }
            
        }
    }
}
