namespace DragonVideoPlayer
{

    public class video_window : Form
    {
        private void Flat_Click(object sender, EventArgs e) => ApplyPreset("Flat");

        private void vhs_Click(object sender, EventArgs e) => ApplyPreset("VHS");
        private void showa_Click(object sender, EventArgs e) => ApplyPreset("ShowaTV");
        private void blue_Click(object sender, EventArgs e) => ApplyPreset("Blue");
        private void mono_Click(object sender, EventArgs e) => ApplyPreset("Mono");
        private void warm_Click(object sender, EventArgs e) => ApplyPreset("Warm");
        private MpvController _mpv;
        public video_window(MpvController mpv)
        {
            _mpv = mpv;





            InitializeUI();
            read_config_file();
            this.FormClosing += new FormClosingEventHandler(formClosing);

        }
        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 }}

           };

        private TrackBar BrightnessSlider;
        private TrackBar ContrastSlider;
        private TrackBar GammaSlider;
        private TrackBar SaturationSlider;
        private TrackBar HueSlider;
        private Label BrightnessValueLabel;
        private Label ContrastValueLabel;
        private Label GammaValueLabel;
        private Label SaturationValueLabel;
        private Label HueValueLabel;
        private CheckBox chk_video_setting;


        private void InitializeUI()
        {
            this.Width = 800;
            this.Height = 450;

            Panel root = new Panel();
            root.Dock = DockStyle.Fill;
            this.Controls.Add(root);


            // ===== 共通スライダー生成関数 =====
            (TrackBar slider, Label valueLabel) CreateLabeledSlider(
                string text, int x, int y, EventHandler changedEvent)
            {
                // 左ラベル
                Label nameLabel = new Label();
                nameLabel.Text = text;
                nameLabel.Location = new Point(x - 45, y + 5);
                nameLabel.AutoSize = true;
                root.Controls.Add(nameLabel);

                // スライダー
                TrackBar tb = new TrackBar();
                tb.Minimum = -100;
                tb.Maximum = 100;
                tb.Width = 230;
                tb.Height = 30;
                tb.AutoSize = false;
                tb.Location = new Point(x, y);
                tb.TickStyle = TickStyle.None;
                root.Controls.Add(tb);

                // 右数値ラベル
                Label valueLabel = new Label();
                valueLabel.Text = "0";
                valueLabel.Width = 40;
                valueLabel.TextAlign = ContentAlignment.MiddleLeft;
                valueLabel.Location = new Point(x + 240, y + 5);
                root.Controls.Add(valueLabel);

                tb.ValueChanged += (s, e) =>
                {
                    valueLabel.Text = tb.Value.ToString();
                    changedEvent?.Invoke(s, e);
                };

                return (tb, valueLabel);
            }

            // ===== スライダー作成 =====
            (BrightnessSlider, BrightnessValueLabel) =
                CreateLabeledSlider("輝度", 80, 49, BrightnessChanged);

            (ContrastSlider, ContrastValueLabel) =
                CreateLabeledSlider("コントラスト", 80, 86, SetContrast);

            (GammaSlider, GammaValueLabel) =
                CreateLabeledSlider("ガンマ", 80, 134, SetGamma);

            (SaturationSlider, SaturationValueLabel) =
                CreateLabeledSlider("彩度", 80, 189, SetSaturation);

            (HueSlider, HueValueLabel) =
                CreateLabeledSlider("色相", 80, 246, hue);

            // ===== ボタン生成はそのまま =====
            Button CreateButton(string text, int x, int y, EventHandler click, int width = 75)
            {
                var btn = new Button();
                btn.Text = text;
                btn.Width = width;
                btn.Height = 30;
                btn.Location = new Point(x, y);
                btn.Click += click;
                root.Controls.Add(btn);
                return btn;
            }

            CreateButton("映画", 40, 309, CinemaButton_Click);
            CreateButton("Game", 115, 309, GameButton_Click);
            CreateButton("ブルーライト除去", 180, 309, NightButton_Click, 110);
            CreateButton("ダイナミック", 305, 309, Dynamic_Click);

            CreateButton("映画風モノクロ", 40, 344, Ciname_mono_Click, 100);
            CreateButton("レトロ映画", 146, 344, Retoro_cinema_Click);
            CreateButton("HDR風", 234, 354, hdr_Click);
            CreateButton("アニメ", 395, 299, anime_Click);
            CreateButton("スポーツ", 400, 344, sports_Click);
            CreateButton("ブルー", 510, 299, blue_Click);
            CreateButton("白黒", 510, 334, mono_Click);
            CreateButton("暖色", 510, 369, warm_Click);
            CreateButton("VHSレトロ", 430, 390, vhs_Click);
            CreateButton("昭和TV", 340, 390, showa_Click);
            CreateButton("フラット", 260, 390, Flat_Click);
        }
        private void read_config_file()
        {


            BrightnessSlider.Value = config_class.config_int_read("Brightness");
            ContrastSlider.Value = config_class.config_int_read("Contrast");
            SaturationSlider.Value = config_class.config_int_read("Saturation");
            GammaSlider.Value = config_class.config_int_read("Gamma");
            HueSlider.Value = config_class.config_int_read("Hue");
            if (_mpv != null)
            {
                _mpv.SetBrightness(BrightnessSlider.Value);
                _mpv.SetSaturation(SaturationSlider.Value);
                _mpv.SetHue(HueSlider.Value);
                _mpv.SetGamma(GammaSlider.Value);
                _mpv.SetContrast(ContrastSlider.Value);
            }


        }

        void formClosing(object sender, FormClosingEventArgs e)
        {

            if (config_class.config_bool_read("ビデオイコライザーを保存") == true)
            {
                save_config_file();
            }
        }


        private void save_config_file()
        {


            config_class.save_config_file("Brightness", BrightnessSlider.Value.ToString());
            config_class.save_config_file("Contrast", ContrastSlider.Value.ToString());


            config_class.save_config_file("Gamma", GammaSlider.Value.ToString());
            config_class.save_config_file("Saturation", SaturationSlider.Value.ToString());
            config_class.save_config_file("Hue", HueSlider.Value.ToString());
            config_class.save_config_file("video_setting", BrightnessSlider.Value.ToString() + "," + ContrastSlider.Value.ToString() + ","
                + GammaSlider.Value.ToString() + "," + SaturationSlider.Value.ToString() + "," + HueSlider.Value.ToString());




        }
        // ===========================
        // スライダー変更イベント
        // ===========================

        private void BrightnessChanged(object sender, EventArgs e)
        {
            if (_mpv != null)
            {
                _mpv.SetBrightness(BrightnessSlider.Value);
            }
        }

        private void SetContrast(object sender, EventArgs e)
        {
            if (_mpv != null)
            {
                _mpv.SetContrast(ContrastSlider.Value);
            }
        }

        private void SetGamma(object sender, EventArgs e)
        {
            if (_mpv != null)
            {
                _mpv.SetGamma(GammaSlider.Value);
            }
        }

        private void SetSaturation(object sender, EventArgs e)
        {
            if (_mpv != null)
            {
                _mpv.SetSaturation(SaturationSlider.Value);
            }
        }

        private void hue(object sender, EventArgs e)
        {
            if (_mpv != null)
            {
                _mpv.SetHue(HueSlider.Value);
            }
        }

        // ===========================
        // プリセット適用後UI更新
        // ===========================

        private void SliderUpdate(VideoPreset preset)
        {
            BrightnessSlider.Value = (int)Math.Round(preset.Brightness);
            ContrastSlider.Value = (int)Math.Round(preset.Contrast);
            GammaSlider.Value = (int)Math.Round(preset.Gamma);
            SaturationSlider.Value = (int)Math.Round(preset.Saturation);
            HueSlider.Value = (int)Math.Round(preset.Hue);
        }


        // ===========================
        // ボタンクリック
        // ===========================

        private void ApplyPreset(string name)
        {

            var preset = _presets[name];
            if (_mpv != null)
            {
                _mpv.ApplyPreset(preset);
            }
            SliderUpdate(preset);

        }

        private void CinemaButton_Click(object sender, EventArgs e) => ApplyPreset("Cinema");
        private void GameButton_Click(object sender, EventArgs e) => ApplyPreset("Game");
        private void NightButton_Click(object sender, EventArgs e) => ApplyPreset("Night");
        private void Dynamic_Click(object sender, EventArgs e) => ApplyPreset("Dynamic");
        private void Ciname_mono_Click(object sender, EventArgs e) => ApplyPreset("Cinema_mono");
        private void Retoro_cinema_Click(object sender, EventArgs e) => ApplyPreset("Retoro_cinema");
        private void hdr_Click(object sender, EventArgs e) => ApplyPreset("HDR");
        private void anime_Click(object sender, EventArgs e) => ApplyPreset("Anime");
        private void sports_Click(object sender, EventArgs e) => ApplyPreset("Sports");
    }

}
