using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Narumi.UC
{
    public partial class GuideSensor : Control
    {
        public int SensorValue { get; set; } = 0;
        public bool LMark { get; set; } = false;
        public bool RMark { get; set; } = false;

        public GuideSensor()
        {
            InitializeComponent();
            // Set Optimized Double Buffer to reduce flickering
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // Redraw when resized
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            this.Resize += arLabel_Resize;
        }
        void arLabel_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            var boxcount = 11;

            var r = new RectangleF(this.DisplayRectangle.Left + Padding.Left,
                this.DisplayRectangle.Top + Padding.Top,
                this.DisplayRectangle.Width - Padding.Left - Padding.Right,
                this.DisplayRectangle.Height - Padding.Top - Padding.Bottom);

            var term = 3;
            var w = ((r.Width -1) - (term * (boxcount - 1))) / boxcount;
            var h = r.Height -1;

            pe.Graphics.FillRectangle(new SolidBrush(this.BackColor), this.DisplayRectangle);
            //pe.Graphics.DrawRectangle(Pens.Red, r.Left, r.Top, r.Width, r.Height);

            for (int i = 0; i < boxcount; i++)
            {
                var x = r.Left + i * term + i * w;
                var y = r.Top;
                var r2 = new RectangleF(x, y, w, h);

                if (this.Enabled == false)
                {
                    pe.Graphics.FillRectangle(Brushes.LightGray, r2);
                }
                else
                {

                    if (i == 0)
                    {
                        if (LMark)
                            pe.Graphics.FillRectangle(Brushes.SkyBlue, r2);
                        else
                            pe.Graphics.FillRectangle(Brushes.LightGray, r2);
                    }
                    else if (i == 9)
                    {
                        if (RMark)
                            pe.Graphics.FillRectangle(Brushes.SkyBlue, r2);
                        else
                            pe.Graphics.FillRectangle(Brushes.LightGray, r2);
                    }
                    else
                    {
                        if (SensorValue == i)
                            pe.Graphics.FillRectangle(Brushes.Tomato, r2);
                        else
                            pe.Graphics.FillRectangle(Brushes.LightGray, r2);
                    }

                    pe.Graphics.DrawRectangle(Pens.DimGray, r2.Left,r2.Top,r2.Width,r2.Height);

                    if (i == 0 || i == 10)
                    {
                        pe.Graphics.DrawString("M/K", this.Font,
                            new SolidBrush(this.ForeColor), r2,
                            new StringFormat
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            });
                    }
                }

            }
        }
    }
}
