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 CtlMotor : CtlBase
    {
        public int Length { get; set; }
        //public Boolean Pin_Run { get; set; }
        //public Boolean Pin_DirCW { get; set; }
        //public Boolean Pin_Max { get; set; }
        //public Boolean Pin_Min { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo Pin_Run { get { return PinList[0]; } set { this.PinList[0] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PinInfo Pin_DirCW { get { return PinList[1]; } set { this.PinList[1] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
        public PinInfo Pin_Max { get { return PinList[2]; } set { this.PinList[2] = value; } }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
        public PinInfo Pin_Min { get { return PinList[3]; } set { this.PinList[3] = value; } }


        public Boolean speed { get; set; }

        Font font_ = new Font("Malgun Gothic", 10);

    

        [Browsable(true)]
        public new Font Font { get { return font_; } set { font_ = value; this.Invalidate(); } }

        public CtlMotor()
        {
            InitializeComponent();

            SetPinCount(4);

            Length = 100;
            this.Size = new Size(80, 80);
            this.MaximumSize = new Size(80, 80);
            this.MinimumSize = new Size(40, 40);
            if (this.Font == null) this.Font = new Font("Malgun Gothic", 10);
            if (this.Text == null) this.Text = string.Empty;
        }


        public override void MakeRect()
        {

        }

        public override void UpdateValue()
        {

        }

        int anim = 0;
        protected override void OnPaint(PaintEventArgs pe)
        {
          
            pe.Graphics.DrawRectangle(Pens.Gray, DisplayRectangle.Left, DisplayRectangle.Top, DisplayRectangle.Width - 1, DisplayRectangle.Height - 1);

            var rect = new Rectangle(DisplayRectangle.Left + 2, DisplayRectangle.Top + 2, 10, 10);
            var rect2 = new Rectangle(DisplayRectangle.Right - 2 - 10, DisplayRectangle.Top + 2, 10, 10);
           

            //모터영역을 그린다.
            var rectO = new RectangleF(
                DisplayRectangle.Left + Padding.Left,
                DisplayRectangle.Top + Padding.Top,
                DisplayRectangle.Width * 0.6f,
                DisplayRectangle.Height - Padding.Top - Padding.Bottom);

            var rectiH = rectO.Height * 0.6f;
            var rectI = new RectangleF(
                rectO.Left,
                rectO.Top + (rectO.Height - rectiH) / 2.0f,
                DisplayRectangle.Width - Padding.Left- Padding.Right,
                 rectiH
                );

         
        
            if (this.Pin_Run.Value)
            {
                if (this.Pin_DirCW.Value)
                {
                    if (anim % 2 == 0)
                        pe.Graphics.FillRectangle(Brushes.Lime, rectI);
                    else
                        pe.Graphics.FillRectangle(Brushes.Yellow, rectI);
                    pe.Graphics.DrawRectangle(Pens.Black, rectI);
                }
                else
                {
                    if (anim % 2 == 0)
                        pe.Graphics.FillRectangle(Brushes.Lime, rectI);
                    else
                        pe.Graphics.FillRectangle(Brushes.Blue, rectI);
                    pe.Graphics.DrawRectangle(Pens.Black, rectI);
                }

            }
            else
            {
                pe.Graphics.FillRectangle(Brushes.Red, rectI);
                pe.Graphics.DrawRectangle(Pens.Black, rectI);
            }
            pe.Graphics.DrawRect(rectI, Color.Black, 2);
            pe.Graphics.FillRectangle(Brushes.Gray, rectO);
            pe.Graphics.DrawRect(rectO, Color.Black, 2);



            //기어를 그린다.
            Point SPT = new Point(30, 10);
            int GearSize = 20;
            List<PointF> pts = new List<PointF>();
            pts.Add(new PointF(SPT.X, SPT.Y));
            pts.Add(new PointF(SPT.X + GearSize, SPT.Y));
            pts.Add(new PointF(SPT.X + GearSize, SPT.Y + GearSize));
            pts.Add(new PointF(SPT.X, SPT.Y + GearSize));
            pts.Add(pts[0]);

            var CenterPT = new PointF((pts[1].X - pts[0].X) / 2.0f + pts[0].X, (pts[2].Y - pts[0].Y) / 2.0f + pts[0].Y);


            var anglepts = GetAnglePonit(pts.ToArray(), PointF.Empty, 1);

            var degree = 4;
            var rad = degree * (Math.PI / 180);

          //  pe.Graphics.DrawPolygon(Pens.Blue, anglepts.ToArray());


            //pe.Graphics.DrawLine(Pens.Red, CenterPT.X - 10, CenterPT.Y, CenterPT.X + 10, CenterPT.Y);
            //pe.Graphics.DrawLine(Pens.Red, CenterPT.X, CenterPT.Y - 10, CenterPT.X, CenterPT.Y + 10);

           if (string.IsNullOrEmpty(Text) == false)
            {
                pe.Graphics.DrawString(Text ,
                    this.Font,
                    Brushes.Black,
                    rectO,
                    new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
            anim += 1;
        }

        List<PointF> GetAnglePonit(PointF[] array, PointF cpt, double degree)
        {
            if (degree > 360)
            {
                var mok = (int)(degree / 360.0);
                degree = degree - (360 * mok);
                if (degree > 180) degree *= -1;
            }
            var rad = degree * (Math.PI / 180);
            var retval = new List<PointF>();

            var x = array[0].X;// (Math.Cos(rad) * (CPT.X - array[0].X)) + (-Math.Sin(rad) * (CPT.Y - array[0].Y));
            var y = array[0].Y;// (Math.Sin(rad) * (CPT.X - array[0].X)) + (Math.Cos(rad) * (CPT.Y - array[0].Y));


            foreach (var p in array)
            {
                //변환해서 넣어줘야함
                var x1 = (p.X - cpt.X) * Math.Cos(rad) - (p.Y - cpt.Y) * Math.Sign(rad) + cpt.X;
                var y1 = (p.X - cpt.X) * Math.Sign(rad) + (p.Y - cpt.Y) * Math.Cos(rad) + cpt.Y;
                retval.Add(new PointF((float)(x1), (float)(y1)));
            }
            return retval;
        }
    }
}
