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 CtlCylinder : CtlBase
    {
        //  string text_;
        Font font_ = new Font("Malgun Gothic", 10);
        public enum eSensorType
        {
            SingleAction = 0,
            DoubleAction,
        }
        private eSensorType _arsensortype = eSensorType.SingleAction;
        public eSensorType arSensorType
        {
            get
            {
                return _arsensortype;
            }
            set
            {
                _arsensortype = value;
                this.Invalidate();
            }
        }
        [Browsable(true)]
        public new Font Font { get { return font_; } set { font_ = value; this.Invalidate(); } }

        public CtlCylinder()
        {
            InitializeComponent();

            SetPinCount(4); //입력2개 출력2개

            arOutputMax.ValueDirection = eValueDirection.output;
            arOutputMin.ValueDirection = eValueDirection.output;

            if (arVel == 0) arVel = 50;

            //실린더 가동핀은 변경되면 시작시간을 업데이트해줘야 한다
            PinList[0].ValueChanged += (s1, e1) => { RunStartTimeMax = DateTime.Now; };
            PinList[1].ValueChanged += (s1, e1) => { RunStartTimeMin = DateTime.Now; };
        }
        public override void MakeRect()
        {

        }

        public int arLength = 100;
        public int arVel { get; set; }
        public DateTime RunStartTimeMin = DateTime.Now;
        public DateTime RunStartTimeMax = DateTime.Now;
        //  public TimeSpan arRunSec = new TimeSpan(0);
        private double arRunLen = 0;
        public double arProgress
        {
            get
            {
                var val = (arRunLen / (arLength * 1.0) * 100.0);
                if (val > 100.0) val = 100;
                return val;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arOutputMax { get { return PinList[2]; } set { PinList[2] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arOutputMin { get { return PinList[3]; } set { PinList[3] = value; } }

        //Boolean aron1_ = false;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arInput1
        {
            get
            {
                return PinList[0];
            }
            set
            {
                //if (value != aron1_) RunStartTimeMax = DateTime.Now;
                PinList[0] = value;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo arInput2
        {
            get
            {
                return PinList[1];
            }
            set
            {
                // if (value != aron2_) RunStartTimeMin = DateTime.Now;
                PinList[1] = value;
                this.Invalidate();
            }
        }


        public Rectangle arRectProgress { get; set; }

        public override void UpdateValue()
        {
            //둘다 켜져잇거나 거져잇다면 작동하지 않는다
            if (arInput1 != arInput2)
            {
                if (arSensorType == eSensorType.SingleAction)
                {
                    var ts = DateTime.Now - RunStartTimeMax;
                    //단동은 1번 센서의 on/off 로 처리한다
                    if (arInput1.Value)
                    {
                        //경과시간만큼 MAX로 이동한다
                        arRunLen += ts.TotalSeconds * arVel;
                    }
                    else
                    {
                        //경과시간만큼 MIN으로 이동한다
                        arRunLen -= ts.TotalSeconds * arVel;
                    }
                    RunStartTimeMax = DateTime.Now;
                }
                else
                {
                    //복동은 1번센서는 Max로 2번센서는 Min으로 이동시킨다
                    if (arInput1.Value)
                    {
                        var ts = DateTime.Now - RunStartTimeMax;
                        arRunLen += ts.TotalSeconds * arVel;
                        RunStartTimeMax = DateTime.Now;
                    }
                    else if (arInput2.Value)
                    {
                        var ts = DateTime.Now - RunStartTimeMin;
                        arRunLen -= ts.TotalSeconds * arVel;
                        RunStartTimeMin = DateTime.Now;
                    }
                }
            }

            if (arRunLen > arLength) arRunLen = arLength;
            if (arRunLen < 1) arRunLen = 0;

            arOutputMax.Raw = arProgress >= 99.9;
            arOutputMin.Raw = arProgress <= 0.1;

            //       public Boolean arMax { get { return arProgress >= 99.9; } }
            //public Boolean arMin { get { return arProgress <= 0.1; } }
        }

        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);


            //pe.Graphics.DrawRect(baseRect, Color.Blue, 1);


            //사각형안에 사각형이 움직이는 걸로 하며 . 기본 H 배치한다
            var rectH = (int)(baseRect.Height * 0.6);
            var rectOut = new Rectangle(baseRect.Left,
                (int)(baseRect.Top + (baseRect.Height - rectH) / 2.0),
                (int)(baseRect.Width), rectH);

            var InOffset = (int)(baseRect.Height * 0.15);
            //var rectIn = new Rectangle(rectOut.Right, rectOut.Top + InOffset,
            //    DisplayRectangle.Width - rectOut.Width - 2, rectOut.Height - (InOffset * 2));



            //진행율(%)
            var progress = (arProgress / 100.0) * rectOut.Width;
            var PWid = 10;
            var rectP = new Rectangle((int)(progress - PWid + baseRect.Left), baseRect.Top, PWid, baseRect.Height);

            pe.Graphics.FillRectangle(Brushes.Gray, rectOut);
            pe.Graphics.DrawRect(rectOut, Color.Black, 2);

            //pe.Graphics.DrawRect(rectIn, Color.Black, 2);

            if (this.arOutputMax.Value)
                pe.Graphics.FillRectangle(Brushes.Red, rectP);
            else if (this.arOutputMin.Value)
                pe.Graphics.FillRectangle(Brushes.Blue, rectP);
            else
                pe.Graphics.FillRectangle(Brushes.Gold, rectP);

            pe.Graphics.DrawRect(rectP, Color.Black, 2);


            //가동상태를 상단에 표시한다
            var StSize = 10;// baseRect.Height * 0.15f;
            var rectp1 = new RectangleF(
                this.DisplayRectangle.Right - StSize - 3,
                DisplayRectangle.Top + 3,
                StSize, StSize
                );

            if (arInput1.Value)
                pe.Graphics.FillRectangle(Brushes.Red, rectp1);
            else
                pe.Graphics.FillRectangle(Brushes.Gray, rectp1);
            pe.Graphics.DrawRect(rectp1, Color.White);

            if (arSensorType == eSensorType.DoubleAction)
            {
                var rectp2 = new RectangleF(
              this.DisplayRectangle.Right - StSize - 3,
              DisplayRectangle.Bottom - StSize - 3,
              StSize, StSize
              );
                if (arInput2.Value)
                    pe.Graphics.FillRectangle(Brushes.Red, rectp2);
                else
                    pe.Graphics.FillRectangle(Brushes.Gray, rectp2);
                pe.Graphics.DrawRect(rectp2, Color.White);
            }

            //     if (arMin)
            //     {
            //         pe.Graphics.DrawString("MIN", this.Font, Brushes.Black, rectOut,
            //          new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            //     }
            //     else if(arMax)
            //     {
            //         pe.Graphics.DrawString("MAX", this.Font, Brushes.Black, rectOut,
            //new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            //     }
            //     else
            //     {
            //         pe.Graphics.DrawString(arProgress.ToString("N0")+"%", this.Font, Brushes.Black, rectOut,
            //new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            //     }


            if (string.IsNullOrEmpty(Text) == false)
            {
                pe.Graphics.DrawString(Text + "\n" + progress.ToString(),
                    this.Font,
                    Brushes.Black,
                    rectOut,
                    new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
        }
    }
}
