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

namespace FPER
{
    public partial class frmVersion_BigReceiver : Form
    {
        public frmVersion_BigReceiver()
        {
            InitializeComponent();

            // cyim 2013.7.12 디자인개선작업 : 시스템정보 - 버전정보 : 기본결과
            this.button_Menu_BasicSet.BackgroundImage = (Image)Properties.Resources.탭메뉴버튼_주황_100_30;
        }

        MDIParent mdi = null;
        delegate void deleFormInit();
        //int glb_boardNo = 1;

        private void btnWinClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }

        public void Form_Init()
        {
            try
            {

            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }

        private void frmVersion_Load(object sender, EventArgs e)
        {
            // cyim 2013.8.1 : 더블버퍼링 적용
            SetStyle(ControlStyles.UserPaint, true);
            //this.UpdateStyles();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //this.UpdateStyles();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();

            try
            {
                mdi = (MDIParent)this.MdiParent;
                txtThisReceiverID.Text = string.Format("{0:00}", mdi.myReceiverID);
                //this.mdi.SelectMenuIndex = 7;

                txtUIVersion.Text = Application.ProductVersion;
                txtDemonVersion.Text = "";
                txtFileVersion.Text = "0.0.0.0";

                Grid_init();

                this.Cursor = Cursors.WaitCursor;

                //완료이벤트
                dCommandResponse d = new dCommandResponse(VersionInfoComplete);

                //명령생성 및 실행
                CmdInfo cmd = new CmdInfo(prt_cmd_define.read_command_status, mdi.myReceiverID, 1, 0, 0, d);
                cmd.CommandType = "VI";
                cmd.ApplyRange = "A";   //차단레벨(회로타입차단용) A-All,C-Comm,B-Board,L-Loop,R-중계기,D-회로,I-입력회로,O-출력회로
                cmd.CommandData = null;
                mdi.ui.runCommand(cmd);
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }

        private void frmVersion_Activated(object sender, EventArgs e)
        {
            try
            {
                //this.mdi.SelectMenuIndex = 7;
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }
        private void frmVersion_Disposed(object sender, EventArgs e)
        {
            //if (mdi.ui.CommResponseReading)
            //{
            //    //MessageBox.Show("응답 대기중입니다.. 잠시만 기다려 주십시요..");
            //    return;
            //}
            try
            {
                //this.mdi.SelectMenuIndex = 0;
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }

        //*********************************************************************//
        //        버젼정보 완료 응답데이터 이벤트                          //
        //*********************************************************************//
        public void VersionInfoComplete(CmdInfo cmd)
        {
            try
            {
                this.Cursor = Cursors.Default;

                //에러응답처리
                if (!Util.ComInfoErrProcess(cmd, mdi.ui.Connected, "RCVData_ReadCommandStatus")) // cyim 2015.7.23 NACK 처리 부분 오류 
                {
                    return;
                }

                RCVData_ReadCommandStatus resData2 = (RCVData_ReadCommandStatus)cmd.ResponseData;
                string demonVer = string.Format("{0}.{1}.{2}.{3}", resData2.Reply_value1, resData2.Reply_value2, resData2.Reply_value3, resData2.Reply_value4);
                txtDemonVersion.Text = demonVer;
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }

        //*********************************************************************//
        //        버젼정보 완료 응답데이터 이벤트                          //
        //*********************************************************************//
        private void BrdVersionInfoComplete(CmdInfo cmd)
        {
            try
            {
                this.Cursor = Cursors.Default;

                //에러응답처리
                if (!Util.ComInfoErrProcess(cmd, mdi.ui.Connected, "RCVData_ReadTBoardStatus")) // cyim 2015.7.23 NACK 처리 부분 오류 
                {
                    button_end(true);
                    return;
                }

                if (cmd != null)
                {
                    //TextBox txt = (TextBox)Util.FineControl(tabPage2, string.Format("txtBoardVer_{0}", glb_boardNo));
                    //if (txt != null)
                    //{
                    //    RCVData_ReadTBoardStatus res = (RCVData_ReadTBoardStatus)cmd.ResponseData;
                    //    txt.Text = string.Format("{0}.{1}", res.TBD_MainVersion, res.TBD_SubVersion);
                    //}

                    // cyim 2016.10.31 : 대용량 수신기용 UI 변경
                    RCVData_ReadTBoardStatus res = (RCVData_ReadTBoardStatus)cmd.ResponseData;
                    Label txt = (Label)Util.FineControl(tabPage2, string.Format("lbl_COMM{0}", res.TBD_ID));
                    if (txt != null)
                    {
                        if (!txt.Text.Contains(":"))
                        {
                            txt.Text = txt.Text + " : " + string.Format("{0}.{1}", res.TBD_MainVersion, res.TBD_SubVersion);
                        }
                    }
                }
                if (cmd == null)
                {
                    if (mdi.ui == null) return;
                    ReceiverTypeInfo receiverInfo = mdi.ui.MyReceiverTypeInfo;
                    if (receiverInfo == null) return;

                    BoardTypeInfo[] boardInfos = receiverInfo.MyBoardType; //전체통신보드
                    if (boardInfos != null)
                    {
                        foreach (BoardTypeInfo brd in boardInfos)
                        {
                            if (brd != null)
                            {
                                if (brd.UseFlag)
                                {
                                    try
                                    {
                                        if (brd.BoardNo <= 64)
                                        {
                                            CmdInfo cmd2 = new CmdInfo(prt_cmd_define.resd_tbd_state, mdi.myReceiverID);
                                            cmd2.boardNo = brd.BoardNo;
                                            dCommandResponse d = new dCommandResponse(BrdVersionInfoComplete);  //응답후 이벤트 읽기 명령으로 이동
                                            cmd2.dEvent = d;
                                            this.mdi.ui.runCommand(cmd2);
                                        }
                                        else
                                        {
                                            button_end(true);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Util.UErrorMessage(ex, 0, 0);
                                        button_end(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
                button_end(true);
                return;
            }

            //try
            //{
            //    // cyim 2016.10.31 : 대용량 수신기용 UI 변경
            //    if (glb_boardNo <= 64)
            //    {
            //        CmdInfo cmd2 = new CmdInfo(prt_cmd_define.resd_tbd_state, mdi.myReceiverID);
            //        cmd2.boardNo = glb_boardNo;
            //        dCommandResponse d = new dCommandResponse(BrdVersionInfoComplete);  //응답후 이벤트 읽기 명령으로 이동
            //        cmd2.dEvent = d;
            //        this.mdi.ui.runCommand(cmd2);
            //    }
            //    else
            //    {
            //        button_end(true);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Util.UErrorMessage(ex, 0, 0);
            //    button_end(true);
            //}
        }

        private void Grid_init()
        {
            try
            {

                if (mdi.ui == null) return;
                ReceiverTypeInfo receiverInfo = mdi.ui.MyReceiverTypeInfo;
                if (receiverInfo == null) return;

                /************************************************************************/
                /*   통신보드 화면 초기화                             */
                /************************************************************************/
                BoardTypeInfo[] boardInfos = receiverInfo.MyBoardType; //전체통신보드
                if (boardInfos != null)
                {

                    foreach (BoardTypeInfo brd in boardInfos)
                    {
                        if (brd != null)
                        {
                            if (brd.UseFlag)
                            {
                                //Label lbl = (Label)Util.FineControl(tabPage2, string.Format("lblBoardID_{0}", brd.BoardNo));
                                //if (lbl != null)
                                //{
                                //    lbl.Enabled = true;
                                //}
                                //TextBox txt = (TextBox)Util.FineControl(tabPage2, string.Format("txtBoardVer_{0}", brd.BoardNo));

                                // cyim 2016.10.31 : 대용량 수신기용 UI 변경
                                Label txt = (Label)Util.FineControl(tabPage2, string.Format("lbl_COMM{0}", brd.BoardNo));//glb_boardNo
                                if (txt != null)
                                {
                                    txt.Enabled = true;
                                }
                            }
                        }
                    }
                }
                /************************************************************************/
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
                throw ex;
            }
        }

        private void btnCommRead_Click(object sender, EventArgs e)
        {
            try
            {
                //버튼 비활성 설정
                button_end(false);
                //glb_boardNo = 1;

                //통신보드 버젼읽기 명령 시작
                BrdVersionInfoComplete(null);
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
                button_end(true);
            }
        }

        //화면 마우스 커서 및 버튼 다시 클릭 못하도록 설정 및 해제
        private void button_end(Boolean val)
        {
            try
            {
                if (val)
                {
                    this.btnCommRead.Enabled = true;
                    this.Cursor = Cursors.Default;
                }
                else
                {
                    this.btnCommRead.Enabled = false;
                    this.Cursor = Cursors.WaitCursor;
                }
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }

        }
        bool CheckBoardVersion = false;//2010.10.29,k.s.d, check flag.
        private void tabPage2_Enter(object sender, EventArgs e)
        {//2010.10.29,k.s.d, if tab enter , read board version.
            try
            {
                if (CheckBoardVersion == false)
                {
                    //glb_boardNo = 1;
                    BrdVersionInfoComplete(null);
                }
                CheckBoardVersion = true;
            }
            catch (Exception ex)
            {
                Util.UErrorMessage(ex, 0, 0);
            }
        }

        //
        // cyim 2013.7.12 디자인개선작업 : 시스템정보 - 버전정보
        //

        private void button_Menu_BasicSet_Click(object sender, EventArgs e)
        {
            button_Menu_Color_Init();
            this.tabControl1.SelectTab(0);
            this.button_Menu_BasicSet.BackgroundImage = (Image)Properties.Resources.탭메뉴버튼_주황_100_30;
            this.panel_Menu.Focus();
        }

        private void button_Menu_CommBoard_Click(object sender, EventArgs e)
        {
            button_Menu_Color_Init();
            this.tabControl1.SelectTab(1);
            this.button_Menu_CommBoard.BackgroundImage = (Image)Properties.Resources.탭메뉴버튼_주황_100_30;
            this.panel_Menu.Focus();
        }

        private void button_Menu_Color_Init()
        {
            this.button_Menu_BasicSet.BackgroundImage = (Image)Properties.Resources.탭메뉴버튼_회색_100_30;
            this.button_Menu_CommBoard.BackgroundImage = (Image)Properties.Resources.탭메뉴버튼_회색_100_30;
        }
    }
}