/*
 *   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.fightsystem.fight;

import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.ai.AI;
import de.steamwar.fightsystem.countdown.Countdown;
import de.steamwar.fightsystem.countdown.EnternCountdown;
import de.steamwar.fightsystem.events.TeamDeathEvent;
import de.steamwar.sql.PersonalKit;
import de.steamwar.sql.SteamwarUser;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.function.Consumer;

public class FightPlayer {

    private final int id;
    private LivingEntity entity;
    @Getter
    private final FightTeam team;
    private boolean isOut;
    @Setter
    @Getter
    private Kit kit;
    @Getter
    private int kills;
    private EnternCountdown enternCountdown = null;

    FightPlayer(LivingEntity entity, SteamwarUser user, FightTeam team) {
        this.id = user.getId();
        this.entity = entity;
        this.team = team;
        this.isOut = false;
        kit = Kit.getKitByName(Config.MemberDefault);
        if(Config.PersonalKits){
            PersonalKit personalKit = PersonalKit.getKitInUse(user.getId(), Config.SchematicType.toDB());
            if(personalKit != null){
                kit = new Kit(personalKit);
            }
        }
        kills = 0;
    }

    public void revive() {
        isOut = false;
    }

    public void setOut() {
        isOut = true;
        Bukkit.getPluginManager().callEvent(new TeamDeathEvent(this));
        stopEnternCountdown();
        ifAI(AI::stop);
    }

    public void startEnternCountdown(Countdown countdown) {
        if(Config.EnterStages.size() > kit.getEnterStage() && kit.getEnterStage() >= 0)
            enternCountdown = new EnternCountdown(this, countdown);
    }

    public void stopEnternCountdown(){
        if(enternCountdown != null){
            enternCountdown.disable();
        }
        enternCountdown = null;
    }

    public LivingEntity getEntity() {
        LivingEntity bukkit = Bukkit.getPlayer(entity.getUniqueId());
        if(bukkit != null)
            entity = bukkit;
        return entity;
    }

    public SteamwarUser getUser() {
        return SteamwarUser.get(id);
    }

    public void ifAI(Consumer<AI> function) {
        if(entity instanceof Player)
            return;
        AI ai = AI.getAI(entity.getUniqueId());
        if(ai != null)
            function.accept(ai);
    }

    public void ifPlayer(Consumer<Player> function) {
        if(entity instanceof Player)
            function.accept((Player) entity);
    }

    public boolean isLiving() {
        return !this.isOut;
    }

    public boolean isLeader() {
        FightPlayer leader = team.getLeader();
        return leader != null && leader.getEntity() == entity;
    }

    public void addKill(){
        kills++;
    }

    public boolean canEntern(){
        if(enternCountdown == null)
            return false;
        return enternCountdown.getTimeLeft() == 0;
    }
}
