package net.Chipperfluff.chipi.item.music;

import net.minecraft.item.Item;
import net.minecraft.item.MusicDiscItem;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import java.util.LinkedHashMap;
import java.util.Map;

import net.Chipperfluff.chipi.ChipiMod;

public class ModMusicDiscs {

    private static final Map<String, Item> DISCS = new LinkedHashMap<>();

    private static final MusicDiscDef[] DEFINITIONS = {
        new MusicDiscDef("aa9", 7, 5, 0),
        new MusicDiscDef("abc", 7, 2, 11),
        new MusicDiscDef("fs", 7, 2, 0),
        new MusicDiscDef("phone", 7, 2, 11),
        new MusicDiscDef("wha", 7, 1, 48),
        new MusicDiscDef("who", 7, 2, 27),
        new MusicDiscDef("working_as_intented", 7, 3, 45)
    };

    private static Item registerDisc(MusicDiscDef def) {
        String id = "chipi_record_" + def.name();
        Identifier identifier = new Identifier(ChipiMod.MOD_ID, id);

        SoundEvent sound = Registry.register(
            Registries.SOUND_EVENT,
            identifier,
            SoundEvent.of(identifier)
        );

        Item item = Registry.register(
            Registries.ITEM,
            identifier,
            new MusicDiscItem(
                def.comparatorOutput(),
                sound,
                new Item.Settings().maxCount(1),
                toTicks(def.minutes(), def.seconds())
            )
        );

        DISCS.put(def.name(), item);
        return item;
    }

    public static Map<String, Item> getAll() {
        return DISCS;
    }

    public static void registerAll() {
        for (MusicDiscDef def : DEFINITIONS) {
            registerDisc(def);
        }
    }

    private static int toTicks(int minutes, int seconds) {
        int totalSeconds = Math.max(0, minutes) * 60 + Math.max(0, seconds);
        return totalSeconds * 20;
    }
}
