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

import me.hammerle.supersnuvi.entity.Entity;
import me.hammerle.supersnuvi.entity.components.Health;
import me.hammerle.supersnuvi.javafx.Image;
import me.hammerle.supersnuvi.util.Utils;

public class DefaultAnimator extends EntityAnimator
{
    private int counter;
    private int frame;
    
    private Image currentFace;
    
    private final Image face;
    private final Image[] move;
    private final Image[] moveAir;
    private boolean flipped;
    
    private int blink;
    
    public DefaultAnimator(Entity ent, double offsetX, double offsetY, String face, String[] move, String[] moveAir) 
    {
        super(ent, offsetX, offsetY);
        flipped = false;
        counter = 0;
        frame = 0;  
        blink = 0;
        
        this.face = Utils.getImage(face);
        this.face.createSlot(255, 0, 0, 255);
        this.face.createSlot(0, 255, 0, 255);
        
        this.currentFace = this.face;
        
        this.move = new Image[move.length];
        for(int i = 0; i < move.length; i++)
        {
            this.move[i] = Utils.getImage(move[i]);
            this.move[i].createSlot(255, 0, 0, 255);
            this.move[i].createSlot(0, 255, 0, 255);
        }
        
        this.moveAir = new Image[moveAir.length];
        for(int i = 0; i < moveAir.length; i++)
        {
            this.moveAir[i] = Utils.getImage(moveAir[i]);
            this.moveAir[i].createSlot(255, 0, 0, 255);
            this.moveAir[i].createSlot(0, 255, 0, 255);
        }
    }

    @Override
    public boolean drawImageFlipped() 
    {
        return flipped;
    }

    @Override
    public void tick() 
    {
        counter++;
        if(counter >= 5)
        {
            counter = 0;
            frame++;
        }
        if(frame < 0)
        {
            frame = 0;
        }
        
        blink++;
        if(blink > 9)
        { 
            blink = 0;
        }
    }
    
    @Override
    public Image getImage() 
    {
        double motionX = ent.getPreviousMotionX();
        if(ent.isOnGround())
        {
            currentFace = face;
            if(motionX != 0)
            {
                if(frame >= move.length)
                {
                    frame = 0;
                }
                flipped = motionX < 0;
                return move[frame];
            }
            else
            {
                frame = 0;
            }
        }
        else
        {
            currentFace = moveAir[0];
            if(motionX != 0)
            {
                if(frame >= moveAir.length)
                {
                    frame = 0;
                }
                flipped = motionX < 0;
                return moveAir[frame];
            }
            else
            {
                frame = 0;
            }
        }
        return currentFace;
    }
    
    @Override
    public void bindSlot()
    {
        Image i = getImage();
        if(i != null)
        {
            Health h = ent.getHealth();
            if(h.isHealed())
            {
                i.bindSlot(1);
            }
            else if(h.isHurt())
            {
                if(blink >= 5)
                {
                    i.bindSlot(0);
                }
                else
                {
                    i.bindSlot(-1);
                }
            }
            else
            {
                i.bindSlot(-1);
            }
        }
    }
}
