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

namespace vmsnet.HMI
{
    [TypeConverterAttribute(typeof(ExpandableObjectConverter)), Serializable()]
    public partial class CITEM
    {
        public int column;
        public int row;

        public RectangleF rect;
        public RectangleF BarRect;
        private String name;

        public int seq;

        /// <summary>
        /// 0부터시작
        /// </summary>
        public int idx_dev;      //장치의 인덱스값

        /// <summary>
        /// 0부터시작
        /// </summary>
        public int idx_unit;     //서브유닛의 인덱스값

        /// <summary>
        /// 0부터시작
        /// </summary>
        public int idx_ch;       //채널의 인덱스값
        public int idx;          //셀의고유인덱스값(DB는 이값이 저장되고 후에 불러오게된다)
        public Boolean ismax = false;
        public Boolean ismin = false;
        public Color c_color = Color.Black;
        //MY EVENT        
        public delegate void OnAlamStatusChangeHandler();
        public event OnAlamStatusChangeHandler OnAlamStausChanged; //UPDATE USER CURSOR

        public delegate void OnChangeDataHandler();
        public event OnChangeDataHandler OnChangeData;

        public delegate void OnChangeValueHandler();    //값이변경되었을경우 발생한다.
        public event OnChangeValueHandler OnChangeValueData;
        
        public Boolean enable = false;
       private Boolean active = false;
        public Single alamh = 0;
        public Single alaml = 0;
        public Single aalamh = 0;
        public Single aalaml = 0;
        public Single alamv = -999;    //auto시 기준값
        private COMM.EALAMTYPE alamatype;  //0:no 1:all 2:grp 3:cell
        private Single offset;
        public String 적용형태 = "";

        public Boolean _onalamh = false;
        public Boolean _onalaml = false;
        public Boolean _p_onalamh = false;
        public Boolean _p_onalaml = false;
        public Boolean _onalamover = false;
        public Boolean _p_onalamover = false;

        private String _value = "";
        public int value2 = 0;   //소수자리가 들어가기전의 값
        public Single CurValue =0f;
        public Single CurValue1 = 0f;

        public String mtime = "";
        private String _time_pre = ""; //이전값

        public String unit;   //표시단위(v,c)
        public int decpos;    //10진수단위값
       // public Form detailform = null;

        public Boolean Onset = false;
       // public Boolean firstsave = true;
        
        public CITEM()
        {
            offset = 0;
            column = 0;
            row = 0;
            name = "Citem";
            rect = new RectangleF(0, 0, 0, 0);
            BarRect = new RectangleF(0, 0, 0, 0);
            _value = "";
            value2 = 0;
            mtime = "";
            idx = 0;
           // detailform = null;
            Onset = false;
            active = false;
            seq = 0;
            CurValue = 0;
            CurValue1 = 0;
        }

