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

namespace AGVControl
{
    public partial class BatteryLevelGauge : Control
    {
        float _vlevel = 50;
        public float VLevel { get { return _vlevel; } set { _vlevel = value; } }
        public float Volt { get; set; } = 0;
        public String sign { get; set; } = "%";
        public float CurA { get; set; } = 0;
        public float MaxA { get; set; } = 0;

        bool isopen = false;
        public Boolean IsOpen { get { return isopen; } set { isopen = value; this.Invalidate(); } } 

        public Color BorderColor { get; set; } = Color.DimGray;

        public BatteryLevelGauge()
        {
            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;
        }


        protected override void OnPaint(PaintEventArgs pe)
        {
            pe.Graphics.InterpolationMode = InterpolationMode.High;
            pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            pe.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            //base.OnPaint(pe);
            pe.Graphics.FillRectangle(new SolidBrush(BackColor), DisplayRectangle);

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

            pe.Graphics.FillRectangle(new SolidBrush(this.BackColor), r);


            var w = r.Width * (this.VLevel / 100f);
            var lr = new RectangleF(r.Left, r.Top, w, r.Height);

            Color bColor = Color.Red;
            if (VLevel > 80) bColor = Color.YellowGreen;
            else if (VLevel > 60) bColor = Color.Yellow;
            else if (VLevel > 40) bColor = Color.Orange;
            else if (VLevel > 20) bColor = Color.Tomato;
            else bColor = Color.Red;
            pe.Graphics.FillRectangle(new SolidBrush(bColor), lr);

            Color textcolor = this.ForeColor;
            if (IsOpen == false) textcolor = Color.Black;
            var smg = IsOpen ? $"{ this.VLevel:N0}{ this.sign}" : "연결안됨";

            pe.Graphics.DrawString(smg, this.Font, new SolidBrush(textcolor), r,
            new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            });

            pe.Graphics.DrawRectangle(Pens.Black, r.Left, r.Top, r.Width, r.Height);


        }
        void arLabel_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
