/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023  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.redstone.RedstoneElement;
import de.steamwar.bausystem.features.simulator.data.redstone.RedstonePhase;
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.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.Arrays;

public class SimulatorRedstonePhaseSettingsGui extends SimulatorBaseGui {
    private final RedstoneElement redstoneElement;
    private final RedstonePhase redstone;
    private final SimulatorBaseGui back;

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

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

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

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

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

        // Delete
        inventory.setItem(8, new SWItem(Material.BARRIER, "§eDelete", clickType -> {
            redstoneElement.getPhases().remove(redstone);
            back.open();
            SimulatorWatcher.update(simulator);
        }));

        int index = redstoneElement.getPhases().indexOf(redstone);
        int min;
        if (index > 0) {
            RedstonePhase previous = redstoneElement.getPhases().get(index - 1);
            min = previous.getTickOffset() + previous.getLifetime() + 1;
        } else {
            min = 0;
        }

        int maxLifetime;
        int maxOffset;
        if (index < redstoneElement.getPhases().size() - 1) {
            RedstonePhase next = redstoneElement.getPhases().get(index + 1);
            maxLifetime = next.getTickOffset() - redstone.getTickOffset() - 1;
            maxOffset = next.getTickOffset() - redstone.getLifetime() - 1;
        } else {
            maxLifetime = Integer.MAX_VALUE - 5;
            maxOffset = Integer.MAX_VALUE - 5;
        }

        //Tick Offset
        int offset = redstone.getTickOffset();
        inventory.setItem(10, SWItem.getDye(offset < maxOffset ? 10 : 8), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            redstone.setTickOffset(Math.min(maxOffset, offset + (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        });

        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;
                redstone.setTickOffset(Math.min(Math.max(integer, min), maxOffset));
                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, SWItem.getDye(offset > min ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            redstone.setTickOffset(Math.max(min, offset - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        });

        //Lifetime
        int lifetime = redstone.getLifetime();
        inventory.setItem(11, SWItem.getDye(lifetime < maxLifetime ? 10 : 8), "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            redstone.setLifetime(Math.min(maxLifetime, lifetime + (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        });

        SWItem lifetimeItem = new SWItem(Material.CLOCK, "§eActivation Time§8:§7 " + lifetime, clickType -> {
            new SimulatorAnvilGui<>(player, "Activation Time", lifetime + "", Integer::parseInt, integer -> {
                if (integer < 0) return false;
                redstone.setLifetime(Math.min(integer, maxLifetime));
                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, SWItem.getDye(lifetime > 0 ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            redstone.setLifetime(Math.max(0, lifetime - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        });

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

        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;
                redstone.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, SWItem.getDye(order > -SimulatorPhase.ORDER_LIMIT ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
            redstone.setOrder(Math.max(-SimulatorPhase.ORDER_LIMIT, order - (clickType.isShiftClick() ? 5 : 1)));
            SimulatorWatcher.update(simulator);
        });
    }
}
