using Newtonsoft.Json;
namespace DragonVideoPlayer
{
    public class VideoPreset
    {
        public double Brightness { get; set; }
        public double Contrast { get; set; }
        public double Gamma { get; set; }
        public double Saturation { get; set; }
        public double Hue { get; set; }
    }
    public class MpvController
    {
        private MpvPlayer _player;
        public MpvController(MpvPlayer player)
        {
            _player = player;
        }
        public void ApplyPreset(VideoPreset preset)
        {
            SetBrightness(preset.Brightness);
            SetContrast(preset.Contrast);
            SetGamma(preset.Gamma);
            SetSaturation(preset.Saturation);
            SetHue(preset.Hue);
        }





        public void SetBrightness(double value)
        {

            _player.SendToMpv($"{{\"command\": [\"set_property\", \"brightness\", {value.ToString(System.Globalization.CultureInfo.InvariantCulture)}]}}");
        }


        public void SetProperty(string property, object value)
        {
            var command = new
            {
                command = new object[]
                {
                "set_property",
                property,
                value
                }
            };

            string json = JsonConvert.SerializeObject(command);
            _player.SendToMpv(json);
        }
        public void ApplyEqualizer(EqualizerPreset preset)
        {

            var filters = preset.Bands
                .Select(b => $"equalizer=f={b.Frequency}:width_type=h:width=1:g={b.Gain}");

            string filterString = "lavfi=[" + string.Join(",", filters) + "]";

            SetProperty("af", filterString);
        }

        public void SetContrast(double value)
        {
            _player.SendToMpv($"{{\"command\": [\"set_property\", \"contrast\", {value.ToString(System.Globalization.CultureInfo.InvariantCulture)}]}}");
        }

        public void SetGamma(double value)
        {
            _player.SendToMpv($"{{\"command\": [\"set_property\", \"gamma\", {value.ToString(System.Globalization.CultureInfo.InvariantCulture)}]}}");
        }

        public void SetSaturation(double value)
        {
            _player.SendToMpv($"{{\"command\": [\"set_property\", \"saturation\", {value.ToString(System.Globalization.CultureInfo.InvariantCulture)}]}}");
        }
        public void SetHue(double value)
        {
            _player.SendToMpv($"{{\"command\": [\"set_property\", \"hue\", {value.ToString(System.Globalization.CultureInfo.InvariantCulture)}]}}");
        }

        internal void Kill()
        {
            throw new NotImplementedException();
        }
    }
}
