namespace DragonVideoPlayer
{
    public class AudioEffects
    {
        private MpvPlayer _player;

        public AudioEffects(MpvPlayer player)
        {
            _player = player;
        }

        private Dictionary<string, double[]> _eqPresets =
    new Dictionary<string, double[]>
{
        { "Flat",    new double[] { 0,0,0,0,0,0,0,0,0,0 } },
        { "Movie",   new double[] { 4,3,1,0,0,1,2,3,4,4 } },
        { "Sports",  new double[] { 2,2,1,0,1,2,3,3,2,1 } },
        { "Talk",    new double[] { -2,-1,0,1,3,4,4,2,0,-2 } },
        { "Jazz",    new double[] { 2,1,0,0,1,2,2,1,0,0 } },
        { "Classic", new double[] { 3,2,1,0,0,1,2,3,4,5 } },
        { "Rock",    new double[] { 5,3,1,0,1,3,5,4,3,2 } },
        { "重低音", new double[] { 8,7,5,2,0,-1,-2,-2,-3,-3 } },
};
        private readonly int[] _freq =
  {
    31, 62, 125, 250, 500,
    1000, 2000, 4000, 8000, 16000
};
        public void ApplyEqPreset(string name)
        {

            if (!_eqPresets.ContainsKey(name)) return;

            var gains = _eqPresets[name];

            var filters = gains
                .Select((g, i) => $"equalizer=f={_freq[i]}:g={g}");

            string filterChain = string.Join(",", filters);





            _player.SendToMpv($"{{\"command\": [\"af\", \"set\", \"{filterChain}\"]}}");

            string eqValues = string.Join(",", gains);
            if (config_class.config_bool_read("オーディオイコライザーを保存") == true)
            {
                config_class.save_config_file("オーディオイコライザーの値", eqValues);

            }



        }
        public void audio_Equalizer_start(String value)
        {
            _player.SendToMpv($"{{\"command\": [\"af\", \"add\", \"{value}\"]}}");
        }
        public void audio_lavfi_start(String value)
        {
            _player.SendToMpv($"{{\"command\": [\"af\", \"add\", \"{value}\"]}}");
        }
        public void audio_lavfi_normal()
        {
            _player.SendToMpv("{\"command\": [\"af\", \"set\", \"\"]}");

            //
        }
        public void audio_lavfi(String value)
        {
            if (config_class.config_bool_read("オーディオエフェクトを保存") == true)
            {
                config_class.save_config_file("オーディオエフェクトの値", value);
            }
            audio_lavfi_normal();
            _player.SendToMpv($"{{\"command\": [\"af\", \"add\", \"{value}\"]}}");
        }
        public void audio_lavfi_single(String value)
        {
            if (config_class.config_bool_read("オーディオエフェクトを保存") == true)
            {
                config_class.save_config_file("オーディオエフェクトの値", value);
            }


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

    }
}
