using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class BetterRangeSlider : Control
    {
        // Properties for range and selection
        public float Minimum { get; set; } = 0;
        public float Maximum { get; set; } = 100;
        public float SelectedMin { get; set; } = 20;
        public float SelectedMax { get; set; } = 80;
        public int TickFrequency { get; set; } = 10;

        // Dragging state
        private bool isDraggingMin = false;
        private bool isDraggingMax = false;
        private const int ThumbWidth = 10;
        private const int MarginSide = 15;

        public event EventHandler ValuesChanged;

        public BetterRangeSlider()
        {
            this.DoubleBuffered = true; // Prevents flickering
            this.Height = 50;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            int trackY = Height / 2;

            // 1. Draw Track (The background line)
            g.DrawLine(Pens.Silver, MarginSide, trackY, Width - MarginSide, trackY);

            // 2. Draw Tick Marks
            for (float i = Minimum; i <= Maximum; i += TickFrequency)
            {
                float x = ValueToX(i);
                g.DrawLine(Pens.Gray, x, trackY + 5, x, trackY + 12);
            }

            // 3. Draw Range Highlight (The area between thumbs)
            float minX = ValueToX(SelectedMin);
            float maxX = ValueToX(SelectedMax);
            using (Brush highlightBrush = new SolidBrush(Color.FromArgb(100, Color.SkyBlue)))
            {
                g.FillRectangle(highlightBrush, minX, trackY - 3, maxX - minX, 6);
            }

            // 4. Draw Thumbs
            RectangleF minThumb = new RectangleF(minX - ThumbWidth / 2, trackY - 10, ThumbWidth, 20);
            RectangleF maxThumb = new RectangleF(maxX - ThumbWidth / 2, trackY - 10, ThumbWidth, 20);

            g.FillRectangle(Brushes.RoyalBlue, minThumb);
            g.DrawRectangle(Pens.DarkBlue, Rectangle.Round(minThumb));

            g.FillRectangle(Brushes.Crimson, maxThumb);
            g.DrawRectangle(Pens.DarkRed, Rectangle.Round(maxThumb));
        }

        // Helper: Map slider value to X coordinate
        private float ValueToX(float value)
        {
            return MarginSide + (Width - 2 * MarginSide) * (value - Minimum) / (Maximum - Minimum);
        }

        // Helper: Map X coordinate back to slider value
        private float XToValue(int x)
        {
            float val = Minimum + (float)(x - MarginSide) / (Width - 2 * MarginSide) * (Maximum - Minimum);
            return Math.Max(Minimum, Math.Min(Maximum, val));
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            // PROBLEM IF BOTH THUMBS AT 0 .. CANT MOVE MIN THUMB PAST
            // MAX THUMB and MIN THUMB GETS EVENT FIRST???
            float minX = ValueToX(SelectedMin);
            float maxX = ValueToX(SelectedMax);

            // Hit-testing: Check if mouse is near a thumb
            //if (Math.Abs(minX - maxX) < .5 && Math.Abs(minX) < .5)
            //{ 
            //isDraggingMax = true; Console.WriteLine("hack");
            // }   
            //else 
            if (Math.Abs(e.X - minX) < ThumbWidth) isDraggingMin = true;
            else if (Math.Abs(e.X - maxX) < ThumbWidth) isDraggingMax = true;
         
            
            // BMVP
           // else  // this just grabbed right
            //    isDraggingMax = true;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isDraggingMin)
            {
                // Update Min but don't let it pass Max
                //SelectedMin = Math.Min(XToValue(e.X), SelectedMax - 1); 
                SelectedMin = Math.Min(XToValue(e.X), SelectedMax );
                Invalidate();
                ValuesChanged?.Invoke(this, EventArgs.Empty);
            }
            else if (isDraggingMax)
            {
                // Update Max but don't let it pass Min
                //SelectedMax = Math.Max(XToValue(e.X), SelectedMin + 1);
                SelectedMax = Math.Max(XToValue(e.X), SelectedMin );
                Invalidate();
                ValuesChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            isDraggingMin = isDraggingMax = false;
            Console.WriteLine(SelectedMin.ToString());
            Console.WriteLine(SelectedMax.ToString());
            Console.WriteLine((Maximum - SelectedMax).ToString());
            Form1 mainForm = (Form1)this.FindForm();
            int best = (int)SelectedMin;
            mainForm.label1.Text = best.ToString();
            int crossover = (int)(SelectedMax - SelectedMin);
            mainForm.label2.Text = crossover.ToString();
            mainForm.label3.Text = (Maximum-crossover - best).ToString();
        }
    }

}
