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

using SmartX;
using INT_LKD_2.DataStore;
using INT_LKD_2.Forms;

namespace INT_LKD_2.Controls
{
    public partial class ControlMenuRecipeData : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private double m_ScaleDiff;
        #endregion

        #region Constructor
        public ControlMenuRecipeData(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.Initialize();
            this.InitializeData();
            this.InitializeDrawDiff();
            this.InitializeDrawDisp();
        } 
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            set { this.m_ParentForm = value; }
        }

        public double ScaleDiff
        {
            get { return this.m_ScaleDiff; }
            set { this.m_ScaleDiff = value; }
        }
        #endregion

        #region Method
        private void Initialize()
        {
        }
        public void InitializeData()
        {
            this.labelMesResult.Text = "-";
            this.labelMesResult.TextColor = Define.ColorTextResultNone;
            this.labelMesResultDiff.Text = "Diff";
            this.labelMesResultDiff.TextColor = Define.ColorTextResultNone;
            this.labelMesResultDisp.Text = "Disp";
            this.labelMesResultDisp.TextColor = Define.ColorTextResultNone;
         
            this.labelMesPressureMaster.Text = "0.0";
            this.labelMesPressureWork.Text = "0.0";

            this.labelMesDiffMadc.Text = "0.00";
            this.labelMesDiffSecDiff.Text = "0.00";
            this.labelMesDiffSecMax.Text = "0.00";

            this.labelMesDispRData.Text = "0.00";
            this.labelMesDispMData.Text = "0.00";
            this.labelMesDispMDataDiff.Text = "0.00";
            this.labelMesDispMDataMax.Text = "0.00";
        }
        private void InitializeDrawDiff()
        {
            // SetChartConfig
            this.smartDrawDiff.Chart.SetChartConfig(10, 242, 405, 230, 2, SmartDraw.ChartRefresh.LEFTSCROLL);
            // BackLayer 기준선 표시
            this.smartDrawDiff.BackLayer.SetPenStyle(Color.Gray, 2);
            // BackLayer 세로
            this.smartDrawDiff.BackLayer.Line(10, 12, 10, 242);
            // BackLayer 가로 
            this.smartDrawDiff.BackLayer.Line(10, 242, 415, 242);
        }
        private void InitializeDrawDisp()
        {
            // SetChartConfig
            this.smartDrawDisp.Chart.SetChartConfig(10, 242, 405, 230, 2, SmartDraw.ChartRefresh.LEFTSCROLL);
            // BackLayer 기준선 표시
            this.smartDrawDisp.BackLayer.SetPenStyle(Color.Gray, 2);
            // BackLayer 세로
            this.smartDrawDisp.BackLayer.Line(10, 12, 10, 242);
            // BackLayer 가로 
            this.smartDrawDisp.BackLayer.Line(10, 242, 415, 242);
        }
        public void DrawPutDataAllClear()
        {
            this.smartDrawDiff.Chart.PutDataAllClear();
            this.smartDrawDisp.Chart.PutDataAllClear();
        }
        public void CalScaleDiff(string limit)
        { 
            int iValue = 0;

            // Lr Limit 설정값
            // double to int
            // 소수점 2자리 고정
            iValue = int.Parse(limit.Replace(".", ""));

            // 그래프 높이 230
            this.ScaleDiff = (iValue * 10) / 230;
        }
        /// <summary>
        /// 차압센서 그래프 기준선
        /// </summary>
        /// <param name="value">Lr.Limit</param>
        public void DrawDiffReferenceLine(string value)
        {
            string sValue = "";
            int iValue = 0;
            double dValue = 0.0;

            sValue = value.Replace(".", "");
            dValue = int.Parse(sValue) / this.ScaleDiff;

            if (dValue > 230)
                dValue = 230;
            if (dValue < 1)
                dValue = 3;

            iValue = int.Parse(string.Format("{0:f0}", dValue));

            this.smartDrawDiff.BackLayer.Erase();
            this.InitializeDrawDiff();
            // 가로 기준선
            //this.smartDrawDiff.BackLayer.Line(10, 242, 415, 242);
            this.smartDrawDiff.BackLayer.SetPenStyle(Color.Blue, 2);
            this.smartDrawDiff.BackLayer.Line(10, 242 - iValue, 415, 242 - iValue);
        }
        /// <summary>
        /// 변위센서 그래프 기준선
        /// </summary>
        /// <param name="value">Min Hight</param>
        public void DrawDispReferenceLine(string value)
        {
            string sValue = "";
            int iValue = 0;
            double dValue = 0.0;

            // 값 표시 범위
            // 0.00 ~ 15.00
            // 그래프 영역 높이 230
            // 1500 / 230 = 6.521739130434783

            sValue = value.Replace(".", "");
            dValue = int.Parse(sValue) / 6.521739130434783;

            if (dValue > 230)
                dValue = 230;
            if (dValue < 1)
                dValue = 3;

            iValue = int.Parse(string.Format("{0:f0}", dValue));

            this.smartDrawDisp.BackLayer.Erase();
            this.InitializeDrawDisp();
            // 가로 기준선
            //this.smartDrawDisp.BackLayer.Line(10, 242, 415, 242);
            this.smartDrawDisp.BackLayer.SetPenStyle(Color.Blue, 2);
            this.smartDrawDisp.BackLayer.Line(10, 242 - iValue, 415, 242 - iValue);
        }

        private void UpdateDisplayDotGraphDiff(DiffData diff)
        {
            string value = "";
            int iValue = 0;
            double dValue = 0.0;

            value = diff.SecBuf.Replace(".", "");
            dValue = int.Parse(value) / this.ScaleDiff;
            
            if (dValue > 230)
                dValue = 230;
            if (dValue < 1)
                dValue = 3;

            iValue = int.Parse(string.Format("{0:f0}", dValue));

            this.smartDrawDiff.Chart.PutData(iValue);
        }
        private void UpdateDisplayDotGraphDisp(DispData disp)
        {
            string value = "";
            int iValue = 0;
            double dValue = 0.0;

            // 값 표시 범위
            // 0.00 ~ 15.00
            // 그래프 영역 높이 230
            // 1500 / 230 = 6.521739130434783

            value = disp.MData.Replace(".","");
            dValue = int.Parse(value) / 6.521739130434783;

            if (dValue > 230)
                dValue = 230;
            if (dValue < 1)
                dValue = 3;

            iValue = int.Parse(string.Format("{0:f0}", dValue));

            this.smartDrawDisp.Chart.PutData(iValue);
        }

        private void GetJudgmentResult(Define.E_JudgmentStatus judg, SmartLabel label2)
        {
            string value = "";

            switch (judg)
            {
                case Define.E_JudgmentStatus.None:
                    value = "-";
                    label2.TextColor = Define.ColorTextResultNone;
                    break;
                case Define.E_JudgmentStatus.Pass:
                    value = "Pass";
                    label2.TextColor = Define.ColorTextResultPass;
                    break;
                case Define.E_JudgmentStatus.Ng:
                    value = "Leak";
                    label2.TextColor = Define.ColorTextResultNG;
                    break;
                case Define.E_JudgmentStatus.Empty:
                    value = "Empty";
                    label2.TextColor = Define.ColorTextResultNone;
                    break;
                case Define.E_JudgmentStatus.Error:
                    value = "Error";
                    label2.TextColor = Define.ColorTextResultNone;
                    break;
                default:
                    break;
            }

            if (label2.Text != value)
                label2.Text = value;
        }
        private void GetJudgmentResultDiff(Define.E_JudgmentStatus judg, SmartLabel label)
        {
            switch (judg)
            {
                case Define.E_JudgmentStatus.None:
                    label.TextColor = Define.ColorTextResultNone;
                    break;
                case Define.E_JudgmentStatus.Pass:
                    label.TextColor = Define.ColorTextResultPass;
                    break;
                case Define.E_JudgmentStatus.Ng:
                    label.TextColor = Define.ColorTextResultNG;
                    break;
                case Define.E_JudgmentStatus.Empty:
                    label.TextColor = Define.ColorTextResultNone;
                    break;
                case Define.E_JudgmentStatus.Error:
                    label.TextColor = Define.ColorTextResultNone;
                    break;
                default:
                    break;
            }
        }
        private void GetJudgmentResultDisp(Define.E_JudgmentStatus judg, SmartLabel label)
        {
            switch (judg)
            {
                case Define.E_JudgmentStatus.None:
                    label.TextColor = Define.ColorTextResultNone;
                    break;
                case Define.E_JudgmentStatus.Pass:
                    label.TextColor = Define.ColorTextResultPass;
                    break;
                case Define.E_JudgmentStatus.Ng:
                    label.TextColor = Define.ColorTextResultNG;
                    break;
                case Define.E_JudgmentStatus.Empty:
                    label.TextColor = Define.ColorTextResultNone;
                    break;
                case Define.E_JudgmentStatus.Error:
                    label.TextColor = Define.ColorTextResultNone;
                    break;
                default:
                    break;
            }
        }

        public void UpdateDisplayMeasuringLeakDataDiff(DiffData data)
        {
            string value = "";

            // 차압센서 데이터
            value = data.MAdc;
            if (this.labelMesDiffMadc.Text != value)
                this.labelMesDiffMadc.Text = value;

            value = data.SecBuf;
            if (this.labelMesDiffSecDiff.Text != value)
                this.labelMesDiffSecDiff.Text = value;

            value = data.SecBufMax;
            if (this.labelMesDiffSecMax.Text != value)
                this.labelMesDiffSecMax.Text = value;

            if (this.ParentForm.ParentForm.CurrentSystemStatus.EquipmentStatus == Define.E_EquipmentStatus.Start)
                this.UpdateDisplayDotGraphDiff(data);
        }
        public void UpdateDisplayMeasuringLeakDataDisp(DispData data)
        {
            string value = "";

            // 변위센서 데이터
            value = data.RData;
            if (this.labelMesDispRData.Text != value)
                this.labelMesDispRData.Text = value;

            value = data.MData;
            if (this.labelMesDispMData.Text != value)
                this.labelMesDispMData.Text = value;

            value = data.MDataDiff;
            if (this.labelMesDispMDataDiff.Text != value)
                this.labelMesDispMDataDiff.Text = value;

            value = data.MDataMax;
            if (this.labelMesDispMDataMax.Text != value)
                this.labelMesDispMDataMax.Text = value;

            if (this.ParentForm.ParentForm.CurrentSystemStatus.EquipmentStatus == Define.E_EquipmentStatus.Start)
                this.UpdateDisplayDotGraphDisp(data);
        }
        public void UpdateDisplayMeasuringLeakDataPres(PressureData data)
        {
            string value = "";

            // 압력 데이터
            value = data.WorkingChamber;
            if (this.labelMesPressureWork.Text != value)
                this.labelMesPressureWork.Text = value;

            value = data.MasterChamber;
            if (this.labelMesPressureMaster.Text != value)
                this.labelMesPressureMaster.Text = value;
        }
        public void UpdateDisplayMeasuringLeadDataResult(LeakData1 data)
        {
            this.GetJudgmentResult(data.Judgment.Result, this.labelMesResult);

            if (data.Judgment.Result == Define.E_JudgmentStatus.Empty)
            {
                this.GetJudgmentResultDiff(Define.E_JudgmentStatus.Empty, this.labelMesResultDiff);
                this.GetJudgmentResultDisp(Define.E_JudgmentStatus.Empty, this.labelMesResultDisp);
            }
            else
            {
                this.GetJudgmentResultDiff(data.Judgment.DIFF_Result, this.labelMesResultDiff);
                this.GetJudgmentResultDisp(data.Judgment.DISP_Result, this.labelMesResultDisp);
            }
        }
        public void UpdateDisplayDispControl(bool enable)
        {
            if (enable == false)
            {
                this.labelMesDispRData.Enabled = false;
                this.labelMesDispMData.Enabled = false;
                this.labelMesDispMDataDiff.Enabled = false;
                this.labelMesDispMDataMax.Enabled = false;

                this.smartLabel32.Enabled = false;
                this.smartLabel30.Enabled = false;
                this.smartLabel53.Enabled = false;
                this.smartLabel48.Enabled = false;
            }
            else
            {
                this.labelMesDispRData.Enabled = true;
                this.labelMesDispMData.Enabled = true;
                this.labelMesDispMDataDiff.Enabled = true;
                this.labelMesDispMDataMax.Enabled = true;

                this.smartLabel32.Enabled = true;
                this.smartLabel30.Enabled = true;
                this.smartLabel53.Enabled = true;
                this.smartLabel48.Enabled = true;
            }
        }
        #endregion
    }
}
