/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2022  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.util;

import de.steamwar.lobby.particle.ParticleEnum;
import de.steamwar.lobby.particle.ParticleInventory;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserConfig;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class LobbyPlayer {

    private static Map<UUID, LobbyPlayer> cache = new HashMap<>();

    private ParticleEnum particle;
    private int userId;

    private boolean fly;

    private LobbyPlayer(UUID uuid) {
        userId = SteamwarUser.get(uuid).getId();
        cache.put(uuid, this);
        particle = null;
        String saved = UserConfig.getConfig(userId, "lobby-particle");
        if (saved != null) {
            particle = ParticleInventory.convertFromString(saved);
        }
    }

    public boolean isFlying() {
        return fly;
    }

    public void setFly(boolean fly) {
        this.fly = fly;
    }

    public ParticleEnum getParticle() {
        return particle;
    }

    public void setParticle(ParticleEnum particle) {
        if (particle == null) {
            UserConfig.removePlayerConfig(userId, "lobby-particle");
        } else {
            String saved = ParticleInventory.convertToString(particle);
            UserConfig.updatePlayerConfig(userId, "lobby-particle", saved);
        }
        this.particle = particle;
    }

    public static LobbyPlayer getLobbyPlayer(UUID uuid) {
        LobbyPlayer lobbyPlayer = cache.get(uuid);
        return lobbyPlayer == null ? new LobbyPlayer(uuid) : lobbyPlayer;
    }

    public static LobbyPlayer getLobbyPlayer(Player player) {
        return getLobbyPlayer(player.getUniqueId());
    }
}
