/*
 * Copyright (C) 2006-2026 Metallizer, Solarus - http://www.solarus-games.org
 *
 * Solarus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Solarus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "solarus/audio/ALWrapper.h"
#include "solarus/audio/effects/ChorusAudioEffect.h"
#include "solarus/core/Debug.h"
#include "solarus/core/Logger.h"

namespace Solarus
{

const std::string EnumInfoTraits<ChorusPreset>::pretty_name = "chorus_effect_preset";

const EnumInfo<ChorusPreset>::names_type EnumInfoTraits<ChorusPreset>::names = {
  { ChorusPreset::ENSEMBLE, "ensemble" },
  { ChorusPreset::HEAVY,    "heavy" },
  { ChorusPreset::LIGHT,    "light" },
  { ChorusPreset::STANDARD, "standard" },
  { ChorusPreset::VIBRATO,  "vibrato" },
};

ChorusAudioEffect::ChorusAudioEffect(ChorusPreset preset): AudioEffect(AudioEffectId::CHORUS) {
  ALWrapper::generate_effect(1, &effect, "create_chorus_effect");
  ALWrapper::set_effect_int(effect, AL_EFFECT_TYPE, AL_EFFECT_CHORUS, "create_chorus_effect");
  ALWrapper::set_effect_int(effect, AL_CHORUS_WAVEFORM, AL_CHORUS_WAVEFORM_SINUSOID, "create_chorus_effect");
  apply_props(preset_props(preset));
  AudioEffect::create_slot();
}

void ChorusAudioEffect::set_preset(ChorusPreset preset) {
  apply_props(preset_props(preset));
  ALWrapper::set_effect_slot_int(auxiliary_slot, AL_EFFECTSLOT_EFFECT, static_cast<ALint>(effect), "set_chorus_preset");
}

void ChorusAudioEffect::apply_props(const Props& props) {
  ALWrapper::set_effect_float(effect, AL_CHORUS_RATE,     props.rate,     "create_chorus_effect");
  ALWrapper::set_effect_float(effect, AL_CHORUS_DEPTH,    props.depth,    "create_chorus_effect");
  ALWrapper::set_effect_float(effect, AL_CHORUS_FEEDBACK, props.feedback, "create_chorus_effect");
  ALWrapper::set_effect_float(effect, AL_CHORUS_DELAY,    props.delay,    "create_chorus_effect");
  ALWrapper::set_effect_int  (effect, AL_CHORUS_PHASE,    props.phase,    "create_chorus_effect");
}

ChorusAudioEffect::Props ChorusAudioEffect::preset_props(ChorusPreset preset) {
  switch (preset) {
    case ChorusPreset::LIGHT:    return { 1.1f,  0.1f,  0.25f, 0.016f,  90 };
    case ChorusPreset::STANDARD: return { 1.1f,  0.3f,  0.5f,  0.016f,  90 };
    case ChorusPreset::HEAVY:    return { 3.0f,  0.8f,  0.7f,  0.016f,  90 };
    case ChorusPreset::VIBRATO:  return { 5.0f,  0.9f,  0.0f,  0.016f,   0 };
    case ChorusPreset::ENSEMBLE: return { 0.5f,  0.5f,  0.3f,  0.016f, 180 };
    default:                     return { 1.1f,  0.3f,  0.5f,  0.016f,  90 };
  }
}

} // namespace Solarus
