using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;

namespace Hawkeye.VisionBuilder.Workflow.ColorStudioClasses
{
    public class HSIValue
    {

        public HSIValue()
        {
            
        }
        public int MinSize { get; set; } = 1;
        public int MinComponentSize { get; set; } = 1;
        public int PreFilterId { get; set; } = 0;
        public int PreFilterSize { get; set; } = 10;

        public int Closing { get; set; }

        public int Circle { get; set; } = 1;
        public string Tag { get; set; }
        private Range<int> _hue = new Range<int>() {Minimum = 0,Maximum = 10};
        private Range<int> _saturation = new Range<int>() { Minimum = 0, Maximum = 10 };
        private Range<int> _intensity = new Range<int>() { Minimum = 0, Maximum = 10 };

        public string Name { get; set; }

        public Range<int> Hue
        {
            get { return _hue; }
            set
            {
                if (Equals(value, _hue)) return;
                _hue = value;
                OnPropertyChanged();
            }
        }

        public Range<int> Saturation
        {
            get { return _saturation; }
            set
            {
                if (Equals(value, _saturation)) return;
                _saturation = value;
                OnPropertyChanged();
            }
        }

        public Range<int> Intensity
        {
            get { return _intensity; }
            set
            {
                if (Equals(value, _intensity)) return;
                _intensity = value;
                OnPropertyChanged();
            }
        }

        public bool Active { get; set; }
        public List<HSIValue> SubColors { get; set; }=new List<HSIValue>();
        public Rectangle GetRectangle()
        {
            Rectangle th = new Rectangle((int) (Hue.Minimum), (int) (Saturation.Minimum),
                (int) (Hue.Maximum) - (int) (Hue.Minimum), (int) (Saturation.Maximum) - (int) (Saturation.Minimum));
            return th;
        }
        [XmlIgnore]
        public bool ShowStar { get; set; }

        public int Dilation { get; set; }


        public override string ToString()
        {
            return $"{Name} {Hue}:{Saturation}:{Intensity}{(ShowStar?"*":"")}";
        }

        

        public static explicit operator ColorConfig(HSIValue value)
        {
            var config = new ColorConfig()
            {
                ClosingCircleRadius = value.Circle,
                HueHighSaturationMaxGrey = value.Hue.Maximum,
                HueHighSaturationMinGrey = value.Hue.Minimum,
                HighSaturationMaxGrey = value.Saturation.Maximum,
                HighSaturationMinGrey = value.Saturation.Minimum,
                IntensityMaxGrey = value.Intensity.Maximum,
                IntensityMinGrey = value.Intensity.Minimum,
                MaxBlobSize = value.MinSize,
                MedianMargin = 1,
                MedianRadius = 10,
                UseMedianFilter = true,
                
            };
            return config;
        }

        public static implicit operator HSIValue(ColorConfig config)
        {
            return new HSIValue()
            {
                Hue = new Range<int>(config.HueHighSaturationMinGrey, config.HueHighSaturationMaxGrey),
                Saturation = new Range<int>(config.HighSaturationMinGrey, config.HighSaturationMaxGrey),
                Intensity = new Range<int>(config.IntensityMinGrey, config.IntensityMaxGrey),
                Circle = (int)config.ClosingCircleRadius,
                MinSize = config.MaxBlobSize
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}