package me.hammerle.supersnuvi.entity;

import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import me.hammerle.supersnuvi.gamelogic.Level;
import me.hammerle.supersnuvi.gamelogic.StateRenderer;
import me.hammerle.supersnuvi.input.IKeyHandler;
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, double width, double height) 
    {
        super(level, x, y, width, height);
        
        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 controlTick(IKeyHandler keyHandler) 
    {
        super.controlTick(keyHandler);
        
        if(keyHandler.isKeyDown(KeyCode.SPACE))
        {
            if(jump())
            {
                SoundUtils.playSound(SoundUtils.Sound.JUMP);
            }

            /*posX = 96;
            posY= 128;*/
        }
        
        //motionX = -2;
        //motionY = -2;
        double speed = keyHandler.isKeyDown(KeyCode.SHIFT) ? 4.5 : 3;
        
        if(keyHandler.isKeyDown(KeyCode.LEFT))
        {
            setMotionX(-speed * getMaxSpeedModifier());
        }
        else if(keyHandler.isKeyDown(KeyCode.RIGHT))
        {
            setMotionX(speed * getMaxSpeedModifier());
        }
        /*if(keyHandler.isKeyDown(KeyCode.UP))
        {
            setMotionY(2);
        }
        else if(keyHandler.isKeyDown(KeyCode.DOWN))
        {
            setMotionY(-2);
        }
        else
        {
            setMotionY(0);
        }*/
        
        if(isOnGround() && getPreviousMotionX() != 0)
        {
            if(isInWater())
            {
                SoundUtils.playSound(SoundUtils.Sound.WALK_WATER);
            }
            else
            {
                SoundUtils.playSound(SoundUtils.Sound.WALK);
            }           
        }
    }

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