/*
 *   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.simulator.gui;

import de.steamwar.bausystem.features.simulator.SimulatorWatcher;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.data.SimulatorPhase;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTPhase;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui;
import de.steamwar.core.Core;
import de.steamwar.data.CMDs;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.Arrays;

public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui {
    private final TNTElement tntElement;
    private final TNTPhase tnt;
    private final SimulatorBaseGui back;

    public SimulatorTNTPhaseSettingsGui(Player player, Simulator simulator, TNTElement tntElement, TNTPhase tnt, SimulatorBaseGui back) {
        super(player, simulator, 5 * 9);
        this.tntElement = tntElement;
        this.tnt = tnt;
        this.back = back;
    }

    @Override
    public String title() {
        return "TNT";
    }

    @Override
    public void populate() {
        if (!tntElement.getPhases().contains(tnt)) {
            back.open();
            return;
        }

        // Back Arrow
        inventory.setItem(0, new SWItem(Material.ARROW, "§eBack", clickType -> {
            back.open();
        }).setCustomModelData(CMDs.BACK));

        // Material Chooser
        inventory.setItem(4, tntElement.toItem(player, clickType -> {
            new SimulatorMaterialGui(player, simulator, tntElement::getMaterial, tntElement::setMaterial, this).open();
        }));

        //Delete
        inventory.setItem(8, new SWItem(Material.BARRIER, "§eDelete", clickType -> {
            tntElement.getPhases().remove(tnt);
            back.open();
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DELETE));

        //Count
        int count = tnt.getCount();
        inventory.setItem(9, new SWItem(SWItem.getDye(10), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            tnt.setCount(count + (clickType.isShiftClick() ? 5 : 1));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));

        SWItem countItem = new SWItem(Material.TNT, "§eCount§8:§7 " + count, clickType -> {
            new SimulatorAnvilGui<>(player, "Count", count + "", Integer::parseInt, integer -> {
                if (integer < 1) return false;
                tnt.setCount(integer);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.TNT).open();
        });
        countItem.getItemStack().setAmount(Math.max(1, Math.min(count, 64)));
        inventory.setItem(18, countItem);

        inventory.setItem(27, new SWItem(SWItem.getDye(count > 1 ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            tnt.setCount(Math.max(1, count - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        //Tick Offset
        int offset = tnt.getTickOffset();
        inventory.setItem(10, new SWItem(SWItem.getDye(10), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            tnt.setTickOffset(offset + (clickType.isShiftClick() ? 5 : 1));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));

        SWItem offsetItem = new SWItem(Material.REPEATER, "§eStart at§8:§7 " + offset, clickType -> {
            new SimulatorAnvilGui<>(player, "Start at", offset + "", Integer::parseInt, integer -> {
                if (integer < 0) return false;
                tnt.setTickOffset(integer);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.REPEATER).open();
        });
        offsetItem.getItemStack().setAmount(Math.max(1, Math.min(offset, 64)));
        inventory.setItem(19, offsetItem);

        inventory.setItem(28, new SWItem(SWItem.getDye(offset > 0 ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            tnt.setTickOffset(Math.max(0, offset - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        //Lifetime
        int lifetime = tnt.getLifetime();
        inventory.setItem(11, new SWItem(SWItem.getDye(10), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            tnt.setLifetime(lifetime + (clickType.isShiftClick() ? 5 : 1));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));

        SWItem lifetimeItem = new SWItem(Material.CLOCK, "§eLifetime§8:§7 " + lifetime, clickType -> {
            new SimulatorAnvilGui<>(player, "Lifetime", lifetime + "", Integer::parseInt, integer -> {
                if (integer < 1) return false;
                tnt.setLifetime(integer);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.CLOCK).open();
        });
        lifetimeItem.getItemStack().setAmount(Math.max(1, Math.min(lifetime, 64)));
        inventory.setItem(20, lifetimeItem);

        inventory.setItem(29, new SWItem(SWItem.getDye(lifetime > 0 ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            tnt.setLifetime(Math.max(1, lifetime - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        //Order
        int order = tnt.getOrder();
        inventory.setItem(13, new SWItem(SWItem.getDye(order < SimulatorPhase.ORDER_LIMIT ? 10 : 8), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            tnt.setOrder(Math.min(SimulatorPhase.ORDER_LIMIT, order + (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));

        Material negativeNumbers = Material.getMaterial(Core.getVersion() >= 19 ? "RECOVERY_COMPASS" : "FIREWORK_STAR");
        SWItem orderItem = new SWItem(order >= 0 ? Material.COMPASS : negativeNumbers, "§eCalculation Order§8:§7 " + order, clickType -> {
            new SimulatorAnvilGui<>(player, "Calculation Order", order + "", Integer::parseInt, integer -> {
                if (integer < -SimulatorPhase.ORDER_LIMIT) return false;
                if (integer > SimulatorPhase.ORDER_LIMIT) return false;
                tnt.setOrder(integer);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(order >= 0 ? Material.COMPASS : negativeNumbers).open();
        });
        orderItem.getItemStack().setAmount(Math.max(1, Math.min(Math.abs(order), 30)));
        inventory.setItem(22, orderItem);

        inventory.setItem(31, new SWItem(SWItem.getDye(order > -SimulatorPhase.ORDER_LIMIT ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            tnt.setOrder(Math.max(-SimulatorPhase.ORDER_LIMIT, order - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        //Jump
        SWItem jumpX = new SWItem(tnt.isXJump() ? Material.LIME_WOOL : Material.RED_WOOL, "§7TNT §eJump X§8: " + (tnt.isZJump() ? "§aon" : "§coff"), clickType -> {
            tnt.setXJump(!tnt.isXJump());
            SimulatorWatcher.update(simulator);
        });
        inventory.setItem(33, jumpX);

        SWItem jumpY = new SWItem(tnt.isYJump() ? Material.LIME_WOOL : Material.RED_WOOL, "§7TNT §eJump Y§8: " + (tnt.isYJump() ? "§aon" : "§coff"), clickType -> {
            tnt.setYJump(!tnt.isYJump());
            SimulatorWatcher.update(simulator);
        });
        inventory.setItem(16, jumpY);

        SWItem jumpZ = new SWItem(tnt.isZJump() ? Material.LIME_WOOL : Material.RED_WOOL, "§7TNT §eJump Z§8: " + (tnt.isZJump() ? "§aon" : "§coff"), clickType -> {
            tnt.setZJump(!tnt.isZJump());
            SimulatorWatcher.update(simulator);
        });
        inventory.setItem(35, jumpZ);

        SWItem jumpAll = new SWItem(Material.TNT, "§7TNT §eJump §8: " + (tnt.hasJump() ? "§aon" : "§coff"), clickType -> {
            tnt.setJump(!tnt.hasJump());
            SimulatorWatcher.update(simulator);
        });
        inventory.setItem(25, jumpAll);
    }
}
