package me.hammerle.supersnuvi.entity.components.ai;

import me.hammerle.supersnuvi.entity.Entity;
import me.hammerle.supersnuvi.javafx.KeyHandler;
import me.hammerle.supersnuvi.util.SoundUtils;

public class HumanController extends EntityController
{
    private boolean combatMode = false;
    private int combatTimer = 0;
    private int blockTimer = -1;
    private int attackTimer = -1;
    private int dodgeTimer = -1;
    private int dashTimer = -1;
    
    public HumanController(Entity ent) 
    {
        super(ent);
    }
    
    @Override
    public boolean isRunning() 
    {
        return KeyHandler.RUN.isDown();
    }

    @Override
    public boolean isInCombatMode() 
    {
        return combatMode || combatTimer < 0;
    }

    @Override
    public int getCombatTimer() 
    {
        return combatTimer;
    }

    @Override
    public boolean isBlocking() 
    {
        return blockTimer >= 0;
    }

    @Override
    public boolean isAttacking() 
    {
        return attackTimer >= 0;
    }

    @Override
    public boolean isDashing() 
    {
        return dashTimer != -1;
    }
    
    @Override
    public boolean isDodging() 
    {
        return dodgeTimer != -1;
    }
    

    @Override
    public void tick() 
    {
        if(ent.getHealth().isDead())
        {
            return;
        }
            
        if(KeyHandler.COMBAT.wasJustReleased())
        {
            if(combatMode)
            {
                combatMode = false;
                combatTimer = -ent.getAnimator().getCombatStartTicks();
            }
            else
            {
                combatMode = true;
                combatTimer = ent.getAnimator().getCombatStartTicks();
            }
        }
        
        if(combatTimer < 0)
        {
            combatTimer++;
        }
        else if(combatTimer > 0)
        {
            combatTimer--;
        }
        
        if(blockTimer != -1)
        {
            blockTimer++;
        }
        if(blockTimer >= ent.getAnimator().getBlockTicks())
        {
            blockTimer = -60;
        }
        
        if(attackTimer != -1)
        {
            attackTimer++;
        }
        if(attackTimer >= ent.getAnimator().getAttackTicks())
        {
            attackTimer = -1;
        }
        
        if(dodgeTimer != -1)
        {
            dodgeTimer++;
            if(dodgeTimer > 5)
            {
                dodgeTimer = -1;
            }
            
            ent.setMotionX(ent.getAnimator().drawImageFlipped() ? 5 : -5);
        }
        
        if(dashTimer != -1)
        {
            dashTimer++;
            if(dashTimer > 5)
            {
                dashTimer = -1;
            }
            
            ent.setMotionX(ent.getAnimator().drawImageFlipped() ? -5 : 5);
        }
         
        if(combatMode && combatTimer == 0)
        {
            if(KeyHandler.COMBAT_BLOCK.isDown() && blockTimer == -1)
            {
                ent.getAnimator().resetFrames();
                blockTimer = 0;
            }
            else if(KeyHandler.COMBAT_ATTACK.wasJustReleased() && attackTimer == -1)
            {
                attackTimer = 0;
            }
            else if(KeyHandler.COMBAT_DASH.wasJustReleased() && dashTimer == -1 && ent.getEnergy().getEnergyPercent() >= 0.2)
            {
                dashTimer = 0;
                ent.getEnergy().addEnergyPercent(-0.2);
            }
            else if(KeyHandler.COMBAT_DODGE.wasJustReleased() && dodgeTimer == -1 && ent.getEnergy().getEnergyPercent() >= 0.05)
            {
                dodgeTimer = 0;
                ent.getEnergy().addEnergyPercent(-0.05);
            }
        }
        
        
        if(!isBlocking() && dodgeTimer == -1 && dashTimer == -1)
        {
            if(KeyHandler.JUMP.isDown() && ent.getMovement().jump())
            {
                SoundUtils.playSound(SoundUtils.Sound.JUMP);
            }
            
            double speed;
            if(ent.getController().isInCombatMode())
            {
                speed = 1.5;
            }
            else
            {
                speed = 3.0;
                if(isRunning())
                {
                    if(ent.getEnergy().getEnergyPercent() > 0.0)
                    {
                        speed = 4.5;
                    }
                    if(ent.isMoving())
                    {
                        ent.getEnergy().addEnergy(-0.3);
                    }
                }
            }

            if(KeyHandler.LEFT.isDown())
            {
                ent.setMotionX(-speed * ent.getMovementPenalty().getFactor());
            }
            else if(KeyHandler.RIGHT.isDown())
            {
                ent.setMotionX(speed * ent.getMovementPenalty().getFactor());
            }

            if(ent.isOnGround() && ent.getPreviousMotionX() != 0)
            {
                if(ent.getMovementPenalty().isInWater())
                {
                    SoundUtils.playSound(SoundUtils.Sound.WALK_WATER);
                }
                else
                {
                    SoundUtils.playSound(SoundUtils.Sound.WALK);
                }           
            }
        }
    }
}
