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


namespace UIControl
{
    public partial class HMI
    {
        void Draw_PickerY(Graphics g, RectangleF rect)
        {

            //return;

            //전체영역의 80% 영역에 Y-로봇의 축을 그린다.
            //var motorMax = 400; //전체 가동 길이 400mm
            var cx = rect_main.Left + rect_main.Width / 2.0f;
            var cy = rect_main.Top + rect_main.Height * 0.2f;// / 2.0f - 200;


            //모터길이가 설정되지않았따면 600으로 설정한다
            if (this.arMotorLengthY == 0) this.arMotorLengthY = 600;
            var motYPosX = rect.Left + (rect.Width * ((arMotorPosition[0] + 1) / (this.arMotorLengthY * 1.0f)));



            //g.DrawString(arMotorPositionYP.ToString() + "/" + motYPosX.ToString(), this.Font, Brushes.Red, 100, 100);

            //Y축 모터의 현재위치를 표시한다
            g.DrawLine(new Pen(Color.SteelBlue, 10), (float)motYPosX, rect.Top, (float)motYPosX, rect.Bottom);

            g.DrawLine(new Pen(Color.Black, 1), (float)motYPosX, rect.Top - 5, (float)motYPosX, rect.Bottom + 5);

            //Y축 모터의 영역을 표시한다.
            //            g.DrawRect(rect_picker, Color.White, 3);

            if(arPickerSafeZone) g.FillRectangle(Brushes.Lime, rect_picker);
            else g.FillRectangle(Brushes.DimGray, rect_picker);
            g.DrawString(arMotPosNamePX, arFont_MotPosName, Brushes.Gray, rect_picker, new StringFormat
            {
                Alignment = StringAlignment.Near,
                LineAlignment = StringAlignment.Center
            });
            if (arMotILockPKX) g.DrawRect(rect_picker, Color.Blue, 5);
            else g.DrawRect(rect_picker, Color.FromArgb(50, 50, 50), 3);

            //중앙에 Safty Zone 글자 표시함
            if(arPickerSafeZone)
            {
                g.DrawString("PICKER X - SAFETY ZONE", new Font("Consolas",12, FontStyle.Bold), Brushes.Black, rect_picker, new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                });
            }


            //Z축 모터의 영역 계산     
            var motZLPosX = motYPosX;
            //   var motZRPosX = motYPosX + CvtMMtoPX_W(motZSpaceMM / 2, 0);
            var motZPosY = rect.Top - CvtMMtoPX_H(50, 0); //Y축하단에서 50mm 아래에 Z축을 표시한다.
            var motZHMm = 300;
            var motZhPx = CvtMMtoPX_H(motZHMm, 0); //
            var motZwPx = rect.Height;
            rect_zlaxis = new RectangleF((float)(motZLPosX - motZwPx / 2.0), (float)motZPosY, motZwPx, (float)motZhPx);
            //  rect_zraxis = new RectangleF((float)(motZRPosX - motZwPx / 2.0), (float)motZPosY, motZwPx, (float)motZhPx);

            //현재위치를 표시하는 영역생성
            var zlSize = g.MeasureString("UNKNOWN", this.Font);
            var zrSize = g.MeasureString("UNKNOWN", this.Font);
            var zSizeW = Math.Max(zlSize.Width, zrSize.Width);
            var zSizeH = Math.Max(zlSize.Height, zrSize.Height);
            var rect_zlposname = new RectangleF(
                rect_zlaxis.Left + (rect_zlaxis.Width - zSizeW) / 2.0f,
                rect_zlaxis.Top - zSizeH + 5,
                zSizeW,
                zSizeH);


            g.FillRectangle(Brushes.DimGray, rect_zlaxis);


            //테두리
            if (arMotILockPKZ == true) g.DrawRect(rect_zlaxis, Color.Blue, 5);
            else g.DrawRect(rect_zlaxis, Color.DimGray, 3);



            g.FillRectangle(Brushes.DimGray, rect_zlposname);

            if (arMotILockPKZ == true) g.DrawRect(rect_zlposname, Color.Blue, 5);
            else g.DrawRect(rect_zlposname, Color.DimGray, 3);


            //피커Z축위치 표시
            g.DrawString(arMotPosNamePZ, arFont_MotPosName, Brushes.Gray, rect_zlposname, new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            });


            //중심점 센서 확인
            g.DrawLine(new Pen(Color.Black, 1), (float)motZLPosX, rect.Top - 5, (float)motZLPosX, rect.Bottom + 5);

            //Z축 모터위치와 길이확인
            if (this.arMotorLengthZL == 0) this.arMotorLengthZL = 600;
            var motZLosY = rect_zlaxis.Top + (rect_zlaxis.Height * ((arMotorPosition[1] + 1) / (this.arMotorLengthZL * 1.0f)));

            //상(Rear), 하(Front)로 영역을 그린다
            var port_width = rect_picker_left.Width;// * 3f;
            var port_height = rect_picker_left.Height; // rect.Height * 0.2f;

