/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.detonator;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage;
import de.steamwar.bausystem.features.detonator.storage.ItemStorage;
import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Arrays;

@Linked
public class DetonatorCommand extends SWCommand {

    public static ItemStack getWAND(Player player) {
        ItemStack wand = new ItemStack(Material.BLAZE_ROD);
        ItemStorage.setDetonator(wand);
        ItemMeta meta = wand.getItemMeta();

        meta.setDisplayName(BauSystem.MESSAGE.parse("DETONATOR_WAND_NAME", player));
        meta.setLore(Arrays.asList(BauSystem.MESSAGE.parse("DETONATOR_WAND_LORE_1", player),
                BauSystem.MESSAGE.parse("DETONATOR_WAND_LORE_2", player),
                BauSystem.MESSAGE.parse("DETONATOR_WAND_LORE_3", player)));
        meta.setCustomModelData(3);

        wand.setItemMeta(meta);
        ItemUtils.setItem(wand, "detonator");
        return wand;
    }

    public DetonatorCommand() {
        super("detonator", "dt");
    }

    @Register(value = "wand", description = "DETONATOR_HELP_WAND")
    public void giveWand(@Validator Player p) {
        SWUtils.giveItemToPlayer(p, getWAND(p));
    }

    @Register(value = "click", description = "DETONATOR_HELP_CLICK")
    public void clickDetonator(@Validator Player p) {
        Detonator.activateDetonator(new ItemStorage(p));
    }

    @Register(value = "clear", description = "DETONATOR_HELP_CLEAR")
    public void clearDetonator(Player p) {
        DetonatorStorage storage = new ItemStorage(p);
        storage.clear();
        storage.write();
    }

    @Register(value = "autostart", description = "DETONATOR_HELP_AUTOSTART")
    public void toggleAutostartTimer(Player p) {
        boolean current = Config.getInstance().get(p).getPlainValueOrDefault("detonator-autostart", false);
        Config.getInstance().get(p).put("detonator-autostart", !current);
        if (!current) {
            BauSystem.MESSAGE.send("DETONATOR_AUTOSTART_ENABLE", p);
        } else {
            BauSystem.MESSAGE.send("DETONATOR_AUTOSTART_DISABLE", p);
        }
    }
}
