/*
 *   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.gui;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.FlatteningWrapper;
import de.steamwar.bausystem.tracer.show.ShowModeParameter;
import de.steamwar.bausystem.tracer.show.TraceShowManager;
import de.steamwar.bausystem.tracer.show.mode.EntityShowMode;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class GuiTraceShow {

    private static final Map<Player, ShowModeParameter> ShowModeParameterMap = new HashMap<>();

    private GuiTraceShow() {

    }

    public static void openGui(Player player) {
        ShowModeParameter playerShowMode = new ShowModeParameter();
        playerShowMode.setInterpolate_Y(false);
        playerShowMode.setInterpolate_XZ(false);
        ShowModeParameterMap.put(player, playerShowMode);

        SWInventory swInventory = new SWInventory(player, 9, "Trace Show GUI");
        swInventory.addCloseCallback(clickType -> ShowModeParameterMap.remove(player));
        setActiveShow(player, swInventory);

        SWItem water = new SWItem(Material.TNT, "§eWasser §7Positionen", Arrays.asList("§7Zeigt alles TNT, welches", "§7im Wasser explodiert ist."), false, clickType -> {
        });
        swInventory.setItem(5, water);
        swInventory.setCallback(5, clickType -> toggleHideTNTinWaterExploded(player, swInventory, water));

        SWItem interpolateY = new SWItem(Material.QUARTZ_STAIRS, "§eInterpolation §7Y-Achse", Arrays.asList("§7Zeigt die Interpolation", "§7auf der Y-Achse."), false, clickType -> {
        });
        swInventory.setItem(6, interpolateY);
        swInventory.setCallback(6, clickType -> toggleInterpolateYPosition(player, swInventory, interpolateY));

        Material xzMaterial = FlatteningWrapper.impl.getTraceXZMaterial();
        SWItem interpolateXZ = new SWItem(xzMaterial, (byte) 7, "§eInterpolation §7XZ-Achse", Arrays.asList("§7Zeigt die Interpolation", "§7auf der XZ-Achse."), false, clickType -> {
        });
        swInventory.setItem(7, interpolateXZ);
        swInventory.setCallback(7, clickType -> toggleInterpolateXZPosition(player, swInventory, interpolateXZ));
        // Water Bucket (-water)
        // TNT (-water-exploded)
        // Quartz_Stair (-interpolate-y)
        // Quartz_Slab (-interpolate-xz)
        swInventory.open();
    }

    private static void setActiveShow(Player player, SWInventory swInventory) {
        if (TraceShowManager.hasActiveShow(player)) {
            Material showMaterial = FlatteningWrapper.impl.getTraceShowMaterial();
            SWItem shown = new SWItem(showMaterial, (byte) 5, "§aTraces angezeigt", new ArrayList<>(), false, clickType -> {
                TraceShowManager.hide(player);
                player.sendMessage(BauSystem.PREFIX + "§cAlle TNT-Positionen ausgeblendet");
                setActiveShow(player, swInventory);
            });
            swInventory.setItem(1, shown);
        } else {
            Material hideMaterial = FlatteningWrapper.impl.getTraceHideMaterial();
            SWItem hidden = new SWItem(hideMaterial, (byte) 14, "§cTraces ausgeblendet", new ArrayList<>(), false, clickType -> {
                show(player);
                player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen angezeigt");
                setActiveShow(player, swInventory);
            });
            swInventory.setItem(1, hidden);
        }
    }

    private static void toggleHideTNTinWaterExploded(Player player, SWInventory swInventory, SWItem swItem) {
        ShowModeParameter showModeParameter = ShowModeParameterMap.get(player);
        showModeParameter.setWater(!showModeParameter.isWater());
        show(player);

        swItem.setEnchanted(showModeParameter.isWater());
        swInventory.setItem(5, swItem);
        swInventory.setCallback(5, clickType -> toggleHideTNTinWaterExploded(player, swInventory, swItem));
    }

    private static void toggleInterpolateYPosition(Player player, SWInventory swInventory, SWItem swItem) {
        ShowModeParameter showModeParameter = ShowModeParameterMap.get(player);
        showModeParameter.setInterpolate_Y(!showModeParameter.isInterpolate_Y());
        show(player);

        swItem.setEnchanted(showModeParameter.isInterpolate_Y());
        swInventory.setItem(6, swItem);
        swInventory.setCallback(6, clickType -> toggleInterpolateYPosition(player, swInventory, swItem));
    }

    private static void toggleInterpolateXZPosition(Player player, SWInventory swInventory, SWItem swItem) {
        ShowModeParameter showModeParameter = ShowModeParameterMap.get(player);
        showModeParameter.setInterpolate_XZ(!showModeParameter.isInterpolate_XZ());
        show(player);

        swItem.setEnchanted(showModeParameter.isInterpolate_XZ());
        swInventory.setItem(7, swItem);
        swInventory.setCallback(7, clickType -> toggleInterpolateXZPosition(player, swInventory, swItem));
    }

    private static void show(Player player) {
        TraceShowManager.show(player, new EntityShowMode(player, ShowModeParameterMap.get(player)));
    }

}
