/*
 *   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.SimulatorGroup;
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 java.util.Arrays;

public class SimulatorGroupSettingsGui extends SimulatorBaseGui {

    private final SimulatorGroup simulatorGroup;
    private final SimulatorBaseGui back;

    public SimulatorGroupSettingsGui(Player player, Simulator simulator, SimulatorGroup simulatorGroup, SimulatorBaseGui back) {
        super(player, simulator, 5 * 9);
        this.simulatorGroup = simulatorGroup;
        this.back = back;
    }

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

    @Override
    public void populate() {
        if (simulatorGroup.getElements().size() < 2) {
            back.open();
            return;
        }

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

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

        // Base Tick
        int baseTicks = simulatorGroup.getBaseTick();
        inventory.setItem(9, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList("§7Shift§8: §e+5"), false, clickType -> {
            simulatorGroup.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;
                simulatorGroup.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 (baseTicks - (clickType.isShiftClick() ? 5 : 1) < 0) {
                simulatorGroup.changeBaseTicks(-baseTicks);
            } else {
                simulatorGroup.changeBaseTicks(clickType.isShiftClick() ? -5 : -1);
            }
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));

        boolean allTNT = simulatorGroup.getElements().stream().allMatch(TNTElement.class::isInstance);

        if (allTNT) {
            // Subpixel Alignment
            inventory.setItem(21, new SWItem(Material.SUNFLOWER, "§7Align§8: §eCenter", clickType -> {
                simulatorGroup.getElements().stream()
                        .map(TNTElement.class::cast)
                        .forEach(tntElement -> {
                            tntElement.alignX(0);
                            tntElement.alignZ(0);
                        });
                SimulatorWatcher.update(simulator);
            }));

            // Z
            SWItem negativZItem = new SWItem(Material.OAK_BUTTON, "§7Align§8: §eNegativ Z", clickType -> {
                simulatorGroup.getElements().stream()
                        .map(TNTElement.class::cast)
                        .forEach(tntElement -> {
                            if (tntElement.getAlignment().getZ() != -1) tntElement.alignZ(-1);
                        });
                SimulatorWatcher.update(simulator);
            });
            negativZItem.setEnchanted(simulatorGroup.getElements().stream()
                    .map(TNTElement.class::cast)
                    .allMatch(tntElement -> tntElement.getAlignment().getZ() == -1));
            inventory.setItem(20, negativZItem);

            SWItem positivZItem = new SWItem(Material.OAK_BUTTON, "§7Align§8: §ePositiv Z", clickType -> {
                simulatorGroup.getElements().stream()
                        .map(TNTElement.class::cast)
                        .forEach(tntElement -> {
                            if (tntElement.getAlignment().getZ() != 1) tntElement.alignZ(1);
                        });
                SimulatorWatcher.update(simulator);
            });
            positivZItem.setEnchanted(simulatorGroup.getElements().stream()
                    .map(TNTElement.class::cast)
                    .allMatch(tntElement -> tntElement.getAlignment().getZ() == 1));
            inventory.setItem(22, positivZItem);

            // X
            SWItem negativXItem = new SWItem(Material.STONE_BUTTON, "§7Align§8: §eNegativ X", clickType -> {
                simulatorGroup.getElements().stream()
                        .map(TNTElement.class::cast)
                        .forEach(tntElement -> {
                            if (tntElement.getAlignment().getX() != -1) tntElement.alignX(-1);
                        });

                SimulatorWatcher.update(simulator);
            });
            negativXItem.setEnchanted(simulatorGroup.getElements().stream()
                    .map(TNTElement.class::cast)
                    .allMatch(tntElement -> tntElement.getAlignment().getX() == -1));
            inventory.setItem(12, negativXItem);

            SWItem positivXItem = new SWItem(Material.STONE_BUTTON, "§7Align§8: §ePositiv X", clickType -> {
                simulatorGroup.getElements().stream()
                        .map(TNTElement.class::cast)
                        .forEach(tntElement -> {
                            if (tntElement.getAlignment().getX() != 1) tntElement.alignX(1);
                        });
                SimulatorWatcher.update(simulator);
            });
            positivXItem.setEnchanted(simulatorGroup.getElements().stream()
                    .map(TNTElement.class::cast)
                    .allMatch(tntElement -> tntElement.getAlignment().getX() == -1));
            inventory.setItem(30, positivXItem);
        }

        //Pos X
        inventory.setItem(15, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList(allTNT ? "§7Shift§8: §e+0.0625" : "§7Shift§8: §e+5"), false, clickType -> {
            simulatorGroup.move(clickType.isShiftClick() ? (allTNT ? 0.0625 : 5) : 1, 0, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(24, new SWItem(Material.PAPER, "§eX", clickType -> {
            new SimulatorAnvilGui<>(player, "Relative X", "", Double::parseDouble, number -> {
                if (!allTNT) {
                    number = (double) Math.round(number);
                }
                simulatorGroup.move(number, 0, 0);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.PAPER).open();
        }));
        inventory.setItem(33, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList(allTNT ? "§7Shift§8: §e-0.0625" : "§7Shift§8: §e-5"), false, clickType -> {
            simulatorGroup.move(clickType.isShiftClick() ? (allTNT ? -0.0625 : -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(allTNT ? "§7Shift§8: §e+0.0625" : "§7Shift§8: §e+5"), false, clickType -> {
            simulatorGroup.move(0, clickType.isShiftClick() ? (allTNT ? 0.0625 : 5) : 1, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(25, new SWItem(Material.PAPER, "§eY", clickType -> {
            new SimulatorAnvilGui<>(player, "Relative Y", "", Double::parseDouble, number -> {
                if (!allTNT) {
                    number = (double) Math.round(number);
                }
                simulatorGroup.move(0, number, 0);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.PAPER).open();
        }));
        inventory.setItem(34, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList(allTNT ? "§7Shift§8: §e-0.0625" : "§7Shift§8: §e-5"), false, clickType -> {
            simulatorGroup.move(0, clickType.isShiftClick() ? (allTNT ? -0.0625 : -5) : -1, 0);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.ENABLED_OR_DISABLED));

        //Pos Z
        inventory.setItem(17, new SWItem(Material.LIME_DYE, "§e+1", Arrays.asList(allTNT ? "§7Shift§8: §e+0.0625" : "§7Shift§8: §e+5"), false, clickType -> {
            simulatorGroup.move(0, 0, clickType.isShiftClick() ? (allTNT ? 0.0625 : 5) : 1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.INCREMENT_OR_DISABLED));
        inventory.setItem(26, new SWItem(Material.PAPER, "§eZ", clickType -> {
            new SimulatorAnvilGui<>(player, "Relative Z", "", Double::parseDouble, number -> {
                if (!allTNT) {
                    number = (double) Math.round(number);
                }
                simulatorGroup.move(0, 0, number);
                SimulatorWatcher.update(simulator);
                return true;
            }, this).setItem(Material.PAPER).open();
        }));
        inventory.setItem(35, new SWItem(Material.RED_DYE, "§e-1", Arrays.asList(allTNT ? "§7Shift§8: §e-0.0625" : "§7Shift§8: §e-5"), false, clickType -> {
            simulatorGroup.move(0, 0, clickType.isShiftClick() ? (allTNT ? -0.0625 : -5) : -1);
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DECREMENT_OR_DISABLED));
    }
}
