package me.hammerle.supersnuvi.entity;

import me.hammerle.supersnuvi.gamelogic.Level;
import me.hammerle.supersnuvi.gamelogic.StateRenderer;
import me.hammerle.supersnuvi.javafx.Image;
import me.hammerle.supersnuvi.javafx.KeyHandler;
import me.hammerle.supersnuvi.util.SoundUtils;
import me.hammerle.supersnuvi.util.Utils;

public class Hero extends AnimatedEntity
{
    private final double jumpPower;
    
    public Hero(Level level,  double x, double y) 
    {
        super(level, x, y, 32, 64, 100.0);
        
        jumpPower = StateRenderer.CONFIG.getDouble("jump", 10);
                
        face = Utils.getImage("snuvi/snuvi");
        super.setImage(face);
        move = new Image[9];
        move[0] = Utils.getImage("snuvi/snuvi_frame1");
        move[1] = Utils.getImage("snuvi/snuvi_frame2");
        move[2] = Utils.getImage("snuvi/snuvi_frame3");
        move[3] = Utils.getImage("snuvi/snuvi_frame4");
        move[4] = Utils.getImage("snuvi/snuvi_frame5");
        move[5] = Utils.getImage("snuvi/snuvi_frame6");
        move[6] = Utils.getImage("snuvi/snuvi_frame7");
        move[7] = Utils.getImage("snuvi/snuvi_frame8");
        move[8] = Utils.getImage("snuvi/snuvi_frame9");
        
        moveAir = new Image[1];
        moveAir[0] = Utils.getImage("snuvi/snuvi_jump");
        /*moveAir[0] = Utils.getImage("snuvi/snuvi_frame1");
        moveAir[1] = Utils.getImage("snuvi/snuvi_frame2");
        moveAir[2] = Utils.getImage("snuvi/snuvi_frame3");
        moveAir[3] = Utils.getImage("snuvi/snuvi_frame4");
        moveAir[4] = Utils.getImage("snuvi/snuvi_frame5");
        moveAir[5] = Utils.getImage("snuvi/snuvi_frame6");
        moveAir[6] = Utils.getImage("snuvi/snuvi_frame7");
        moveAir[7] = Utils.getImage("snuvi/snuvi_frame8");
        moveAir[8] = Utils.getImage("snuvi/snuvi_frame9");*/
    }

    @Override
    public double getJumpPower() 
    {
        return jumpPower * getMaxSpeedModifier();
    }

    @Override
    public void tick() 
    {
        controlTick();
        super.tick();
    }

    @Override
    public boolean isRunning() 
    {
        return KeyHandler.RUN.isDown();
    }
    
    public void controlTick() 
    {
        if(KeyHandler.JUMP.isDown() && jump())
        {
            SoundUtils.playSound(SoundUtils.Sound.JUMP);
        }
        
        double speed = 3.0;
        if(isRunning())
        {
            if((getLevel().getEnergy() / Level.MAX_ENERGY) > 0.25)
            {
                speed = 4.5;
            }
            if(isMoving())
            {
                getLevel().addEnergy(-0.3);
            }
        }
        
        if(KeyHandler.LEFT.isDown())
        {
            setMotionX(-speed * getMaxSpeedModifier());
        }
        else if(KeyHandler.RIGHT.isDown())
        {
            setMotionX(speed * getMaxSpeedModifier());
        }
        
        if(isOnGround() && getPreviousMotionX() != 0)
        {
            if(isInWater())
            {
                SoundUtils.playSound(SoundUtils.Sound.WALK_WATER);
            }
            else
            {
                SoundUtils.playSound(SoundUtils.Sound.WALK);
            }           
        }
    }

    @Override
    public double getRenderOffsetY()
    {
        return -2;
    }   

    @Override
    public void onDeath() 
    {
        getLevel().scheduleReset();
    }
}
