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

import lombok.AllArgsConstructor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

import java.io.StringReader;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@AllArgsConstructor
public class PersonalKit {
    private final InternalKit kit;

    public String getName() {
        return kit.getName();
    }

    public boolean isInUse() {
        return kit.getInUse();
    }

    public String getRawInventory() {
        return kit.getRawInventory();
    }

    public String getRawArmor() {
        return kit.getRawArmor();
    }

    public ItemStack[] getInventory() {
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(getRawInventory()));
        return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]);
    }

    public ItemStack[] getArmor() {
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(getRawArmor()));
        return Objects.requireNonNull(config.getList("Armor")).toArray(new ItemStack[4]);
    }

    public void setInUse() {
        kit.setDefault();
    }

    public void setInventory(ItemStack[] inventory) {
        kit.setInventory(saveInvConfig("Inventory", inventory));
    }

    public void setArmor(ItemStack[] armor) {
        kit.setArmor(saveInvConfig("Armor", armor));
    }

    public void setContainer(ItemStack[] inventory, ItemStack[] armor) {
        setInventory(inventory);
        setArmor(armor);
    }

    public void delete() {
        kit.delete();
    }

    public static List<PersonalKit> get(int userID, String gamemode) {
        return InternalKit.get(userID, gamemode).stream().map(PersonalKit::new).collect(Collectors.toList());
    }

    public static PersonalKit get(int userID, String gamemode, String name) {
        InternalKit kit = InternalKit.get(userID, gamemode, name);
        if (kit == null) return null;
        return new PersonalKit(kit);
    }

    public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor) {
        InternalKit kit = InternalKit.create(userID, gamemode, name, saveInvConfig("Inventory", inventory), saveInvConfig("Armor", armor));
        return new PersonalKit(kit);
    }

    public static PersonalKit getKitInUse(int userID, String gamemode) {
        InternalKit kit = InternalKit.getKitInUse(userID, gamemode);
        if (kit == null) return null;
        return new PersonalKit(kit);
    }

    private static String saveInvConfig(String name, ItemStack[] inv) {
        YamlConfiguration armorConfig = new YamlConfiguration();
        armorConfig.set(name, inv);

        return armorConfig.saveToString();
    }
}
