using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace UIControl
{
    public class CPort
    {
        public Boolean SaftyErr { get; set; }
        //public Boolean Safty2Err { get; set; }
        //        public Boolean SaftyErr { get { return Safty1Err || Safty2Err; } }
        public Boolean MotorRun { get; set; }
        public Boolean MotorDir { get; set; }
        public int arrowIndex { get; set; }
        public Boolean LimitUpper { get; set; }
        public Boolean LimitLower { get; set; }
        public Boolean OverLoad
        {
            get
            {
                return LimitLower && DetectUp;
            }
        }
        public byte AlignOK { get; set; }
        public void AlignReset() { AlignOK = 0; errorCount = 0; }
        public Boolean Ready { get; set; }
        public Boolean DetectUp { get; set; }   //상단에 있는 자재 감지 센서
        /// 
        /// 7인치 13인치의 크기 정보를 표시한다
        /// 
        public string title { get; set; }
        public int reelNo { get; set; }
        /// 
        /// 차수별 릴 작업 수량이 표시됨
        /// 
        public int reelCount { get; set; }
        public int errorCount { get; set; }
        public int CartSize { get; set; }
        public System.Drawing.Color bgColor { get; set; }
        private Boolean _enable = false;
        public Color fgColor { get; set; }
        public Color fgColorCount { get; set; }
        public Rectangle rect_title { get; set; }
        public RectangleF Rect { get; set; }
        public Rectangle rect_count { get; set; }
        public int AnimationStepPort { get; set; }
        /// 
        /// 0:notcart , 1:ready, 2:full
        /// 
        public ushort State { get; set; }
        public Boolean Enable
        {
            get { return _enable; }
            set
            {
                _enable = value;
                this.bgColor = value ? Color.Lime : Color.FromArgb(43, 43, 43);
                this.fgColor = value ? Color.White : Color.DimGray;
            }
        }
        public CPort()
        {
            CartSize = 0;
            Ready = false;
            Enable = false;
            rect_title = Rectangle.Empty;
            rect_count = Rectangle.Empty;
            Rect = RectangleF.Empty;
            reelNo = -1;
            arrowIndex = 2;
            reelCount = 0;
            fgColor = Color.Black;
            Clear();
            AlignOK = 0;
            AnimationStepPort = 9;
            //Items.Clear();
        }
        //public void ClearItem()
        //{
        //    Items.Clear();
        //}
        public void Clear()
        {
            CartSize = 0;
            Enable = true;
            SaftyErr = false;
            MotorRun = false;
            MotorDir = false;
            LimitUpper = false;
            LimitLower = false;
            reelNo = 0;
            reelCount = 0;
            DetectUp = false;
        }
        public void Display(Graphics g, Font fCnt, Font fMsg, Boolean Magneton, Boolean VisionRdy, Boolean VisionEnd, Boolean ItemOn, Boolean VisionLock, int VisionCnt)
        {
            if (Enable == false)
            {
                g.DrawLine(Pens.DimGray, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom);
                g.DrawLine(Pens.DimGray, Rect.Right, Rect.Top, Rect.Left, Rect.Bottom);
            }
            //모터사용시 화살표
            eDirection DirL = MotorDir == false ? eDirection.TopToBottom : eDirection.BottomToTop;
            if (MotorRun) UIControl.Common.Draw_Arrow(g, Rect, DirL, arrowIndex, AnimationStepPort, Color.Gold, fMsg);
            //글자표시 (크기 및 작업 수량)
            var sf = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center,
            };
            //리밋영역표시(상/하)
            var limitSizeH = (int)(Rect.Height * 0.2);
            if (OverLoad == true)//과적
            {
                g.FillRectangle(Brushes.Red, Rect);
                g.DrawString("OVER\nLOAD", fMsg, new SolidBrush(fgColor), Rect, sf);
            }
            else
            {
                if (errorCount > 5)
                {
                    g.FillRectangle(new SolidBrush(Color.FromArgb(250, Color.Gold)), Rect);
                }
                else
                {
                    g.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.Black)), Rect);
                }
                if (errorCount > 0)
                {
                    if (errorCount > 05)
                    {
                        g.DrawString(reelCount.ToString() + "\n(ERROR)", fCnt, new SolidBrush(Color.Red), Rect, sf);
                    }
                    else g.DrawString(reelCount.ToString() + "\nE:" + errorCount.ToString(), fCnt, new SolidBrush(Color.Red), Rect, sf);
                }
                else
                {
                    g.DrawString(reelCount.ToString(), fCnt, new SolidBrush(fgColor), Rect, sf);
                }
            }
            //마그넷상태표시
            var magheight = 30;
            var magrect = new RectangleF(this.Rect.Left, this.Rect.Bottom - magheight, this.Rect.Width, magheight);
            if (Magneton)
            {
                g.DrawString("LOCK(" + CartSize.ToString() + ")", new Font("Consolas", 10, FontStyle.Bold), Brushes.Gold, magrect, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
            else
            {
                g.FillRectangle(Brushes.DimGray, magrect);
                g.DrawString("UNLOCK(" + CartSize.ToString() + ")", new Font("Consolas", 10, FontStyle.Bold), Brushes.Black, magrect, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
            //아이템을 가지고 있다면 처리해준다.
            if (ItemOn)
            {
                magrect = new RectangleF(this.Rect.Left, this.Rect.Top, this.Rect.Width, magheight);
                g.FillRectangle(Brushes.Gold, magrect);
                g.DrawString("ITEM-ON", new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, magrect, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
            //데두리표시 ( 비활성 회색, 활성 감지 : 라임, 미감지 흰색)
            Color borderL = Enable ? (LimitUpper ? Color.Red : (LimitLower ? Color.Blue : (DetectUp ? Color.Lime : Color.White))) : Color.DimGray;
            if (OverLoad) borderL = Color.White;
            int bordersize = 7;//ortL.enable ? 7 : 1;
            //비젼영역추가 201228
            using (Font f = new Font("Consolas", 8, FontStyle.Bold))
            {
                var vrect = new RectangleF(Rect.Right, Rect.Top, 20, Rect.Height);
                Color fcolor2 = Color.Gray;
                var drawstr = "VISON RDY";
                if (VisionEnd) { drawstr = "VISION END"; fcolor2 = Color.Lime; }
                else if (VisionRdy) { drawstr = "VISION RUN"; fcolor2 = Color.Gold; };
                drawstr += "(" + VisionCnt.ToString() + ")";
                if (VisionLock) g.DrawRect(vrect, Color.Blue, 7);
                else g.DrawRect(vrect, fcolor2, 7);
                g.DrawString(drawstr, f, new SolidBrush(fcolor2), vrect, new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center,
                    FormatFlags = StringFormatFlags.DirectionVertical
                });
                vrect = new RectangleF(Rect.Left - 20, Rect.Top, 20, Rect.Height);
                fcolor2 = Color.Gray;
                drawstr = "PORT RDY(" + this.AlignOK.ToString() + ")";
                if (Ready) fcolor2 = Color.Lime;
                g.DrawRect(vrect, fcolor2, 7);
                g.DrawString(drawstr, f, new SolidBrush(fcolor2), vrect, new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center,
                    FormatFlags = StringFormatFlags.DirectionVertical
                });
            }
            if (OverLoad == false)
            {
                var fontsize = 9;
                using (Font fnt = new Font("Consolas", fontsize, FontStyle.Bold))
                {
                    //상단 리밋은 상단에
                    if (LimitUpper)
                    {
                        var msgLU = "+ LIMIT";
                        var fsize = g.MeasureString(msgLU, fnt);
                        var msgW = fsize.Width * 1.5f;
                        var msgH = fsize.Height * 1.5f;
                        if (msgW > this.Rect.Width * 0.70f) msgW = this.Rect.Width * 0.7f;
                        var RectMsgL = new RectangleF(
                        Rect.Left + (Rect.Width - msgW) / 2.0f,
                        Rect.Top - msgH - bordersize / 2.0f + 1,
                        msgW, msgH);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(250, Color.Red)), RectMsgL);
                        // g.DrawRectangle(Pens.Black, RectMsgL);
                        g.DrawString(msgLU, fnt, Color.White, RectMsgL);
                    }
                    //아이템 감지신호는 상단 아래쪽으로
                    if (Ready)
                    {
                        //var msgLU = "READY";
                        //var fsize = g.MeasureString(msgLU, fnt);
                        //var msgW = fsize.Width * 1.5f;
                        //var msgH = fsize.Height * 1.5f;
                        //if (msgW > this.Rect.Width * 0.70f) msgW = this.Rect.Width * 0.7f;
                        //var RectMsgL = new RectangleF(
                        //Rect.Left + (Rect.Width - msgW) / 2.0f,
                        //Rect.Top + bordersize / 2.0f - 1,
                        //msgW, msgH);
                        //g.FillRectangle(new SolidBrush(Color.FromArgb(250, Color.Lime)), RectMsgL);
                        //// g.DrawRectangle(Pens.Black, RectMsgL);
                        //g.DrawString(msgLU, fnt, Color.Black, RectMsgL);
                    }
                    //하단 리밋은 하단에표시
                    if (LimitLower)
                    {
                        var msgLU = "- LIMIT";
                        var fsize = g.MeasureString(msgLU, fnt);
                        var msgW = fsize.Width * 1.5f;
                        var msgH = fsize.Height * 1.5f;
                        if (msgW > this.Rect.Width * 0.70f) msgW = this.Rect.Width * 0.7f;
                        var RectMsgL = new RectangleF(
                        Rect.Left + (Rect.Width - msgW) / 2.0f,
                        Rect.Top - msgH - bordersize / 2.0f + 1,
                        msgW, msgH);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(250, Color.Blue)), RectMsgL);
                        //g.DrawString(msgLU, fnt, Brushes.White, RectMsgL, sf);
                        g.DrawString(msgLU, fnt, Color.White, RectMsgL);
                    }
                    //아이템 감지
                    if (DetectUp)
                    {
                        var msgLU = "DETECT";
                        var fsize = g.MeasureString(msgLU, fnt);
                        var msgW = fsize.Width * 1.5f;
                        var msgH = fsize.Height * 1.5f;
                        if (msgW > this.Rect.Width * 0.70f) msgW = this.Rect.Width * 0.7f;
                        var RectMsgL = new RectangleF(
                        Rect.Left + (Rect.Width - msgW) / 2.0f,
                        Rect.Bottom + 1,
                        msgW, msgH);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(250, Color.Lime)), RectMsgL);
                        //g.DrawRectangle(Pens.Black, RectMsgL);
                        g.DrawString(msgLU, fnt, Color.Black, RectMsgL);
                    }
                    //안전 오류는 중앙에           
                    if (SaftyErr)
                    {
                        var msgS = "SAFTY ERROR";
                        var fsize = g.MeasureString(msgS, fMsg);
                        var msgW = fsize.Width * 1.5f;
                        var msgH = fsize.Height * 1.5f;
                        if (msgW > this.Rect.Width * 0.80f) msgW = this.Rect.Width * 0.8f;
                        var RectMsgL = new RectangleF(
                        Rect.Left + (Rect.Width - msgW) / 2.0f,
                        Rect.Top + (Rect.Height - msgH) / 2.0f,
                        msgW, msgH);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(240, Color.Khaki)), RectMsgL);
                        g.DrawRectangle(Pens.Black, RectMsgL);
                        g.DrawString(msgS, fMsg, Color.Maroon, RectMsgL);
                    }
                }
            }
            //테두리가 리밋영역을 감추도록 그린다
            g.DrawRectangle(new Pen(borderL, bordersize), Rect.Left, Rect.Top, Rect.Width, Rect.Height);
            sf.Dispose();
        }
        public void DisplayConv(Graphics g, Font fCnt, Font fMsg, Boolean Magneton, Boolean VisionRdy, Boolean VisionEnd, Boolean ItemOn, Boolean VisionLock, int VisionCnt, bool cvbusy, bool cvreadyoff)
        {
            if (Enable == false)
            {
                g.DrawLine(Pens.DimGray, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom);
                g.DrawLine(Pens.DimGray, Rect.Right, Rect.Top, Rect.Left, Rect.Bottom);
            }
            //모터사용시 화살표
            eDirection DirL = MotorDir == false ? eDirection.TopToBottom : eDirection.BottomToTop;
            if (MotorRun) UIControl.Common.Draw_Arrow(g, Rect, DirL, arrowIndex, AnimationStepPort, Color.Gold, fMsg);
            //글자표시 (크기 및 작업 수량)
            var sf = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center,
            };
            //리밋영역표시(상/하)
            var limitSizeH = (int)(Rect.Height * 0.2);
            if (errorCount > 5)
            {
                g.FillRectangle(new SolidBrush(Color.FromArgb(250, Color.Gold)), Rect);
            }
            else
            {
                if (cvbusy)
                    g.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.Tomato)), Rect);
                if (cvreadyoff)
                    g.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.Red)), Rect);
                else
                    g.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.Black)), Rect);
            }
            if (reelCount != 0)
            {
                //버튼형태처럼 보이게한다.
                g.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.Gold)), Rect);
                g.DrawRectangle(new Pen(Color.WhiteSmoke, 5), Rect.Left, Rect.Top, Rect.Width, Rect.Height);
            }
            if (errorCount > 0)
            {
                if (errorCount > 05)
                {
                    g.DrawString(reelCount.ToString() + "\n(ERROR)", fCnt, new SolidBrush(Color.Red), Rect, sf);
                }
                else g.DrawString(reelCount.ToString() + "\nE:" + errorCount.ToString(), fCnt, new SolidBrush(Color.Red), Rect, sf);
            }
            else if (reelCount > 0)
            {
                g.DrawString(reelCount.ToString(), fCnt, new SolidBrush(fgColor), Rect, sf);
            }
            //마그넷상태표시
            var magheight = 30;
            var magrect = new RectangleF(this.Rect.Left, this.Rect.Bottom - magheight, this.Rect.Width, magheight);
            //if (Magneton)
            //{
            //    g.DrawString("LOCK(" + CartSize.ToString() + ")", new Font("Consolas", 10, FontStyle.Bold), Brushes.Gold, magrect, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            //}
            //else
            //{
            //    g.FillRectangle(Brushes.DimGray, magrect);
            //    g.DrawString("UNLOCK(" + CartSize.ToString() + ")", new Font("Consolas", 10, FontStyle.Bold), Brushes.Black, magrect, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            //}
            //아이템을 가지고 있다면 처리해준다.
            if (ItemOn)
            {
                magrect = new RectangleF(this.Rect.Left, this.Rect.Top, this.Rect.Width, magheight);
                g.FillRectangle(Brushes.Gold, magrect);
                g.DrawString("ITEM-ON", new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, magrect, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
            //데두리표시 ( 비활성 회색, 활성 감지 : 라임, 미감지 흰색)
            Color borderL = Enable ? Color.White : Color.DimGray;
            //if (OverLoad) borderL = Color.White;
            int bordersize = 7;//ortL.enable ? 7 : 1;
            //비젼영역추가 201228
            using (Font f = new Font("Consolas", 8, FontStyle.Bold))
            {
                var vrect = new RectangleF(Rect.Right, Rect.Top, 20, Rect.Height);
                Color fcolor2 = Color.Gray;
                var drawstr = "VISON RDY";
                if (VisionEnd) { drawstr = "VISION END"; fcolor2 = Color.Lime; }
                else if (VisionRdy) { drawstr = "VISION RUN"; fcolor2 = Color.Gold; };
                drawstr += "(" + VisionCnt.ToString() + ")";
                if (VisionLock) g.DrawRect(vrect, Color.Blue, 7);
                else g.DrawRect(vrect, fcolor2, 7);
                g.DrawString(drawstr, f, new SolidBrush(fcolor2), vrect, new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center,
                    FormatFlags = StringFormatFlags.DirectionVertical
                });
                vrect = new RectangleF(Rect.Left - 20, Rect.Top, 20, Rect.Height);
                fcolor2 = Color.Gray;
                drawstr = "EXT RDY";
                if (cvreadyoff) fcolor2 = Color.Red;
                else if (Ready) fcolor2 = Color.Lime;
                g.DrawRect(vrect, fcolor2, 7);
                g.DrawString(drawstr, f, new SolidBrush(fcolor2), vrect, new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center,
                    FormatFlags = StringFormatFlags.DirectionVertical
                });
            }
            //테두리가 리밋영역을 감추도록 그린다
            if (reelCount == 0)
            {
                g.DrawRectangle(new Pen(borderL, bordersize), Rect.Left, Rect.Top, Rect.Width, Rect.Height);
            }
            
            sf.Dispose();
        }
    }
}