package cn.citycraft.AuthMe.cache.limbo;

import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.ConsoleLogger;
import cn.citycraft.AuthMe.cache.backup.DataFileCache;
import cn.citycraft.AuthMe.cache.backup.JsonCache;
import cn.citycraft.AuthMe.settings.Settings;

public class LimboCache {
    private volatile static LimboCache singleton;
    public ConcurrentHashMap<String, LimboPlayer> cache;
    public AuthMe plugin;
    private final JsonCache playerData;

    private LimboCache(final AuthMe plugin) {
        this.plugin = plugin;
        this.cache = new ConcurrentHashMap<>();
        this.playerData = new JsonCache();
    }

    public static LimboCache getInstance() {
        if (singleton == null) {
            singleton = new LimboCache(AuthMe.getInstance());
        }
        return singleton;
    }

    public void addLimboPlayer(final Player player) {
        final String name = player.getName().toLowerCase();
        Location loc = player.getLocation();
        GameMode gameMode = player.getGameMode();
        boolean operator = false;
        String playerGroup = "";
        boolean flying = false;

        if (playerData.doesCacheExist(player)) {
            final DataFileCache cache = playerData.readCache(player);
            if (cache != null) {
                playerGroup = cache.getGroup();
                operator = cache.getOperator();
                flying = cache.isFlying();
            }
        } else {
            operator = player.isOp();
            flying = player.isFlying();
            if (plugin.permission != null) {
                try {
                    playerGroup = plugin.permission.getPrimaryGroup(player);
                } catch (final UnsupportedOperationException e) {
                    ConsoleLogger.showError("你的权限系统 (" + plugin.permission.getName() + ") 不支持权限组功能 移除配置... UnHook!");
                    plugin.permission = null;
                }
            }
        }

        if (Settings.isForceSurvivalModeEnabled) {
            if (gameMode == GameMode.CREATIVE) {
                flying = false;
            }
            gameMode = GameMode.SURVIVAL;
        }
        if (player.isDead()) {
            loc = plugin.getSpawnLocation(player);
        }
        cache.put(name, new LimboPlayer(name, loc, gameMode, operator, playerGroup, flying));
    }

    public void addLimboPlayer(final Player player, final String group) {
        cache.put(player.getName().toLowerCase(), new LimboPlayer(player.getName().toLowerCase(), group));
    }

    public void deleteLimboPlayer(final String name) {
        cache.remove(name);
    }

    public LimboPlayer getLimboPlayer(final String name) {
        return cache.get(name);
    }

    public boolean hasLimboPlayer(final String name) {
        return cache.containsKey(name);
    }

    public void updateLimboPlayer(final Player player) {
        if (this.hasLimboPlayer(player.getName().toLowerCase())) {
            this.deleteLimboPlayer(player.getName().toLowerCase());
        }
        addLimboPlayer(player);
    }

}
