/*
 *   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.lobby.special.easter;

import de.steamwar.inventory.SWItem;
import de.steamwar.lobby.LobbySystem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.util.Vector;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

public class Egg {

    private static final World world = Bukkit.getWorlds().get(0);

    private final int x;
    private final int y;
    private final int z;
    private final String message;
    private final EggDifficulty difficulty;

    private Optional<PlayerProfile> playerProfile = null;

    public Egg(Map<String, ?> config) {
        this.x = (int) config.get("x");
        this.y = (int) config.get("y");
        this.z = (int) config.get("z");
        this.message = (String) config.get("name");
        this.difficulty = EggDifficulty.valueOf(((String) config.get("difficulty")).toUpperCase());
    }

    public Vector getVector() {
        return new Vector(x, y, z);
    }

    public String getMessage() {
        return message;
    }

    public EggDifficulty getDifficulty() {
        return difficulty;
    }

    public Block getBlock() {
        Block block = world.getBlockAt(x, y, z);
        if (block.getType() != Material.PLAYER_HEAD && block.getType() != Material.PLAYER_WALL_HEAD) {
            System.out.println("Block is not a skull: " + block.getType() + " " + x + "," + y + "," + z);
            return null;
        }
        return block;
    }

    public SWItem getItem(Player player, boolean found) {
        if (playerProfile == null) {
            Block block = getBlock();
            if (block == null) {
                playerProfile = Optional.empty();
                return null;
            }

            Skull skull = (Skull) block.getState();
            PlayerProfile playerProfile = skull.getOwnerProfile();
            if (playerProfile == null) {
                this.playerProfile = Optional.empty();
                return null;
            }
            this.playerProfile = Optional.of(playerProfile);
        }

        if (!playerProfile.isPresent()) {
            return null;
        }

        SWItem swItem;
        if (found) {
            swItem = new SWItem();
            ItemStack itemStack = new ItemStack(Material.PLAYER_HEAD);
            SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta();
            skullMeta.setOwnerProfile(playerProfile.get());
            itemStack.setItemMeta(skullMeta);
            swItem.setItemStack(itemStack);
        } else {
            swItem = SWItem.getPlayerSkull("MHF_Question");
        }

        swItem.setLore(Arrays.asList(LobbySystem.getMessage().parse(difficulty.getMessage(), player)));
        try {
            swItem.setName("§f" + LobbySystem.getMessage().parse(message, player));
        } catch (Exception e) {
            swItem.setName("§f" + message);
        }
        return swItem;
    }
}
