/*
 * Copyright (c) 2025-2025 GeyserMC. http://geysermc.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 *
 *  @author GeyserMC
 *  @link https://github.com/GeyserMC/PackConverter
 *
 */

package org.geysermc.pack.converter.type.sound;

import org.geysermc.pack.bedrock.resource.BedrockResourcePack;
import org.geysermc.pack.bedrock.resource.sounds.sounddefinitions.SoundDefinitions;
import org.geysermc.pack.bedrock.resource.sounds.sounddefinitions.Sounds;
import org.geysermc.pack.converter.pipeline.AssetCombiner;
import org.geysermc.pack.converter.pipeline.AssetConverter;
import org.geysermc.pack.converter.pipeline.CombineContext;
import org.geysermc.pack.converter.pipeline.ConversionContext;
import org.jetbrains.annotations.Nullable;
import team.unnamed.creative.sound.SoundEntry;
import team.unnamed.creative.sound.SoundEvent;
import team.unnamed.creative.sound.SoundRegistry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SoundRegistryConverter implements AssetConverter<SoundRegistry, Map<String, SoundDefinitions>>, AssetCombiner<Map<String, SoundDefinitions>> {
    public static final SoundRegistryConverter INSTANCE = new SoundRegistryConverter();
    static final String BEDROCK_SOUNDS_LOCATION = "sounds";

    @Override
    public @Nullable Map<String, SoundDefinitions> convert(SoundRegistry soundRegistry, ConversionContext context) throws Exception {
        Map<String, SoundDefinitions> definitions = new HashMap<>();

        for (SoundEvent value : soundRegistry.sounds()) {
            String key = value.key().asString();

            SoundDefinitions definition = new SoundDefinitions();
            definition.useLegacyMaxDistance(true); // TODO: Needed?
            definition.maxDistance(64); // ???
            for (SoundEntry sound : value.sounds()) {
                Sounds bedrockSound = new Sounds();
                bedrockSound.name(BEDROCK_SOUNDS_LOCATION + "/" + sound.key().value());
                bedrockSound.stream(sound.stream());
                bedrockSound.loadOnLowMemory(true);
                bedrockSound.volume(sound.volume());
                bedrockSound.pitch(sound.pitch());
                bedrockSound.weight(sound.weight());

                definition.sounds().add(bedrockSound);
            }

            definitions.put(key, definition);
        }

        return definitions;
    }

    @Override
    public void include(BedrockResourcePack pack, List<Map<String, SoundDefinitions>> maps, CombineContext context) {
        List<String> soundEvents = new ArrayList<>();

        for (Map<String, SoundDefinitions> definitions : maps) {
            for (Map.Entry<String, SoundDefinitions> entry : definitions.entrySet()) {
                if (soundEvents.contains(entry.getKey())) {
                    context.warn("Conflicting sound event " + entry.getKey() + "!");
                }
                pack.addSoundDefinition(entry.getKey(), entry.getValue());
                soundEvents.add(entry.getKey());
            }
        }
    }
}
