/*
 *   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 de.steamwar.sql.internal.Field;
import de.steamwar.sql.internal.SelectStatement;
import de.steamwar.sql.internal.Statement;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

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

@AllArgsConstructor
public class PersonalKit {

    private static final Table<PersonalKit> table = new Table<>(PersonalKit.class);
    private static final SelectStatement<PersonalKit> getKits = table.selectFields("UserID", "GameMode");
    private static final SelectStatement<PersonalKit> getKit = table.select(Table.PRIMARY);
    private static final SelectStatement<PersonalKit> getKitInUse = table.selectFields("UserID", "GameMode", "InUse");
    private static final Statement update = table.insertAll();
    private static final Statement delete = table.delete(Table.PRIMARY);

    @Getter
    @Field(keys = {Table.PRIMARY})
    private final int userID;
    @Getter
    @Field(keys = {Table.PRIMARY})
    private final String gamemode;
    @Getter
    @Field(keys = {Table.PRIMARY})
    private final String name;
    @Field
    private String inventory;
    @Field
    private String armor;
    @Getter
    @Field(def = "1")
    private boolean inUse;

    public String getRawInventory() {
        return inventory;
    }

    public String getRawArmor() {
        return armor;
    }

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

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

    public void setInUse() {
        PersonalKit kit = getKitInUse(userID, gamemode);
        if(kit != null)
            kit.setUse(false);
        setUse(true);
    }

    private void setUse(boolean inUse) {
        this.inUse = inUse;
        update();
    }

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

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

    public void setContainer(ItemStack[] inventory, ItemStack[] armor) {
        this.armor = saveInvConfig("Armor", armor);
        this.inventory = saveInvConfig("Inventory", inventory);
        update();
    }

    public void delete() {
        delete.update(userID, gamemode, name);
    }

    private void update() {
        update.update(userID, gamemode, name, inventory, armor, inUse);
    }

    public static List<PersonalKit> get(int userID, String gamemode){
        return getKits.listSelect(userID, gamemode);
    }

    public static PersonalKit get(int userID, String gamemode, String name) {
        return getKit.select(userID, gamemode, name);
    }

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

    public static PersonalKit getKitInUse(int userID, String gamemode) {
        return getKitInUse.select(userID, gamemode, true);
    }

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

        return armorConfig.saveToString();
    }
}
