package org.project.entity.enemies;

import org.project.entity.Entity;
import org.project.item.weapons.Weapon;

/**
 * Dragon - A powerful boss enemy with unique abilities
 * Subclass of Boss with enhanced combat capabilities
 */
public class Dragon extends Boss {

    private int fireBreathCooldown;
    private boolean enraged;
    private int enrageThreshold;

    public Dragon(String name, int maxHP, int maxMP, int damage, Weapon weapon, int xpReward) {
        super(name, maxHP, maxMP, damage, weapon, xpReward);
        this.fireBreathCooldown = 0;
        this.enraged = false;
        this.enrageThreshold = maxHP / 3; // Enrages when HP drops below 33%
    }

    public Dragon() {
        this("Ancient Dragon", 250, 100, 30, null, 600);
    }

    @Override
    public void attack(Entity target) {
        if (stunned) {
            System.out.println(name + " is stunned and cannot attack!");
            return;
        }

        // Check for enrage status
        if (!enraged && hp <= enrageThreshold) {
            enraged = true;
            damage += 10;
            System.out.println("🔥 " + name + " has become ENRAGED! Its attack power increases!");
        }

        // Decrease cooldown
        if (fireBreathCooldown > 0) {
            fireBreathCooldown--;
        }

        // Choose attack type based on conditions
        double attackChoice = Math.random();

        // Fire breath attack (cooldown of 3 turns)
        if (attackChoice < 0.25 && fireBreathCooldown == 0 && mp >= 30) {
            fireBreath(target);
        }
        // Tail swipe - area attack (if enraged)
        else if (enraged && attackChoice < 0.45 && mp >= 15) {
            tailSwipe(target);
        }
        // Normal attack
        else {
            normalAttack(target);
        }
    }

    /**
     * Fire Breath - Powerful area attack dealing high damage
     */
    private void fireBreath(Entity target) {
        if (mp < 30) {
            System.out.println(name + " tries to breathe fire but lacks mana!");
            normalAttack(target);
            return;
        }

        mp -= 30;
        fireBreathCooldown = 3;

        int totalDamage = (damage * 2) + (weapon != null ? weapon.getDamage() : 0);
        boolean ignoreDefense = true; // Fire burns through defenses

        System.out.println("🐉 " + name + " unleashes a devastating FIRE BREATH!");
        System.out.println("🔥 Flames engulf the battlefield!");

        // Extra damage if enraged
        if (enraged) {
            totalDamage *= 1.5;
            System.out.println("💢 Enraged fire breath deals extra damage!");
        }

        target.receiveAttack(totalDamage, ignoreDefense);
    }

    /**
     * Tail Swipe - Heavy attack that can stun
     */
    private void tailSwipe(Entity target) {
        if (mp < 15) {
            normalAttack(target);
            return;
        }

        mp -= 15;

        int totalDamage = (int)((damage * 1.7) + (weapon != null ? weapon.getDamage() * 0.5 : 0));

        System.out.println("🐉 " + name + " performs a massive TAIL SWIPE!");
        target.receiveAttack(totalDamage, false);

        // Chance to stun the target
        if (Math.random() < 0.3) {
            System.out.println("💫 The tail swipe stuns the target!");
            // Note: Would need to implement stun on Player
        }
    }

    /**
     * Normal attack with potential for extra effects
     */
    private void normalAttack(Entity target) {
        int totalDamage = damage + (weapon != null ? weapon.getDamage() : 0);

        // Enraged dragons deal additional damage
        if (enraged) {
            totalDamage += 5;
        }

        // Chance for critical hit
        boolean isCritical = Math.random() < 0.15;
        if (isCritical) {
            totalDamage *= 1.8;
            System.out.println("💥 " + name + " lands a CRITICAL strike!");
        }

        System.out.println("🐉 " + name + " attacks with its claws!");
        target.receiveAttack(totalDamage, false);
    }

    /**
     * Dragon's special ability - Rage Roar (buffs itself)
     */
    public void rageRoar() {
        if (mp < 20) {
            System.out.println("Not enough mana for Rage Roar!");
            return;
        }

        mp -= 20;
        damage += 5;
        System.out.println("🐉 " + name + " lets out a terrifying RAGE ROAR!");
        System.out.println("💪 " + name + "'s attack power increases permanently!");
    }

    /**
     * Dragon's ultimate ability - Inferno (must be used when enraged)
     */
    public void inferno(Entity target) {
        if (!enraged) {
            System.out.println(name + " must be enraged to use Inferno!");
            return;
        }

        if (mp < 50) {
            System.out.println("Not enough mana for Inferno!");
            return;
        }

        mp -= 50;
        int totalDamage = damage * 3 + (weapon != null ? weapon.getDamage() * 2 : 0);

        System.out.println("🔥🐉 " + name + " summons an INFERNO of pure destruction!");
        System.out.println("🌋 The ground erupts in flames!");
        target.receiveAttack(totalDamage, true);

        // After using inferno, dragon becomes exhausted
        damage -= 5;
        if (damage < 10) damage = 10;
        System.out.println("😰 " + name + " is exhausted from the inferno!");
    }

    @Override
    public void nextTurn() {
        super.nextTurn();
        // Reduce cooldowns
        if (fireBreathCooldown > 0) {
            fireBreathCooldown--;
        }
    }

    @Override
    public void receiveAttack(int damage, boolean ignoreDefense) {
        super.receiveAttack(damage, ignoreDefense);

        // Check if dragon becomes enraged due to damage
        if (!enraged && hp <= enrageThreshold) {
            enraged = true;
            this.damage += 10;
            System.out.println("🔥 " + name + " roars in pain and becomes ENRAGED!");
            System.out.println("⚔️ Its damage increases by 10!");
        }
    }

    @Override
    public int getXPReward() {
        // Enhanced XP reward for defeating a dragon
        int baseReward = super.getXPReward();
        return enraged ? baseReward + 100 : baseReward + 50;
    }

    // Getters
    public boolean isEnraged() {
        return enraged;
    }

    public int getFireBreathCooldown() {
        return fireBreathCooldown;
    }
}