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

namespace Solarus
{

const std::string EnumInfoTraits<FlangerPreset>::pretty_name = "flanger_effect_preset";

const EnumInfo<FlangerPreset>::names_type EnumInfoTraits<FlangerPreset>::names = {
  { FlangerPreset::SUBTLE,   "subtle" },
  { FlangerPreset::CLASSIC,  "classic" },
  { FlangerPreset::METALLIC, "metallic" },
};

FlangerAudioEffect::FlangerAudioEffect(FlangerPreset preset): AudioEffect(AudioEffectId::FLANGER) {
  ALWrapper::generate_effect(1, &effect, "create_flanger_effect");
  ALWrapper::set_effect_int(effect, AL_EFFECT_TYPE, AL_EFFECT_FLANGER, "create_flanger_effect");
  ALWrapper::set_effect_int(effect, AL_FLANGER_WAVEFORM, AL_FLANGER_WAVEFORM_TRIANGLE, "create_flanger_effect");
  apply_props(preset_props(preset));
  AudioEffect::create_slot();
}

void FlangerAudioEffect::set_preset(FlangerPreset preset) {
  apply_props(preset_props(preset));
  ALWrapper::set_effect_slot_int(auxiliary_slot, AL_EFFECTSLOT_EFFECT, static_cast<ALint>(effect), "set_flanger_preset");
}

void FlangerAudioEffect::apply_props(const Props& props) {
  ALWrapper::set_effect_float(effect, AL_FLANGER_RATE,     props.rate,     "create_flanger_effect");
  ALWrapper::set_effect_float(effect, AL_FLANGER_DEPTH,    props.depth,    "create_flanger_effect");
  ALWrapper::set_effect_float(effect, AL_FLANGER_FEEDBACK, props.feedback, "create_flanger_effect");
  ALWrapper::set_effect_float(effect, AL_FLANGER_DELAY,    props.delay,    "create_flanger_effect");
  ALWrapper::set_effect_int  (effect, AL_FLANGER_PHASE,    props.phase,    "create_flanger_effect");
}

FlangerAudioEffect::Props FlangerAudioEffect::preset_props(FlangerPreset preset) {
  switch (preset) {
    case FlangerPreset::SUBTLE:   return { 0.2f,  0.3f, 0.1f,  0.001f,  0 };
    case FlangerPreset::CLASSIC:  return { 0.27f, 0.8f, -0.5f, 0.002f,  0 };
    case FlangerPreset::METALLIC: return { 0.5f,  0.85f, 0.4f,  0.0035f, 90 };
    default:                      return { 0.27f, 0.8f, -0.5f, 0.002f,  0 };
  }
}

} // namespace Solarus
