/*
 *   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.redstone.RedstoneElement;
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.Arrays;

public class SimulatorRedstoneSettingsGui extends SimulatorBaseGui {
    private final RedstoneElement redstone;
    private final SimulatorBaseGui back;

    public SimulatorRedstoneSettingsGui(Player player, Simulator simulator, RedstoneElement redstone, SimulatorBaseGui back) {
        super(player, simulator, 5 * 9);
        this.redstone = redstone;
        this.back = back;
    }

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

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

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

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

        // Base Tick
        int baseTicks = redstone.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;
            redstone.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;
                redstone.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) {
                redstone.changeBaseTicks(-baseTicks);
            } else {
                redstone.changeBaseTicks(clickType.isShiftClick() ? -5 : -1);
            }
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        //Pos X
        inventory.setItem(15, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            redstone.move(clickType.isShiftClick() ? 5 : 1, 0, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(24, new SWItem(Material.PAPER, "§eX§8:§7 " + redstone.getPosition().getBlockX(), clickType -> {
            new SimulatorAnvilGui<>(player, "X", redstone.getPosition().getBlockX() + "", Integer::parseInt, i -> {
                redstone.getPosition().setX(i);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).open();
        }));
        inventory.setItem(33, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            redstone.move(clickType.isShiftClick() ? -5 : -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+5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            redstone.move(0, clickType.isShiftClick() ? 5 : 1, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(25, new SWItem(Material.PAPER, "§eY§8:§7 " + redstone.getPosition().getBlockY(), clickType -> {
            new SimulatorAnvilGui<>(player, "Y", redstone.getPosition().getBlockY() + "", Integer::parseInt, i -> {
                redstone.getPosition().setY(i);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).open();
        }));
        inventory.setItem(34, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            redstone.move(0, clickType.isShiftClick() ? -5 : -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+5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            redstone.move(0, 0, clickType.isShiftClick() ? 5 : 1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(26, new SWItem(Material.PAPER, "§eZ§8:§7 " + redstone.getPosition().getBlockZ(), clickType -> {
            new SimulatorAnvilGui<>(player, "Z", redstone.getPosition().getBlockZ() + "", Integer::parseInt, i -> {
                redstone.getPosition().setZ(i);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).open();
        }));
        inventory.setItem(35, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            if (clickType == ClickType.DOUBLE_CLICK) return;
            redstone.move(0, 0, clickType.isShiftClick() ? -5 : -1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));
    }
}