            //New Front Position
            var newYF = (float)(motZLPosX - port_width / 2.0);
            if (newYF != rect_picker_left.X)
            {
                var offset = newYF - rect_picker_left.X;
                this.rect_picker_left.Offset(offset, 0);  //좌표가 변경되었다면 재계산
                this.rect_picker_front_vac1.Offset(offset, 0);
                this.rect_picker_front_vac2.Offset(offset, 0);
                this.rect_picker_front_vac3.Offset(offset, 0);
                this.rect_picker_front_vac4.Offset(offset, 0);
            }
            if (motZLosY != rect_picker_left.Y)
            {
                var offset = (float)(motZLosY - rect_picker_left.Y);
                this.rect_picker_left.Offset(0, offset);  //좌표가 변경되었다면 재계산
                this.rect_picker_front_vac1.Offset(0, offset);
                this.rect_picker_front_vac2.Offset(0, offset);
                this.rect_picker_front_vac3.Offset(0, offset);
                this.rect_picker_front_vac4.Offset(0, offset);
            }

            //피커 #1 Circle 색상 
            var Bg1 = Color.FromArgb(100, 100, 100);
            var Bg2 = Color.FromArgb(160, 160, 160);
            if (this.arVar_Picker[0].Overload)
            {
                Bg1 = Color.Tomato;
                Bg2 = Color.Red;
            }
            else
            {
                if (this.arVar_Picker[0].ItemOn)
                {

                    //if (this.arVar_Picker[0].isReelDetect)
                    //{
                        Bg1 = Color.Lime; //.FromArgb(100, 100, 100);
                        Bg2 = Color.Green;//.FromArgb(160, 160, 160);
                    //}
                    //else
                    //{
                    //    Bg1 = Color.Magenta; //.FromArgb(100, 100, 100);
                    //    Bg2 = Color.DarkMagenta;//.FromArgb(160, 160, 160);
                    //}

                }
                else
                {
                    Bg1 = Color.FromArgb(100, 100, 100);
                    Bg2 = Color.FromArgb(160, 160, 160);
                }
            }


            using (var br = new LinearGradientBrush(rect_picker_left, Bg1, Bg2, LinearGradientMode.Vertical))
            {
                g.FillEllipse(br, rect_picker_left);
            }

            //피커 #2 Circle 색상 
            if (this.arVar_Picker[1].Overload)
            {
                Bg1 = Color.Tomato;
                Bg2 = Color.Red;
            }
            else
            {
                if (this.arVar_Picker[1].ItemOn)
                {
                    //실제 아이템 체크

                    if (this.arVar_Picker[1].isReelDetect)
                    {
                        Bg1 = Color.Lime; //.FromArgb(100, 100, 100);
                        Bg2 = Color.Green;//.FromArgb(160, 160, 160);
                    }
                    else
                    {
                        Bg1 = Color.Magenta; //.FromArgb(100, 100, 100);
                        Bg2 = Color.DarkMagenta;//.FromArgb(160, 160, 160);
                    }
                }
                else
                {
                    Bg1 = Color.FromArgb(100, 100, 100);
                    Bg2 = Color.FromArgb(160, 160, 160);
                }
            }


            //피커 테두리
            using (var bgPen = new Pen(Color.Black, 3))
            {
                var posT = arMotorPosition[6];
                g.DrawEllipse(bgPen, rect_picker_left);
                g.DrawString(posT.ToString("N0"), this.Font, Brushes.Black, rect_picker_left,new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }

            //피커 내부의 진공 표현
            g.FillEllipse((this.arVar_Picker[0].VacOutput[0] ? brVacOn : brVacOff), rect_picker_front_vac1);
            g.FillEllipse((this.arVar_Picker[0].VacOutput[1] ? brVacOn : brVacOff), rect_picker_front_vac2);
            g.FillEllipse((this.arVar_Picker[0].VacOutput[2] ? brVacOn : brVacOff), rect_picker_front_vac3);
            g.FillEllipse((this.arVar_Picker[0].VacOutput[3] ? brVacOn : brVacOff), rect_picker_front_vac4);

            //피커설명 표시
            if (arVar_Picker[0].Overload)
                g.DrawString("OVL", arFont_picker, Brushes.Black, rect_picker_left, sfCenter);
            else
                g.DrawString(this.arVar_Picker[0].PortPos, arFont_picker, Brushes.Black, rect_picker_left, sfCenter);

            //피커 진공표시 테두리 (진공출력상태에 따라서 색상을 달리 함)
            g.DrawEllipse((this.arVar_Picker[0].VacOutput[0] ? penVacOn : penVacOff), rect_picker_front_vac1);
            g.DrawEllipse((this.arVar_Picker[0].VacOutput[1] ? penVacOn : penVacOff), rect_picker_front_vac2);
            g.DrawEllipse((this.arVar_Picker[0].VacOutput[2] ? penVacOn : penVacOff), rect_picker_front_vac3);
            g.DrawEllipse((this.arVar_Picker[0].VacOutput[3] ? penVacOn : penVacOff), rect_picker_front_vac4);

        }
    }
}
