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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.gui.BauGUI;
import de.steamwar.bausystem.linkage.BauGuiItem;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import yapion.hierarchy.types.YAPIONObject;

import java.util.*;

public class BauGuiMapping {

    private static final HashMap<UUID, BauGuiMapping> fromUUID = new HashMap<>();
    private static final List<BauGuiMapping> mappings = new ArrayList<>();

    private static BukkitTask task;

    public static void startWatchdog() {
        if (task == null) {
            Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), bukkitTask -> {
                mappings.forEach(BauGuiMapping::tick);
                if (BauGuiEditor.getOpen_Edits().isEmpty()) {
                    bukkitTask.cancel();
                    task = null;
                }
            }, 1, 1);
        }
    }

    @Getter
    private final YAPIONObject object;
    @Getter
    private final Player owner;
    @Getter
    @Setter
    private boolean saved;

    protected BauGuiMapping(YAPIONObject object, Player p) {
        this.object = object;
        this.owner = p;
        fromUUID.put(p.getUniqueId(), this);
        mappings.add(this);
    }

    public static BauGuiMapping getGuiMapping(Player p) {
        BauGuiMapping mapping = fromUUID.get(p.getUniqueId());

        if (mapping == null) {
            YAPIONObject yapionObject = Config.getInstance().get(p);
            mapping = new BauGuiMapping(yapionObject.getObject("baugui"), p);
        }

        return mapping;
    }

    public boolean isShown(int id) {
        return object.getPlainValueOrDefault(Integer.toString(id), -1) >= 0;
    }

    public int getSlot(int id) {
        return object.getPlainValue(Integer.toString(id));
    }

    public Map<Integer, Integer> getMapping() {
        return internalReadMap();
    }

    public void setMapping(Map<Integer, Integer> mapping) {
        internalWriteMap(mapping);
    }

    public int getSize() {
        return object.getPlainValueOrDefault("size", 45);
    }

    public void setSize(int size) {
        if (size % 9 != 0) {
            return;
        }

        object.add("size", size);
    }

    private void tick() {
        this.saved = false;
    }

    public void save() {
        if (saved) return;
        YAPIONObject config = Config.getInstance().get(owner);
        config.add("baugui", object);
        Config.getInstance().save(owner);
        saved = true;
    }

    private Map<Integer, Integer> internalReadMap() {
        Map<Integer, Integer> map = new HashMap<>();
        for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
            Integer value = object.getPlainValueOrDefault(e.getKey().toString(), -1);
            if (value == -1) continue;
            map.put(e.getKey(), value);
        }
        return map;
    }

    private void internalWriteMap(Map<Integer, Integer> map) {
        for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
            object.remove(e.getKey().toString());
            Integer value = map.getOrDefault(e.getKey(), -1);
            if (value == -1) continue;
            object.add(e.getKey().toString(), value);
        }
    }
}
