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

import de.steamwar.linkage.Linked;
import de.steamwar.misslewars.Config;
import de.steamwar.misslewars.FightState;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.EnumSet;

@Linked
public class JoinListener extends BasicListener {

	public JoinListener(){
		super(EnumSet.of(FightState.WAITING, FightState.FIGHTING));
	}

	@EventHandler(priority = EventPriority.HIGHEST)
	public void onJoin(PlayerJoinEvent e){
		e.setJoinMessage("§a» " + e.getPlayer().getDisplayName());
		join(e.getPlayer());
	}

	public static void join(Player p) {
		if (Config.isEvent()) {
			joinEvent(p);
			return;
		}

		if (Config.isChallenge()) {
			joinChallenge(p);
			return;
		}

		joinNormal(p);
	}

	private static void joinEvent(Player p) {
		if (Config.EventTeamRedID == 0 && Config.EventTeamBlueID == 0) {
			joinNormal(p);
			return;
		}

		int teamId = SteamwarUser.get(p.getUniqueId()).getTeam();
		if (teamId != Config.EventTeamRedID && teamId != Config.EventTeamBlueID) {
			teamId = 0;
		}
		if (teamId == Config.EventTeamRedID && MissileWars.getRedTeam().size() < Config.MaximumTeamMembers) {
			MissileWars.getRedTeam().join(p);
		} else if (teamId == Config.EventTeamBlueID && MissileWars.getBlueTeam().size() < Config.MaximumTeamMembers) {
			MissileWars.getBlueTeam().join(p);
		} else {
			p.teleport(MissileWars.getRedTeam().getSpawn().toVector().midpoint(MissileWars.getBlueTeam().getSpawn().toVector()).toLocation(p.getWorld()));
		}
	}

	private static void joinChallenge(Player p) {
		if (Config.RedLeader.equals(p.getUniqueId())) {
			MissileWars.getRedTeam().join(p);
		} else if (Config.BlueLeader.equals(p.getUniqueId())) {
			MissileWars.getBlueTeam().join(p);
		} else {
			p.teleport(MissileWars.getRedTeam().getSpawn().toVector().midpoint(MissileWars.getBlueTeam().getSpawn().toVector()).toLocation(p.getWorld()));
		}
	}

	private static void joinNormal(Player p) {
		if (MissileWars.getRedTeam().size() == MissileWars.getBlueTeam().size()) {
			if (Math.random() > 0.5) {
				MissileWars.getRedTeam().join(p);
			} else {
				MissileWars.getBlueTeam().join(p);
			}
		} else if (MissileWars.getRedTeam().size() < MissileWars.getBlueTeam().size()) {
			MissileWars.getRedTeam().join(p);
		} else {
			MissileWars.getBlueTeam().join(p);
		}
	}
}
