/*
 *   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.observer.ObserverElement;
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 SimulatorObserverSettingsGui extends SimulatorBaseGui {

    private final ObserverElement observer;
    private final SimulatorBaseGui back;

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

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

    @Override
    public void populate() {
        if (observer.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, observer.toItem(player, clickType -> {
            new SimulatorMaterialGui(player, simulator, observer::getMaterial, observer::setMaterial, this).open();
        }));

        // Base Tick
        int baseTicks = observer.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;
            observer.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;
                observer.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) {
                observer.changeBaseTicks(-baseTicks);
            } else {
                observer.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;
            observer.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 " + observer.getPosition().getBlockX(), clickType -> {
            new SimulatorAnvilGui<>(player, "X", observer.getPosition().getBlockX() + "", Integer::parseInt, i -> {
                observer.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;
            observer.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;
            observer.move(0, clickType.isShiftClick() ? 5 : 1, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.ENABLED_OR_DISABLED));
        inventory.setItem(25, new SWItem(Material.PAPER, "§eY§8:§7 " + observer.getPosition().getBlockY(), clickType -> {
            new SimulatorAnvilGui<>(player, "Y", observer.getPosition().getBlockY() + "", Integer::parseInt, i -> {
                observer.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;
            observer.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;
            observer.move(0, 0, clickType.isShiftClick() ? 5 : 1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.ENABLED_OR_DISABLED));
        inventory.setItem(26, new SWItem(Material.PAPER, "§eZ§8:§7 " + observer.getPosition().getBlockZ(), clickType -> {
            new SimulatorAnvilGui<>(player, "Z", observer.getPosition().getBlockZ() + "", Integer::parseInt, i -> {
                observer.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;
            observer.move(0, 0, clickType.isShiftClick() ? -5 : -1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));
    }
}
