/*
 * 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/EchoAudioEffect.h"
#include "solarus/core/Debug.h"
#include "solarus/core/Logger.h"

namespace Solarus
{

const std::string EnumInfoTraits<EchoPreset>::pretty_name = "echo_effect_preset";

const EnumInfo<EchoPreset>::names_type EnumInfoTraits<EchoPreset>::names = {
  { EchoPreset::CLOSE,  "close" },
  { EchoPreset::HALL,   "hall" },
  { EchoPreset::CAVE,   "cave" },
  { EchoPreset::CANYON, "canyon" },
};

EchoAudioEffect::EchoAudioEffect(EchoPreset preset): AudioEffect(AudioEffectId::ECHO) {
  ALWrapper::generate_effect(1, &effect, "create_echo_effect");
  ALWrapper::set_effect_int(effect, AL_EFFECT_TYPE, AL_EFFECT_ECHO, "create_echo_effect");
  apply_props(preset_props(preset));
  AudioEffect::create_slot();
}

void EchoAudioEffect::set_preset(EchoPreset preset) {
  apply_props(preset_props(preset));
  ALWrapper::set_effect_slot_int(auxiliary_slot, AL_EFFECTSLOT_EFFECT, static_cast<ALint>(effect), "set_echo_preset");
}

void EchoAudioEffect::apply_props(const Props& props) {
  ALWrapper::set_effect_float(effect, AL_ECHO_DELAY,    props.delay,    "create_echo_effect");
  ALWrapper::set_effect_float(effect, AL_ECHO_LRDELAY,  props.lrdelay,  "create_echo_effect");
  ALWrapper::set_effect_float(effect, AL_ECHO_DAMPING,  props.damping,  "create_echo_effect");
  ALWrapper::set_effect_float(effect, AL_ECHO_FEEDBACK, props.feedback, "create_echo_effect");
  ALWrapper::set_effect_float(effect, AL_ECHO_SPREAD,   props.spread,   "create_echo_effect");
}

EchoAudioEffect::Props EchoAudioEffect::preset_props(EchoPreset preset) {
  switch (preset) {
    case EchoPreset::CLOSE:  return { 0.05f, 0.05f, 0.8f, 0.2f,  0.0f };
    case EchoPreset::HALL:   return { 0.1f,  0.1f,  0.5f, 0.4f, -0.2f };
    case EchoPreset::CAVE:   return { 0.15f, 0.2f,  0.7f, 0.6f,  0.3f };
    case EchoPreset::CANYON: return { 0.207f,0.404f,0.2f, 0.75f, 1.0f };
    default:                 return { 0.1f,  0.1f,  0.5f, 0.4f, -0.2f };
  }
}

} // namespace Solarus
