package com.jan.entity;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;

public class Animator extends Renderable {
    public static final String ANIMATOR_PROPERTY = "Animator";
    
    private ArrayList<AnimationClip> animations;
    private AnimationClip currClip;

    private float stateTime;
    
    public Animator(MapObject mapObject) {
        super(mapObject);
        animations = new ArrayList<>();
        String fileLocation = "animations/"+(String)loadProperty(ANIMATOR_PROPERTY);
        
        Json json = new Json();

        ArrayList<JsonValue> list = json.fromJson(ArrayList.class, Gdx.files.internal(fileLocation));
        for (JsonValue v : list) 
            animations.add(new AnimationClip(json.readValue(AnimationData.class, v)));
        currClip = animations.get(0);
        stateTime = 0f;
    }
    
    public void update(){//override
        stateTime += Gdx.graphics.getDeltaTime();
        this.setTexture(currClip.getKeyFrame(stateTime));
    }

    public void doAnimation(String animation, PlayMode mode){
        doAnimation(animation);
        setAnimationState(mode);
    }
    public void doAnimation(String animation){
        for(int i = 0; i < animations.size(); i++){
            if(animations.get(i).getName().equals(animation))
                currClip = animations.get(i);
        }
    }

    public void setAnimationState(PlayMode mode){ currClip.setPlayMode(mode); }

}

class AnimationClip extends AnimationData{
    private Texture animSheet;
    private Animation<TextureRegion> animation;

    public AnimationClip(AnimationData data){
        this.name = data.name;
        this.path = data.path;
        this.rows = data.rows;
        this.collumns = data.collumns;
        this.speed = data.speed;
        animSheet = new Texture(Gdx.files.internal("images/"+path));
        loadAnimation();
        animation.setPlayMode(PlayMode.LOOP);
	}
    private void loadAnimation(){
        TextureRegion[][] tmp = TextureRegion.split(animSheet, animSheet.getWidth() / collumns, animSheet.getHeight() / rows);
        TextureRegion[] animationFrames = new TextureRegion[collumns * rows];
		int index = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < collumns; j++) 
				animationFrames[index++] = tmp[i][j];
		}
        animation = new Animation<TextureRegion>(speed, animationFrames);
    }

    public void setPlayMode(PlayMode playMode){ animation.setPlayMode(playMode); }

    public TextureRegion getKeyFrame(float deltaTime){ return animation.getKeyFrame(deltaTime); }

}

class AnimationData{
    protected String name;
    protected String path;
    
    protected int rows;
    protected int collumns;
    protected float speed;

    public AnimationData(String name, String path, int rows, int collumns, float speed){
        this.name = name;
		this.path = path;
		this.rows = rows;
		this.collumns = collumns;
		this.speed = speed;
    }
    public AnimationData(){}

    public String getName(){ return name; }
	public String getPath(){ return path; }

	public int getRows(){ return rows; }
	public int getCollumns(){ return collumns; }

	public float getSpeed(){ return speed; }
}