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

import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.core.Core;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.hierarchy.types.YAPIONValue;
import yapion.parser.YAPIONParser;
import yapion.serializing.YAPIONDeserializer;
import yapion.serializing.YAPIONSerializer;

import java.util.HashSet;
import java.util.Set;

@UtilityClass
public class DefaultHotbar {

    private final YAPIONArray DEFAULT_HOTBAR = YAPIONParser.parse("{[{@type(org.bukkit.inventory.ItemStack)v(2230)type(WOODEN_AXE)meta{@type(org.bukkit.inventory.meta.ItemMeta)meta-type(UNSPECIFIC)display-name(WorldEdit Wand)lore{@type(java.util.ArrayList)values[Left click: select pos #1,Right click: select pos #2]}}},{@type(org.bukkit.inventory.ItemStack)v(2230)type(COMPASS)meta{@type(org.bukkit.inventory.meta.ItemMeta)meta-type(UNSPECIFIC)display-name(Navigation Wand)lore{@type(java.util.ArrayList)values[Left click: jump to location,Right click: pass through walls]}}},null,null,null,null,null,null,{@type(org.bukkit.inventory.ItemStack)v(2230)type(NETHER_STAR)meta{@type(org.bukkit.inventory.meta.ItemMeta)meta-type(UNSPECIFIC)display-name(§eBau GUI)}},null,null,{@type(org.bukkit.inventory.ItemStack)v(3117)type(ELYTRA)},null]}").getArray("");

    public void updateHotbar(Player p) {
        ItemStack[] hotbar = new ItemStack[13];
        System.arraycopy(p.getInventory().getContents(), 0, hotbar, 0, 9);
        System.arraycopy(p.getInventory().getArmorContents(), 0, hotbar, 9, 4);
        YAPIONArray yapionArray = new YAPIONArray();
        for (ItemStack itemStack : hotbar) {
            if (itemStack != null) {
                yapionArray.add(YAPIONSerializer.serialize(itemStack));
            } else {
                yapionArray.add(new YAPIONValue<>(null));
            }
        }
        Config.getInstance().get(p).add("hotbar-" + Core.getVersion(), yapionArray);
        Config.getInstance().save(p);
    }

    public void setHotbar(Player p) {
        ItemStack[] hotbar = getItems(p);
        ItemStack[] inv = p.getInventory().getContents();
        ItemStack[] armor = p.getInventory().getArmorContents();
        System.arraycopy(hotbar, 0, inv, 0, 9);
        System.arraycopy(hotbar, 9, armor, 0, 4);
        p.getInventory().setContents(inv);
        p.getInventory().setArmorContents(armor);
    }

    public ItemStack[] getItems(Player p) {
        Config.getInstance().get(p).remap("hotbar", "hotbar-19");
        YAPIONArray yapionArray = Config.getInstance().get(p).getYAPIONArrayOrSetDefault("hotbar-" + Core.getVersion(), defaultHotbar());
        ItemStack[] hotbar = new ItemStack[13];
        Set<Integer> invalid = new HashSet<>();
        yapionArray.forEach((integer, yapionAnyType) -> {
            if (yapionAnyType instanceof YAPIONValue) {
                hotbar[integer] = null;
            } else {
                try {
                    hotbar[integer] = YAPIONDeserializer.deserialize((YAPIONObject) yapionAnyType);
                } catch (Exception e) {
                    invalid.add(integer);
                    hotbar[integer] = null;
                }
            }
        });
        invalid.forEach(i -> yapionArray.set(i, new YAPIONValue<>(null)));
        if (!invalid.isEmpty()) Config.getInstance().save(p);
        return hotbar;
    }

    public YAPIONArray defaultHotbar() {
        return DEFAULT_HOTBAR;
    }
}
