package rip.tilly.bedwars.game;

import io.netty.util.internal.ConcurrentSet;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.github.paperspigot.Title;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.game.arena.Arena;
import rip.tilly.bedwars.game.arena.CopiedArena;
import rip.tilly.bedwars.utils.CC;
import rip.tilly.bedwars.utils.TimeUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * Created by Lucanius
 * Project: BedWars
 */
@Getter
@Setter
public class Game {

    private final BedWars plugin = BedWars.getInstance();

    private final Set<Entity> entitiesToRemove = new ConcurrentSet<>();

    private final List<GameTeam> teams;
    private final Arena arena;

    private final UUID gameId = UUID.randomUUID();

    private CopiedArena copiedArena;
    private GameState gameState = GameState.STARTING;
    private int countdown = 6;
    private int durationTimer;

    public Game(Arena arena, GameTeam... teams) {
        this.arena = arena;
        this.teams = Arrays.asList(teams);
    }

    public GameTeam getTeamByName(String name) {
        return this.teams.stream().filter(team -> team.getPlayerTeam().getName().equalsIgnoreCase(name)).findFirst().orElse(null);
    }

    public String getDuration() {
        return TimeUtils.formatIntoMMSS(durationTimer);
    }

    public void addEntityToRemove(Entity entity) {
        this.entitiesToRemove.add(entity);
    }

    public void removeEntityToRemove(Entity entity) {
        this.entitiesToRemove.remove(entity);
    }

    public void clearEntitiesToRemove() {
        this.entitiesToRemove.clear();
    }

    public void broadcastTitle(String message, String subMessage) {
        Title title = new Title(message, subMessage, 5, 20, 5);
        this.teams.forEach(team -> team.playingPlayers().forEach(player -> player.sendTitle(title)));
    }

    public void broadcast(String message) {
        this.teams.forEach(team -> team.playingPlayers().forEach(player -> player.sendMessage(CC.translate(message))));
    }

    public void broadcastSound(Sound sound) {
        this.teams.forEach(team -> team.playingPlayers().forEach(player -> player.playSound(player.getLocation(), sound, 10, 1)));
    }

    public int decrementCountdown() {
        return --this.countdown;
    }

    public void incrementDuration() {
        ++this.durationTimer;
    }

    public boolean isPartyMatch() {
        return (this.teams.get(0).getAllPlayers().size() >= 2 || this.teams.get(1).getAllPlayers().size() >= 2);
    }

    public boolean isPlaceable(Location location, Game game) {
        double minX = game.getCopiedArena().getMin().getX();
        double minZ = game.getCopiedArena().getMin().getZ();
        double maxX = game.getCopiedArena().getMax().getX();
        double maxZ = game.getCopiedArena().getMax().getZ();

        if (minX > maxX) {
            double lastMinX = minX;
            minX = maxX;
            maxX = lastMinX;
        }

        if (minZ > maxZ) {
            double lastMinZ = minZ;
            minZ = maxZ;
            maxZ = lastMinZ;
        }

        if (location.getX() >= minX && location.getX() <= maxX && location.getZ() >= minZ && location.getZ() <= maxZ) {
            return true;
        }

        return false;
    }

    public boolean isInside(Location location, Game game) {
        double aMinX = game.getCopiedArena().getTeamAmin().getX();
        double aMinZ = game.getCopiedArena().getTeamAmin().getZ();
        double aMaxX = game.getCopiedArena().getTeamAmax().getX();
        double aMaxZ = game.getCopiedArena().getTeamAmax().getZ();

        if (aMinX > aMaxX) {
            double lastMinX = aMinX;
            aMinX = aMaxX;
            aMaxX = lastMinX;
        }

        if (aMinZ > aMaxZ) {
            double lastMinZ = aMinZ;
            aMinZ = aMaxZ;
            aMaxZ = lastMinZ;
        }

        if (location.getX() <= aMinX && location.getX() >= aMaxX && location.getZ() <= aMinZ && location.getZ() >= aMaxZ) {
            return true;
        }

        double bMinX = game.getCopiedArena().getTeamBmin().getX();
        double bMinZ = game.getCopiedArena().getTeamBmin().getZ();
        double bMaxX = game.getCopiedArena().getTeamBmax().getX();
        double bMaxZ = game.getCopiedArena().getTeamBmax().getZ();

        if (bMinX > bMaxX) {
            double lastMinX = bMinX;
            bMinX = bMaxX;
            bMaxX = lastMinX;
        }

        if (bMinZ > bMaxZ) {
            double lastMinZ = bMinZ;
            bMinZ = bMaxZ;
            bMaxZ = lastMinZ;
        }

        if (location.getX() <= bMinX && location.getX() >= bMaxX && location.getZ() <= bMinZ && location.getZ() >= bMaxZ) {
            return true;
        }

        return false;
    }
}
