package com.github.jaystgelais.jrpg.map.actor;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.github.jaystgelais.jrpg.util.TimeUtil;

import java.util.HashMap;
import java.util.Map;

public final class ActorSpriteSet {
    private final long timeToTraverseTileMs;
    private final Texture spriteSheet;
    private final Map<Direction, Animation<TextureRegion>> walking = new HashMap<>();
    private final Map<Direction, TextureRegion> standing = new HashMap<>();

    public ActorSpriteSet(final SpriteSetData spriteSetData, final long timeToTraverseTileMs,
                          final AssetManager assetManager) {
        this.timeToTraverseTileMs = timeToTraverseTileMs;
        if (!assetManager.isLoaded(spriteSetData.getSpriteSheetPath(), Texture.class)) {
            assetManager.load(spriteSetData.getSpriteSheetPath(), Texture.class);
            assetManager.finishLoading();
        }
        spriteSheet = assetManager.get(spriteSetData.getSpriteSheetPath(), Texture.class);
        TextureRegion[][] tmp = TextureRegion.split(
                spriteSheet,
                spriteSheet.getWidth() / spriteSetData.getColumns(),
                spriteSheet.getHeight() / spriteSetData.getRows()
        );
        walking.put(Direction.UP, new Animation<TextureRegion>(
                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),
                tmp[spriteSetData.getUpFacingRowIndex()]
        ));
        walking.put(Direction.RIGHT, new Animation<TextureRegion>(
                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),
                tmp[spriteSetData.getRightFacingRowIndex()]
        ));
        walking.put(Direction.DOWN, new Animation<TextureRegion>(
                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),
                tmp[spriteSetData.getDownFacingRowIndex()]
        ));
        walking.put(Direction.LEFT, new Animation<TextureRegion>(
                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),
                tmp[spriteSetData.getLeftFacingRowIndex()]
        ));
        standing.put(
                Direction.UP,
                tmp[spriteSetData.getUpFacingRowIndex()][spriteSetData.getUpFacingStandingIndex()]
        );
        standing.put(
                Direction.RIGHT,
                tmp[spriteSetData.getRightFacingRowIndex()][spriteSetData.getRightFacingStandingIndex()]
        );
        standing.put(
                Direction.DOWN,
                tmp[spriteSetData.getDownFacingRowIndex()][spriteSetData.getDownFacingStandingIndex()]
        );
        standing.put(
                Direction.LEFT,
                tmp[spriteSetData.getLeftFacingRowIndex()][spriteSetData.getLeftFacingStandingIndex()]
        );
    }

    public long getTimeToTraverseTileMs() {
        return timeToTraverseTileMs;
    }

    public Animation<TextureRegion> getWalkingAnimation(final Direction direction) {
        return walking.get(direction);
    }

    public TextureRegion getStandingImage(final Direction direction) {
        return standing.get(direction);
    }
}

@@ -1404,155 +1404,24 @@
 UP, 
-new Animation%3CTextureRegion%3E(%0A                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),%0A                
+build3CycleWalk(
 tmp%5B
@@ -1452,33 +1452,46 @@
 gRowIndex()%5D
-%0A        
+, timeToTraverseTileMs
 ));%0A        
@@ -1506,172 +1506,58 @@
 put(
-Direction.RIGHT, new Animation%3CTextureRegion%3E(%0A                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),%0A                
+%0A                Direction.RIGHT, build3CycleWalk(
 tmp%5B
@@ -1587,32 +1587,55 @@
 acingRowIndex()%5D
+, timeToTraverseTileMs)
 %0A        ));%0A   
@@ -1619,33 +1619,32 @@
 TileMs)%0A        
-)
 );%0A        walki
@@ -1670,155 +1670,24 @@
 WN, 
-new Animation%3CTextureRegion%3E(%0A                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),%0A                
+build3CycleWalk(
 tmp%5B
@@ -1720,33 +1720,46 @@
 gRowIndex()%5D
-%0A        
+, timeToTraverseTileMs
 ));%0A        
@@ -1790,155 +1790,24 @@
 FT, 
-new Animation%3CTextureRegion%3E(%0A                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / spriteSetData.getColumns(),%0A                
+build3CycleWalk(
 tmp%5B
@@ -1844,25 +1844,38 @@
 Index()%5D
-%0A        
+, timeToTraverseTileMs
 ));%0A    
@@ -2881,11 +2881,483 @@
 );%0A    %7D
+%0A%0A    private Animation%3CTextureRegion%3E build3CycleWalk(final TextureRegion%5B%5D frames, final long timeToTraverseTileMs) %7B%0A        TextureRegion%5B%5D animationFrames = %7B%0A                frames%5B0%5D,%0A                frames%5B1%5D,%0A                frames%5B2%5D,%0A                frames%5B1%5D%0A        %7D;%0A        return new Animation%3CTextureRegion%3E(%0A                TimeUtil.convertMsToFloatSeconds(timeToTraverseTileMs) / animationFrames.length,%0A                animationFrames%0A        );%0A    %7D
 %0A%7D%0A

Fix checkstyle errors