package pathgame.rendering;

import me.hammerle.snuviengine.api.Renderer;
import me.hammerle.snuviengine.api.Texture;
import pathgame.gameplay.Player;
import pathgame.tilemap.TileMap;
import pathgame.tilemap.Tiles;

/**
 * A container for holding everything about the renderer for the player
 *
 * @author julia
 */
public class PlayerRenderer
{
    private static final Texture CHARACTER = new Texture("resources/character.png");

    /**
     * Recalculates the rendering positions and settings of the menu-elements on
     * the screen every rendertick based on the gamelogic in the gametick
     *
     * @param map the current map
     * @param mapR the map renderer
     * @param r the renderer
     * @param p the current player
     * @param lag the current lag
     * @param offX the current x-offset of the map
     * @param offY the current y-offset of the map
     */
    public void renderTick(TileMap map, TileMapRenderer mapR, Renderer r, Player p, float lag, float offX, float offY)
    {
        float playerSize = mapR.getScale() * TileRenderer.TILE_SIZE;

        r.setMixColorEnabled(false);
        r.setColorEnabled(false);
        r.setTextureEnabled(true);

        r.translateTo(0.0f, 0.0f);
        r.updateMatrix();

        float yIndex;
        float baseX = (p.getLastX() + (p.getX() - p.getLastX()) * lag);
        float ix = baseX * mapR.getScale() * TileRenderer.TILE_SIZE + offX;

        float baseY = (p.getLastY() + (p.getY() - p.getLastY()) * lag);
        float iy = baseY * mapR.getScale() * TileRenderer.TILE_SIZE + offY;

        baseX = baseX - (int) baseX;
        baseY = baseY - (int) baseY;

        int tIndex = 0;
        float xTexOff = 0;
        float yTexOff = 0;

        CHARACTER.bind();
        if(p.getVelX() > 0)
        {
            //go right
            yIndex = 2;
            tIndex = checkForAnimationIndex(baseX);
        }
        else if(p.getVelX() < 0)
        {
            //go left
            yIndex = 1;
            tIndex = checkForAnimationIndex(baseX);
        }
        else if(p.getVelY() > 0)
        {
            //go down
            yIndex = 0;
            tIndex = checkForAnimationIndex(baseY);
        }
        else if(p.getVelY() < 0)
        {
            //go up
            yIndex = 3;
            tIndex = checkForAnimationIndex(baseY);
        }
        else
        {
            //stand still
            yIndex = 0;
        }

        if(p.isSailing())
        {
            yTexOff = 0.5f;
        }
        else if(p.getCurrTile() == Tiles.SHALLOW_WATER)
        {
            xTexOff = 0.5f;
        }

        float viewScale = r.getViewScale();
        ix = (int) (ix * viewScale) / viewScale;
        iy = (int) (iy * viewScale) / viewScale;

        r.getTextureRenderer().drawRectangle(ix, iy, ix + playerSize, iy + playerSize,
                tIndex * 0.125f + xTexOff, yIndex * 0.125f + yTexOff,
                (tIndex + 1) * 0.125f + xTexOff, yIndex * 0.125f + 0.125f + yTexOff);
    }

    private int checkForAnimationIndex(float base)
    {
        return ((int) (base * 4 * 2)) % 4;
    }
}
