/*
 *   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.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui;
import de.steamwar.data.CMDs;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SimulatorTNTSettingsGui extends SimulatorBaseGui {
    private final TNTElement tnt;
    private final SimulatorBaseGui back;

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

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

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

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

        // Material Chooser
        List<String> lore = new ArrayList<>();
        lore.add("§7Phase count§8:§e  " + tnt.getPhases().size());
        lore.add("§7Tick§8:§e " + tnt.getBaseTick());
        if (tnt.isDisabled()) {
            lore.add("");
            lore.add("§cDisabled");
        }
        inventory.setItem(4, tnt.toItem(player, clickType -> {
            new SimulatorMaterialGui(player, simulator, tnt::getMaterial, tnt::setMaterial, this).open();
        }));

        // Base Tick
        int baseTicks = tnt.getBaseTick();
        inventory.setItem(9, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.changeBaseTicks(clickType.isShiftClick() ? 5 : 1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        SWItem baseTick = new SWItem(Material.REPEATER, "§eTicks§8:§7 " + baseTicks, clickType -> {
            new SimulatorAnvilGui<>(player, "Ticks", baseTicks + "", Integer::parseInt, integer -> {
                if (integer < 0) return false;
                tnt.changeBaseTicks(integer - baseTicks);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.REPEATER).open();
        });
        baseTick.getItemStack().setAmount(Math.max(1, Math.min(baseTicks, 64)));
        inventory.setItem(18, baseTick);
        inventory.setItem(27, new SWItem(baseTicks > 0 ? Material.RED_DYE : Material.GRAY_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            if (baseTicks - (clickType.isShiftClick() ? 5 : 1) < 0) {
                tnt.changeBaseTicks(-baseTicks);
            } else {
                tnt.changeBaseTicks(clickType.isShiftClick() ? -5 : -1);
            }
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        // Subpixel Alignment
        inventory.setItem(21, new SWItem(Material.SUNFLOWER, "§7Align§8: §eCenter", clickType -> {
            tnt.alignX(0);
            tnt.alignZ(0);
            SimulatorWatcher.update(simulator);
        }));

        // Z
        SWItem negativZItem = new SWItem(Material.OAK_BUTTON, "§7Align§8: §eNegativ Z", clickType -> {
            if (tnt.getAlignment().getZ() != -1) tnt.alignZ(-1);
            SimulatorWatcher.update(simulator);
        });
        negativZItem.setEnchanted(tnt.getAlignment().getZ() == -1);
        inventory.setItem(20, negativZItem);

        SWItem positivZItem = new SWItem(Material.OAK_BUTTON, "§7Align§8: §ePositiv Z", clickType -> {
            if (tnt.getAlignment().getZ() != 1) tnt.alignZ(1);
            SimulatorWatcher.update(simulator);
        });
        positivZItem.setEnchanted(tnt.getAlignment().getZ() == 1);
        inventory.setItem(22, positivZItem);

        // X
        SWItem negativXItem = new SWItem(Material.STONE_BUTTON, "§7Align§8: §eNegativ X", clickType -> {
            if (tnt.getAlignment().getX() != -1) tnt.alignX(-1);
            SimulatorWatcher.update(simulator);
        });
        negativXItem.setEnchanted(tnt.getAlignment().getX() == -1);
        inventory.setItem(12, negativXItem);

        SWItem positivXItem = new SWItem(Material.STONE_BUTTON, "§7Align§8: §ePositiv X", clickType -> {
            if (tnt.getAlignment().getX() != 1) tnt.alignX(1);
            SimulatorWatcher.update(simulator);
        });
        positivXItem.setEnchanted(tnt.getAlignment().getX() == 1);
        inventory.setItem(30, positivXItem);

        // Pos X
        inventory.setItem(15, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList("§7Shift§8: §e+0.0625"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.move(clickType.isShiftClick() ? 0.0625 : 1, 0, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(24, new SWItem(Material.PAPER, "§eX§8:§7 " + tnt.getPosition().getX(), clickType -> {
            new SimulatorAnvilGui<>(player, "X", tnt.getPosition().getX() + "", Double::parseDouble, d -> {
                tnt.getPosition().setX(d);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).open();
        }));
        inventory.setItem(33, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-0.0625"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.move(clickType.isShiftClick() ? -0.0625 : -1, 0, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        // Pos Y
        inventory.setItem(16, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList("§7Shift§8: §e+0.0625"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.move(0, clickType.isShiftClick() ? 0.0625 : 1, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(25, new SWItem(Material.PAPER, "§eY§8:§7 " + tnt.getPosition().getY(), clickType -> {
            new SimulatorAnvilGui<>(player, "Y", tnt.getPosition().getY() + "", Double::parseDouble, d -> {
                tnt.getPosition().setY(d);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).open();
        }));
        inventory.setItem(34, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-0.0625"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.move(0, clickType.isShiftClick() ? -0.0625 : -1, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        // Pos Z
        inventory.setItem(17, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList("§7Shift§8: §e+0.0625"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.move(0, 0, clickType.isShiftClick() ? 0.0625 : 1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(26, new SWItem(Material.PAPER, "§eZ§8:§7 " + tnt.getPosition().getZ(), clickType -> {
            new SimulatorAnvilGui<>(player, "Z", tnt.getPosition().getZ() + "", Double::parseDouble, d -> {
                tnt.getPosition().setZ(d);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).open();
        }));
        inventory.setItem(35, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-0.0625"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            tnt.move(0, 0, clickType.isShiftClick() ? -0.0625 : -1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));
    }
}
