namespace DragonVideoPlayer
{
    public class VideoEffects
    {
        private MpvPlayer _player;

        public VideoEffects(MpvPlayer player)
        {

            _player = player;
        }
        private Dictionary<string, VideoPreset> _presets =
        new Dictionary<string, VideoPreset>()
    {
            { "Cinema", new VideoPreset { Brightness = -5, Contrast = 15, Gamma = 5, Saturation = 10, Hue = 0 }},
            { "Game", new VideoPreset { Brightness = 5, Contrast = 20, Gamma = 0, Saturation = 15, Hue = 0 }},
            { "Night", new VideoPreset { Brightness = -10, Contrast = -5, Gamma = 8, Saturation = -10, Hue = 0 }},
            { "Dynamic", new VideoPreset { Brightness = 10, Contrast = 30, Gamma = -8, Saturation = 30, Hue = 0 }},
            { "Cinema_mono", new VideoPreset { Brightness = -5, Contrast = 20, Gamma = 5, Saturation = -100, Hue = 0 }},
            { "Hard_mono", new VideoPreset { Brightness = 0, Contrast = 30, Gamma = -5, Saturation = -100, Hue = 0 }},
            { "Retoro_cinema", new VideoPreset { Brightness = -5, Contrast = -10, Gamma = 8, Saturation = -20, Hue = 3 }},
            { "HDR", new VideoPreset { Brightness = 5, Contrast = 30, Gamma = -8, Saturation = 25, Hue = 0 }},
            { "Anime", new VideoPreset { Brightness = 3, Contrast = 18, Gamma = -2, Saturation = 18, Hue = 0 }},
            { "Sports", new VideoPreset { Brightness = 6, Contrast = 20, Gamma = -4, Saturation = 12, Hue = 0 }},
       // 追加
    { "Blue", new VideoPreset { Brightness = -3, Contrast = 5, Gamma = 4, Saturation = -5, Hue = -8 }},
    { "Mono", new VideoPreset { Brightness = 0, Contrast = 0, Gamma = 0, Saturation = -100, Hue = 0 }},
    { "Warm", new VideoPreset { Brightness = 3, Contrast = 5, Gamma = -2, Saturation = 5, Hue = 10 }},
{ "VHS", new VideoPreset { Brightness = -2, Contrast = -15, Gamma = 6, Saturation = -25, Hue = 4 }},
    { "ShowaTV", new VideoPreset { Brightness = 2, Contrast = -10, Gamma = 5, Saturation = -35, Hue = 2 }},
    { "Flat", new VideoPreset { Brightness = 0, Contrast = 0, Gamma = 0, Saturation =0, Hue = 0 }}

        };
        public void ApplyPreset(string name)
        {
            if (!_presets.ContainsKey(name)) return;

            var p = _presets[name];

            SetProperty("brightness", p.Brightness);
            SetProperty("contrast", p.Contrast);
            SetProperty("gamma", p.Gamma);
            SetProperty("saturation", p.Saturation);
            SetProperty("hue", p.Hue);
            config_class.save_config_file("Brightness", p.Brightness.ToString());
            config_class.save_config_file("Contrast", p.Contrast.ToString());


            config_class.save_config_file("Gamma", p.Gamma.ToString());
            config_class.save_config_file("Saturation", p.Saturation.ToString());
            config_class.save_config_file("Hue", p.Hue.ToString());
            config_class.save_config_file("video_setting", p.Brightness.ToString() + "," + p.Contrast.ToString() + ","
                + p.Gamma.ToString() + "," + p.Saturation.ToString() + "," + p.Hue.ToString());

        }
        private void SetProperty(string prop, double value)
        {
            string json = $"{{\"command\": [\"set_property\", \"{prop}\", {value}]}}";
            _player.SendToMpv(json);
        }
        public void vf_normal()
        {


            _player.SendToMpv("{\"command\": [\"set_property\", \"vf\", \"\"]}");
        }
        public void video_lavfi_start(String lavfi)
        {



            _player.SendToMpv($"{{\"command\": [\"vf\", \"add\", \"{lavfi}\"]}}");
        }

        public void video_scale_fix(String width)
        {



            _player.SendToMpv($"{{\"command\": [\"vf\", \"add\", \"scale={width}:-1\"]}}");
        }
        public void video_lavfi(String lavfi)
        {

            if (config_class.config_bool_read("ビデオエフェクトを保存") == true)
            {
                config_class.save_config_file("ビデオエフェクトの値", lavfi);
            }
            vf_normal();
            _player.SendToMpv($"{{\"command\": [\"vf\", \"add\", \"{lavfi}\"]}}");
        }
        public void vr360_mode_enable()
        {
            _player.SendToMpv("{\"command\": [\"vf\", \"set\", \"lavfi=[v360=input=equirect:output=rectilinear]\"]}");

        }
        public void vr360_mode_command(int yaw_360,int fov_360,int pitch_360,int roll_360)
        {

            _player.SendToMpv($"{{\"command\": [\"vf\", \"set\", \"lavfi=[v360=input=equirect:output=rectilinear:yaw={yaw_360}:pitch={pitch_360}:roll={roll_360}:h_fov={fov_360}:v_fov={fov_360}]\"]}}");



        }

        public void aspect(String value)
        {
            if (config_class.config_bool_read("ビデオエフェクトを保存") == true)
            {
                config_class.save_config_file("ビデオエフェクトの値", value);
            }

            _player.SendToMpv($"{{\"command\":  [\"set_property\", \"vf\", \"{value}\"]}}");
        }
        public void video_crop(String crop)
        {

            _player.SendToMpv($"{{\"command\":[\"set_property\",\"video-crop\",\"{crop}\"]}}");

        }
        public void panscan()
        {

            _player.SendToMpv("{\"command\": [\"set_property\", \"panscan\", 1.0]}");
        }

        public void ApplyGlsl(String glsl_path)
        {

            _player.SendToMpv("{\"command\": [\"change-list\", \"glsl-shaders\", \"clr\", \"\"]}");
            _player.SendToMpv(
                $"{{\"command\": [\"change-list\", \"glsl-shaders\", \"append\", \"{glsl_path}\"]}}"
            ); config_class.save_config_file("glslのパス", glsl_path);
        }
        public async void video_flip()
        {


            _player.SendToMpv("{\"command\": [\"vf\", \"add\", \"hflip\"]}");
        }
        public async void video_rotate_90_right()
        {


            _player.SendToMpv("{\"command\": [\"set_property\", \"video-rotate\", \"90\"]}");
        }
        public async void video_rotate_90_left()
        {

            _player.SendToMpv("{\"command\": [\"set_property\", \"video-rotate\", \"270\"]}");
        }

        public async void panscan_delay()
        {
            await Task.Delay(5000);

            _player.SendToMpv("{\"command\": [\"set_property\", \"panscan\", 1.0]}");
        }
        public void panscan_normal()
        {

            _player.SendToMpv("{\"command\": [\"set_property\", \"panscan\", 0]}");
        }



    }
}
