package com.loganmagnan.eventcore.managers;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.managers.hotbar.types.StaffModeHotBar;
import com.loganmagnan.eventcore.utils.CachedInventory;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.Constants;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import java.util.*;

@Getter
@Setter
public class StaffManager {

    private EventCore main = EventCore.getInstance();

    private List<UUID> playersInStaffMode = new ArrayList<UUID>();
    private List<UUID> playersVanished = new ArrayList<UUID>();
    private List<UUID> playersHidingStaff = new ArrayList<UUID>();
    private List<UUID> playersBypassed = new ArrayList<UUID>();

    private Map<UUID, CachedInventory> cachedInventories = new HashMap<UUID, CachedInventory>();

    private StaffModeHotBar staffModeHotBar;

    public StaffManager() {
        this.staffModeHotBar = new StaffModeHotBar("staff-mode");
        this.main.getHotBarManager().addHotBar(this.staffModeHotBar);
    }

    public boolean isPlayerInStaffMode(Player player) {
        return this.playersInStaffMode.contains(player.getUniqueId());
    }

    public boolean isPlayerVanished(Player player) {
        return this.playersVanished.contains(player.getUniqueId());
    }

    public void setStaffMode(Player player, boolean enabled) {
        if (enabled) {
            if (!this.playersInStaffMode.contains(player.getUniqueId())) {
                for (Player playerOnline : this.main.getServer().getOnlinePlayers()) {
                    player.showPlayer(playerOnline);
                }
            }

            this.playersInStaffMode.add(player.getUniqueId());
            this.cachedInventories.put(player.getUniqueId(), new CachedInventory(player, false));

            player.getInventory().clear();
            player.getInventory().setArmorContents(null);
            player.setGameMode(GameMode.CREATIVE);

            this.setVanish(player, true);
            this.staffModeHotBar.applyToPlayer(player, true);

            player.sendMessage(ColorUtils.getMessageType(Constants.COMMAND_MESSAGES.get("staff-mode.enabled").get(0)));
        } else {
            this.playersInStaffMode.remove(player.getUniqueId());
            this.cachedInventories.get(player.getUniqueId()).applyToPlayer(player, false);

            player.setGameMode(GameMode.SURVIVAL);

            this.setVanish(player, false);

            player.sendMessage(ColorUtils.getMessageType(Constants.COMMAND_MESSAGES.get("staff-mode.disabled").get(0)));
        }
    }

    public void setVanish(Player player, boolean enabled) {
        if (enabled) {
            if (!this.playersVanished.contains(player.getUniqueId())) {
                this.playersVanished.add(player.getUniqueId());
            }

            for (Player playerOnline : this.main.getServer().getOnlinePlayers()) {
                if (!this.isPlayerInStaffMode(playerOnline)) {
                    playerOnline.hidePlayer(player);

                    continue;
                }

                if (this.playersHidingStaff.contains(playerOnline.getUniqueId())) {
                    playerOnline.hidePlayer(player);
                }
            }
        } else {
            if (this.playersVanished.contains(player.getUniqueId())) {
                this.playersVanished.remove(player.getUniqueId());
            }

            for (Player playerOnline : this.main.getServer().getOnlinePlayers()) {
                playerOnline.showPlayer(player);
            }
        }
    }
}
