/*
 *   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.observer.ObserverElement;
import de.steamwar.bausystem.features.simulator.data.observer.ObserverPhase;
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.block.BlockFace;
import org.bukkit.entity.Player;

import java.util.Arrays;

public class SimulatorObserverPhaseSettingsGui extends SimulatorBaseGui {

    private final ObserverElement observerElement;
    private final ObserverPhase observer;
    private final SimulatorBaseGui back;

    public SimulatorObserverPhaseSettingsGui(Player player, Simulator simulator, ObserverElement observerElement, ObserverPhase observer, SimulatorBaseGui back) {
        super(player, simulator, 5 * 9);
        this.observerElement = observerElement;
        this.observer = observer;
        this.back = back;
    }

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

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

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

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

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

        int index = observerElement.getPhases().indexOf(observer);
        int min;
        if (index > 0) {
            ObserverPhase previous = observerElement.getPhases().get(index - 1);
            min = previous.getTickOffset() + 4;
        } else {
            min = 0;
        }

        int max;
        if (index < observerElement.getPhases().size() - 1) {
            ObserverPhase next = observerElement.getPhases().get(index + 1);
            max = next.getTickOffset() - 4;
        } else {
            max = Integer.MAX_VALUE - 4;
        }

        //Tick Offset
        int offset = observer.getTickOffset();
        inventory.setItem(10, new SWItem(SWItem.getDye(offset < max ? 10 : 8), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            observer.setTickOffset(Math.min(max, 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;
                observer.setTickOffset(Math.min(Math.max(integer, min), max));
                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 > min ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            observer.setTickOffset(Math.max(min, offset - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        //Order
        int order = observer.getOrder();
        inventory.setItem(13, new SWItem(SWItem.getDye(order < SimulatorPhase.ORDER_LIMIT ? 10 : 8), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            observer.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, "§eActivation Order§8:§7 " + order, clickType -> {
            new SimulatorAnvilGui<>(player, "Activation Order", order + "", Integer::parseInt, integer -> {
                if (integer < -SimulatorPhase.ORDER_LIMIT) return false;
                if (integer > SimulatorPhase.ORDER_LIMIT) return false;
                observer.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 -> {
            observer.setOrder(Math.max(-SimulatorPhase.ORDER_LIMIT, order - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        // Update orientation
        inventory.setItem(25, new SWItem(Material.SUNFLOWER, "§7", clickType -> {
        }));
        inventory.setItem(15, new SWItem(observer.getOrientation() == BlockFace.UP ? Material.LIME_CONCRETE : Material.GRAY_CONCRETE, "§eUp", clickType -> {
            observer.setOrientation(BlockFace.UP);
            SimulatorWatcher.update(simulator);
        }));
        inventory.setItem(33, new SWItem(observer.getOrientation() == BlockFace.DOWN ? Material.RED_CONCRETE : Material.GRAY_CONCRETE, "§eDown", clickType -> {
            observer.setOrientation(BlockFace.DOWN);
            SimulatorWatcher.update(simulator);
        }));
        inventory.setItem(16, new SWItem(observer.getOrientation() == BlockFace.NORTH ? Material.LIME_WOOL : Material.GRAY_WOOL, "§eNorth", clickType -> {
            observer.setOrientation(BlockFace.NORTH);
            SimulatorWatcher.update(simulator);
        }));
        inventory.setItem(34, new SWItem(observer.getOrientation() == BlockFace.SOUTH ? Material.RED_WOOL : Material.GRAY_WOOL, "§eSouth", clickType -> {
            observer.setOrientation(BlockFace.SOUTH);
            SimulatorWatcher.update(simulator);
        }));
        inventory.setItem(24, new SWItem(observer.getOrientation() == BlockFace.EAST ? Material.LIME_STAINED_GLASS : Material.GRAY_STAINED_GLASS, "§eEast", clickType -> {
            observer.setOrientation(BlockFace.EAST);
            SimulatorWatcher.update(simulator);
        }));
        inventory.setItem(26, new SWItem(observer.getOrientation() == BlockFace.WEST ? Material.RED_STAINED_GLASS : Material.GRAY_STAINED_GLASS, "§eWest", clickType -> {
            observer.setOrientation(BlockFace.WEST);
            SimulatorWatcher.update(simulator);
        }));
    }
}
