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

import de.steamwar.sql.Event;
import de.steamwar.sql.EventFight;
import de.steamwar.sql.Team;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.UUID;
import java.util.logging.Level;

public class Config {
    private Config() {
    }

    public static final int ArenaMinX;
    public static final int ArenaMaxX;
    public static final int ArenaMinY;
    public static final int ArenaMinZ;
    public static final int ArenaMaxZ;

    public static final Location RedSpawn;
    public static final int RedPortalZ;
    public static final Location BlueSpawn;
    public static final int BluePortalZ;

    public static final int WaitingTime;
    public static final int PlatformTime;
    public static final int ItemTime;
    public static final int ShieldFlyTime;
    public static final int EndTime;

    public static final double MissileChance;

    public static final boolean Barrier;
    public static final boolean Space;

    // Challenge
    public static UUID BlueLeader;
    public static UUID RedLeader;

    private static final int EventKampfID;

    // Event
    public static final EventFight EventKampf;
    public static final String TeamBlueName;
    public static final String TeamRedName;
    public static final String TeamBlueColor;
    public static final String TeamRedColor;
    public static final int EventTeamBlueID;
    public static final int EventTeamRedID;
    public static final int MaximumTeamMembers;

    static {
        File configfile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml");
        if (!configfile.exists()) {
            Bukkit.getLogger().log(Level.SEVERE, "Config fehlt!");
            Bukkit.shutdown();
        }

        FileConfiguration config = YamlConfiguration.loadConfiguration(configfile);
        WaitingTime = config.getInt("WaitingTime");
        PlatformTime = config.getInt("PlatformTime");
        ItemTime = config.getInt("ItemTime");
        ShieldFlyTime = config.getInt("ShieldFlyTime");
        EndTime = config.getInt("EndTime");

        MissileChance = config.getDouble("MissileChance");

        ConfigurationSection arena = config.getConfigurationSection("Arena");
        assert arena != null;
        ArenaMinX = arena.getInt("MinX");
        ArenaMaxX = arena.getInt("MaxX");
        ArenaMinY = arena.getInt("MinY");
        ArenaMinZ = arena.getInt("MinZ");
        ArenaMaxZ = arena.getInt("MaxZ");

        ConfigurationSection red = config.getConfigurationSection("Red");
        assert red != null;
        RedPortalZ = red.getInt("PortalZ");
        RedSpawn = new Location(Bukkit.getWorlds().get(0), red.getDouble("SpawnX"), red.getDouble("SpawnY"), red.getDouble("SpawnZ"), (float) red.getDouble("SpawnYaw"), (float) red.getDouble("SpawnPitch"));

        ConfigurationSection blue = config.getConfigurationSection("Blue");
        assert blue != null;
        BluePortalZ = blue.getInt("PortalZ");
        BlueSpawn = new Location(Bukkit.getWorlds().get(0), blue.getDouble("SpawnX"), blue.getDouble("SpawnY"), blue.getDouble("SpawnZ"), (float) blue.getDouble("SpawnYaw"), (float) blue.getDouble("SpawnPitch"));

        String blueLeader = System.getProperty("blueLeader", null);
        String redLeader = System.getProperty("redLeader", null);
        if (blueLeader != null)
            BlueLeader = UUID.fromString(blueLeader);
        else
            BlueLeader = null;
        if (redLeader != null)
            RedLeader = UUID.fromString(redLeader);
        else
            RedLeader = null;

        Barrier = config.getBoolean("Barrier", false);
        Space = config.getBoolean("Space", false);

        EventKampfID = Integer.parseInt(System.getProperty("fightID", "0"));
        if (EventKampfID >= 1) {
            EventKampf = EventFight.get(EventKampfID);
            if (EventKampf == null) {
                Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight");
                Bukkit.shutdown();
            }
            assert EventKampf != null;

            Team team1 = Team.get(EventKampf.getTeamBlue());
            Team team2 = Team.get(EventKampf.getTeamRed());

            if (team1 == null || team2 == null) {
                Bukkit.getLogger().log(Level.SEVERE, "Failed to load Team");
                Bukkit.shutdown();
            }
            assert team1 != null;
            assert team2 != null;

            TeamBlueName = team1.getTeamKuerzel();
            TeamRedName = team2.getTeamKuerzel();
            TeamBlueColor = "§" + team1.getTeamColor();
            TeamRedColor = "§" + team2.getTeamColor();
            EventTeamBlueID = team1.getTeamId();
            EventTeamRedID = team2.getTeamId();

            Event event = Event.get(EventKampf.getEventID());
            if (EventTeamBlueID == 0 && EventTeamRedID == 0) {
                MaximumTeamMembers = Integer.MAX_VALUE;
            } else {
                MaximumTeamMembers = event.getMaximumTeamMembers();
            }
        } else {
            EventKampf = null;
            TeamBlueName = "Blau";
            TeamRedName = "Rot";
            TeamBlueColor = "§3";
            TeamRedColor = "§c";
            EventTeamBlueID = 0;
            EventTeamRedID = 0;
            MaximumTeamMembers = Integer.MAX_VALUE;
        }
    }

    public static boolean isEvent() {
        return EventKampfID >= 1;
    }

    public static boolean isChallenge() {
        return BlueLeader != null && RedLeader != null;
    }

    public static boolean test() {
        return EventKampfID == -1;
    }
}
