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

import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.winconditions.Winconditions;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

import java.util.Objects;

public class InFightDamage implements Listener {

    public InFightDamage() {
        new StateDependentListener(!Config.ActiveWinconditions.contains(Winconditions.AMONG_US), FightState.Running, this);
    }

    @EventHandler
    public void handleEntityDamageByEntity(EntityDamageByEntityEvent event) {
        if(!(event.getEntity() instanceof LivingEntity))
            //Target is not a player
            return;

        LivingEntity player = ((LivingEntity) event.getEntity());
        if(!Fight.fighting(player))
            return;

        LivingEntity damager;

        if(event.getDamager() instanceof LivingEntity) {
            damager = (LivingEntity) event.getDamager();
        }else if(event.getDamager() instanceof Arrow) {
            Arrow damagerArrow = (Arrow) event.getDamager();
            if(!(damagerArrow.getShooter() instanceof LivingEntity))
                //Shooter is not a player
                return;

            damager = (LivingEntity) damagerArrow.getShooter();
        }else{
            //Damager is not a player
            return;
        }

        if(!Fight.fighting(damager)){
            event.setCancelled(true);
            //Damager is not fighting
            return;
        }

        if(Fight.getPlayerTeam(player) == Fight.getPlayerTeam(damager)) {
            event.setCancelled(true);
            if(event.getDamager() instanceof Arrow){
                event.getDamager().setFireTicks(0);
                player.setFireTicks(0);
            }
            FightSystem.getMessage().sendPrefixless("NO_FRIENDLY_FIRE", damager, ChatMessageType.ACTION_BAR);
        }

        if(player.getHealth() - event.getFinalDamage() <= 0){
            Objects.requireNonNull(Fight.getFightPlayer(damager)).addKill();
        }
    }
}
