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;

namespace UIControl
{
    public partial class CtlTowerLamp : CtlBase
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arPinRed { get { return PinList[0]; } set { PinList[0] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arPinYel { get { return PinList[1]; } set { PinList[1] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arPinGrn { get { return PinList[2]; } set { PinList[2] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arPinBuz { get { return PinList[3]; } set { PinList[3] = value; } }



        public CtlTowerLamp()
        {
            InitializeComponent();

            SetPinCount(4);
            this.MinimumSize = new Size(4, 4);
        }

        public override void MakeRect()
        {

        }
        public override void UpdateValue()
        {

        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            pe.Graphics.DrawRectangle(Pens.Gray, DisplayRectangle.Left, DisplayRectangle.Top, DisplayRectangle.Width - 1, DisplayRectangle.Height - 1);

            var baseRect = new Rectangle(DisplayRectangle.Left + Padding.Left,
                DisplayRectangle.Top + Padding.Top,
                DisplayRectangle.Width - Padding.Left - Padding.Right,
                DisplayRectangle.Height - Padding.Top - Padding.Bottom);


            //상위 80% 영역을 표시영역으로 사용한ㄷ
            var term = 3;
            var DispRect = new Rectangle(baseRect.Left, baseRect.Top, baseRect.Width, (int)(baseRect.Height * 0.8f));
            var LampHeight = arPinBuz.PinIndex == -1 ? (DispRect.Height - 3 * term) / 3.0f : (DispRect.Height - 4 * term) / 4.0f;

            var rectR = new RectangleF(DispRect.Left, DispRect.Top + term, DispRect.Width, LampHeight);
            var rectY = new RectangleF(DispRect.Left, rectR.Bottom + term, DispRect.Width, LampHeight);
            var rectG = new RectangleF(DispRect.Left, rectY.Bottom + term, DispRect.Width, LampHeight);
            var rectB = RectangleF.Empty;
            if (arPinBuz.PinIndex != -1)
            {
                rectB = new RectangleF(DispRect.Left, rectG.Bottom + term, DispRect.Width, LampHeight);
            }

            var rectCT = new RectangleF(DispRect.Left + (DispRect.Width - 20) / 2.0f, DispRect.Top, 20, baseRect.Height);
            pe.Graphics.FillRectangle(Brushes.DimGray, rectCT);
            pe.Graphics.DrawRectangle(Pens.Black, rectCT);

            if(this.PinList[0].Value)
                pe.Graphics.FillRectangle(Brushes.Red, rectR);
            else
                pe.Graphics.FillRectangle(Brushes.Gray, rectR);

            if (this.PinList[1].Value)
                pe.Graphics.FillRectangle(Brushes.Gold, rectY);
            else
                pe.Graphics.FillRectangle(Brushes.Gray, rectY);

            if (this.PinList[2].Value)
                pe.Graphics.FillRectangle(Brushes.Green, rectG);
            else
                pe.Graphics.FillRectangle(Brushes.Gray, rectG);

            pe.Graphics.DrawRectangle(Pens.Black, rectR);
            pe.Graphics.DrawRectangle(Pens.Black, rectY);
            pe.Graphics.DrawRectangle(Pens.Black, rectG);

            pe.Graphics.DrawString("RED",
                this.Font,
                Brushes.Black,
                rectR,
                new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

            pe.Graphics.DrawString("YEL",
                  this.Font,
                  Brushes.Black,
                  rectY,
                  new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

            pe.Graphics.DrawString("GRN",
                  this.Font,
                  Brushes.Black,
                  rectG,
                  new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

            if (rectB.IsEmpty == false)
            {
                if (this.PinList[3].Value)
                    pe.Graphics.FillRectangle(Brushes.Magenta, rectB);
                else
                    pe.Graphics.FillRectangle(Brushes.Gray, rectB);

                pe.Graphics.DrawRectangle(Pens.Black, rectB);

                pe.Graphics.DrawString("BUZ",
            this.Font,
            Brushes.Black,
            rectB,
            new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }

        }
    }
}