        public CITEM(short pcolumn, short prow, String pname, ESTATUS pstatus, RectangleF prect)
        {
            this.column = pcolumn;
            this.row = prow;
            this.name = pname;
            BarRect = new RectangleF(0, 0, 0, 0);
            this.rect = prect;
            CurValue = 0;
            CurValue1 = 0;
            //detailform = null;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("seq=" + seq.ToString() + ",offset=" + offset.ToString() + ",curval=" + CurValue.ToString() + ",rect=" + BarRect.ToString());
            sb.AppendLine("name=" + 이름 + ",use =" + 사용.ToString() + ",활성화=" + 활성화.ToString());
            return sb.ToString();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("측정값"), Category("측정값"),Browsable(false)]
        public String Value
        {
            get { return this._value; }
            set { this._value = value;

                //값이 이전과 바뀌었을경우에 사용한다.
            if (mtime != _time_pre)
                {
                    if (OnChangeValueData != null) OnChangeValueData();
                    _time_pre = mtime;
                }            
            
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("표시기호"), Category("정보"), Browsable(false)]
        public String 표시기호
        {
            get             {                return unit;             }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("소수점위치"), Category("정보"), Browsable(false)]
        public int 소수점위치
        {
            get
            {
                return decpos;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("측정값"), Category("정보"),Browsable(false)]
        public String 측정값
        {
            get {
                if (_value == null) return "";
                else return _value; 
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("측정된시간"), Category("정보"), Browsable(false)]
        public String 측정시간
        {
            get {
                if (mtime == null) return "";
                else return mtime; 
                 }
        }


        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("값배열을 참조하는 Channel INDEX"), Category("테스트"), System.ComponentModel.Browsable(false)]
        public int 인덱스_번호
        {
            get { return idx; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("값배열을 참조하는 Channel INDEX"), Category("테스트"), System.ComponentModel.Browsable(false)]
        public int 인덱스_채널
        {
            get { return idx_ch; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("값배열을 참조하는 Device INDEX"), Category("테스트"), System.ComponentModel.Browsable(false)]
        public int 인덱스_장치
        {
            get { return idx_dev; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("값배열을 참조하는 SubUnit INDEX"), Category("테스트"), System.ComponentModel.Browsable(false)]
        public int 인덱스_유닛
        {
            get { return idx_unit; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("HIGH 알람의 발생여부(변경불가)"), Category("알람발생상태"), System.ComponentModel.Browsable(false)]
        public Boolean 상위알람발생
        {
            get { return _onalamh; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("HIGH 알람의 발생여부(변경불가)"), Category("알람발생상태"), System.ComponentModel.Browsable(false)]
        public Boolean onalamh
        {
            get { return _onalamh; }
            set
            {
                _onalamh = value;
                if (OnAlamStausChanged != null) OnAlamStausChanged();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("HIGH 알람의 발생여부(변경불가)"), Category("알람발생상태"), System.ComponentModel.Browsable(false)]
        public Boolean onalaml
        {
            get { return _onalaml; }
            set { _onalaml = value;
                if (OnAlamStausChanged != null) OnAlamStausChanged();
            }
        }


        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("LOW 알람의 발생여부(변경불가)"), Category("알람발생상태"), System.ComponentModel.Browsable(false)]
        public Boolean 하위알람발생
        {
            get { return _onalaml; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),Browsable(true),   Category("모양"), Description("(보정값) 이곳에 값을 입력하면 해당 셀값에 + 됩니다.")]
        public Single Offset
        {
            get { return offset; }
            set { offset = value;
            if (OnChangeData != null) OnChangeData();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("알람설정"), Description("셀 알람")]
        public COMM.EALAMTYPE 알람형태
        {
            get { return alamatype; }
            set { 
                alamatype = value;
                alamv = -999;
                if (OnChangeData != null) OnChangeData();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("알람(수동)"), Description("상위 알람값입니다. 직접입력일경우에만 입력하세요.")]
        public Single HIGH
        {
            get { return alamh; }
            set
            {
                if (알람형태 == COMM.EALAMTYPE.개별알람)
                {
                    alamh = value;
                    if (OnChangeData != null) OnChangeData(); 
                }
                else
                {
                    MessageBox.Show("알람값을 입력하려면 [개별알람]으로 변경 후 입력하세요");
                }
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("알람(수동)"), Description("하위 알람값입니다. 직접입력일경우에만 입력하세요.")]
        public Single LOW
        {
            get { return alaml; }
            set {
                if (알람형태 == COMM.EALAMTYPE.개별알람)
                {
                    alaml = value;
                    if (OnChangeData != null) OnChangeData();
                }
                else
                {
                    MessageBox.Show("알람값을 입력하려면 [개별알람]으로 변경 후 입력하세요");
                }
                
            }
        }


        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("알람(자동)"), Description("상위 알람값입니다. 직접입력일경우에만 입력하세요.")]
        public Single H
        {
            get { return aalamh; }
            set
            {
                if (알람형태 == COMM.EALAMTYPE.개별알람자동)
                {
                    aalamh = value;
                    if (OnChangeData != null) OnChangeData();

                }
                else
                {
                    MessageBox.Show("알람값을 입력하려면 [개별알람자동]으로 변경 후 입력하세요");
                }

            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("알람(자동)"), Description("하위 알람값입니다. 직접입력일경우에만 입력하세요.")]
        public Single L
        {
            get { return Math.Abs(aalaml); }
            set
            {
                if (알람형태 == COMM.EALAMTYPE.개별알람자동)
                {
                    aalaml = Math.Abs(value);
                    if (OnChangeData != null) OnChangeData();
                }
                else
                {
                    MessageBox.Show("알람값을 입력하려면 [개별알람자동]으로 변경 후 입력하세요");
                }
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("알람(자동)"), Description("기준값")]
        public Single 기준값
        {
            get { return alamv; }
            set { this.alamv = value; }
        
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),System.ComponentModel.Browsable(false)]
        public int Column
        {
            get { return this.column; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), System.ComponentModel.Browsable(false)]
        public int Row
        {
            get { return this.row; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("모양"), Description("이 셀의 이름입니다.")]
        public string 이름
        {
            get { return this.name; }
            set { name = value;
            if (OnChangeData != null) OnChangeData();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("모양"), Description("이 셀의 사용여부를 설정합니다. 사용이 해제된 셀은 자료가 저장되지 않습니다.")]
        public Boolean 사용
        {
            get { return this.enable; }
            set { this.enable = value;
            if (OnChangeData != null) OnChangeData();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("정보"), Description("이 셀의 활성화여부를 설정합니다. 해제된 셀은 알람"), Browsable(true)]
        public Boolean 활성화
        {
            get { return this.active; }
            set
            {
                this.active = value;
            }
        }

    }
}
 