package com.loganmagnan.eventcore.managers.event;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.managers.event.teams.Team;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.CustomLocation;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
public class EventManager {

    private EventCore main = EventCore.getInstance();

    private FileConfiguration config = this.main.getMainConfig().getConfig();

    private Event event;

    public EventManager() {
        this.loadConfig();
    }

    private void loadConfig() {
        if (this.config.contains("EVENT.NAME")) {
            try {
                List<Team> teams = new ArrayList<Team>();
                List<CustomLocation> teamSpawnPoints = new ArrayList<CustomLocation>();

                if (this.config.get("EVENT.TEAMS") != null) {
                    if (this.config.getConfigurationSection("EVENT.TEAMS") != null) {
                        ConfigurationSection configurationSection = this.config.getConfigurationSection("EVENT.TEAMS");

                        if (configurationSection != null) {
                            configurationSection.getKeys(false).forEach((index) -> {
                                String name = "";

                                UUID leader = null;

                                List<UUID> allPlayers = new ArrayList<UUID>();
                                List<UUID> playingPlayers = new ArrayList<UUID>();

                                if (configurationSection.getString(index + ".NAME") != null) {
                                    name = configurationSection.getString(index + ".LEADER");
                                }

                                if (configurationSection.getString(index + ".LEADER") != null) {
                                    leader = UUID.fromString(configurationSection.getString(index + ".LEADER"));
                                }

                                for (String allPlayerUUID : configurationSection.getStringList(index + ".ALL-PLAYERS")) {
                                    allPlayers.add(UUID.fromString(allPlayerUUID));
                                }

                                for (String playingPlayerUUID : configurationSection.getStringList(index + ".PLAYING-PLAYERS")) {
                                    playingPlayers.add(UUID.fromString(playingPlayerUUID));
                                }

                                teams.add(new Team(name, leader, allPlayers, playingPlayers));
                            });
                        }
                    }
                }

                for (String teamSpawnPoint : this.config.getStringList("EVENT.TEAM-SPAWN-POINTS")) {
                    teamSpawnPoints.add(CustomLocation.stringToLocation(teamSpawnPoint));
                }

                this.event = new Event(
                        this.config.getString("EVENT.NAME"),
                        CustomLocation.stringToLocation(this.config.getString("EVENT.SPAWN-POINT")),
                        this.config.getInt("EVENT.DURATION"),
                        teams,
                        this.config.getInt("EVENT.AMOUNT-OF-TEAMS"),
                        this.config.getInt("EVENT.AMOUNT-PER-TEAM"),
                        teamSpawnPoints,
                        this.config.getBoolean("EVENT.TOGGLEABLE-OPTIONS.SCOREBOARD-ENABLED"),
                        this.config.getBoolean("EVENT.TOGGLEABLE-OPTIONS.TIMER-ENABLED"),
                        this.config.getBoolean("EVENT.TOGGLEABLE-OPTIONS.EVENTS.ENABLED"),
                        this.config.getBoolean("EVENT.TOGGLEABLE-OPTIONS.EVENTS.BLOCK-PLACE.ENABLED"),
                        this.config.getBoolean("EVENT.TOGGLEABLE-OPTIONS.EVENTS.BLOCK-BREAK.ENABLED"),
                        this.config.getBoolean("EVENT.STARTED")
                );
            } catch (NullPointerException exception) {
                this.main.getServer().getConsoleSender().sendMessage(ColorUtils.getMessageType("&cEvent information not found"));
            }
        } else {
            this.event = new Event(
                    "Event",
                    this.main.getSpawnManager().getSpawnLocation(),
                    600,
                    new ArrayList<Team>(),
                    2,
                    10,
                    new ArrayList<CustomLocation>(),
                    true,
                    true,
                    false,
                    false,
                    false,
                    false
            );
        }
    }

    public void saveConfig() {
        List<String> teamSpawnPoints = new ArrayList<String>();

        if (this.event.getTeams().size() == 0) {
            this.config.set("EVENT.TEAMS", null);
        } else {
            int index = 0;

            for (Team team : this.event.getTeams()) {
                List<String> allPlayers = new ArrayList<String>();
                List<String> playingPlayers = new ArrayList<String>();

                for (UUID allPlayerUUID : team.getAllPlayers()) {
                    allPlayers.add(allPlayerUUID.toString());
                }

                for (UUID playingPlayerUUID : team.getPlayingPlayers()) {
                    playingPlayers.add(playingPlayerUUID.toString());
                }

                this.config.set("EVENT.TEAMS." + index + ".NAME", team.getName());
                this.config.set("EVENT.TEAMS." + index + ".LEADER", team.getLeader().toString());
                this.config.set("EVENT.TEAMS." + index + ".ALL-PLAYERS", allPlayers);
                this.config.set("EVENT.TEAMS." + index + ".PLAYING-PLAYERS", playingPlayers);

                index++;
            }
        }

        for (CustomLocation teamSpawnPoint : this.event.getTeamSpawnPoints()) {
            teamSpawnPoints.add(CustomLocation.locationToString(teamSpawnPoint));
        }

        this.config.set("EVENT.NAME", this.event.getName());
        this.config.set("EVENT.SPAWN-POINT", CustomLocation.locationToString(this.event.getSpawnPoint()));
        this.config.set("EVENT.DURATION", this.event.getDuration());
        this.config.set("EVENT.AMOUNT-OF-TEAMS", this.event.getAmountOfTeams());
        this.config.set("EVENT.AMOUNT-PER-TEAM", this.event.getAmountPerTeam());
        this.config.set("EVENT.TEAM-SPAWN-POINTS", teamSpawnPoints);
        this.config.set("EVENT.TOGGLEABLE-SETTINGS.SCOREBOARD-ENABLED", this.event.isScoreboardEnabled());
        this.config.set("EVENT.TOGGLEABLE-SETTINGS.TIMER-ENABLED", this.event.isTimerEnabled());
        this.config.set("EVENT.TOGGLEABLE-SETTINGS.EVENTS.ENABLED", this.event.isEventsEnabled());
        this.config.set("EVENT.TOGGLEABLE-SETTINGS.EVENTS.BLOCK-PLACE.ENABLED", this.event.isBlockPlaceEventEnabled());
        this.config.set("EVENT.TOGGLEABLE-SETTINGS.EVENTS.BLOCK-BREAK.ENABLED", this.event.isBlockBreakEventEnabled());
        this.config.set("EVENT.STARTED", this.event.isStarted());
        this.main.getMainConfig().save();
    }
}
