package pathgame;

import me.hammerle.snuviengine.api.IGame;
import me.hammerle.snuviengine.api.Renderer;
import pathgame.algorithm.TravellingSalesAlg;
import pathgame.gameplay.Camera;
import pathgame.gameplay.Gamestate;
import pathgame.gameplay.Gamestates;
import pathgame.gameplay.Keys;
import pathgame.gameplay.Level;
import pathgame.gameplay.Player;
import pathgame.rendering.TileMapRenderer;
import pathgame.gameplay.menu.Menu;
import pathgame.rendering.HUDRenderer;
import pathgame.rendering.MenuRenderer;
import pathgame.rendering.PlayerRenderer;
import pathgame.rendering.TileRenderer;
import pathgame.tilemap.TileMap;

public class PathGame implements IGame
{

    private final Gamestate gamestate = new Gamestate();

    private final Level level = new Level();

    private final PlayerRenderer playerRenderer = new PlayerRenderer();
    private final TileMapRenderer mapRenderer = new TileMapRenderer();

    private final Menu menu = new Menu();
    private final MenuRenderer menuRenderer = new MenuRenderer();
    private final HUDRenderer hudRenderer = new HUDRenderer();

    private final Camera cam = new Camera();

    public PathGame()
    {
    }

    @Override
    public void tick()
    {
        cam.tick(level, gamestate);

        level.getMap().tick();
        mapRenderer.tick();
        if(gamestate.getState() == Gamestates.GAMEPLAY)
        {
            level.getPlayer().tick(level.getMap());
            if(level.getPlayer().isMoving())
            {
                cam.reset();
            }
        }
        menu.tick(gamestate, level);

        if(Keys.TEST_KEY.getTime() == 1)
        {
            level.nextLevel();
            TravellingSalesAlg.calcSalesPathLen(level.getMap());
        }
    }

    @Override
    public void renderTick(Renderer r, float lag)
    {
        if(gamestate.is(Gamestates.MENU))
        {
            lag = 0.0f;
        }
        
        TileMap map = level.getMap();
        Player player = level.getPlayer();

        float zoomRestrictionX = r.getViewWidth() / (map.getWidth() * TileRenderer.TILE_SIZE);
        float zoomRestrictionY = (r.getViewHeight() - HUDRenderer.OFFSET_Y) / (map.getHeight() * TileRenderer.TILE_SIZE);

        cam.limitScale(Math.max(zoomRestrictionX, zoomRestrictionY));
        float interScale = cam.getInterpolatedScale(lag);

        mapRenderer.setScale(interScale);

        float offX = getMapOffsetX(map, player, r, lag, interScale);
        float offY = getMapOffsetY(map, player, r, lag, interScale) + HUDRenderer.OFFSET_Y;

        mapRenderer.renderTick(map, r, false, offX, offY);
        switch(gamestate.getState())
        {
            case GAMEPLAY:
                playerRenderer.renderTick(map, mapRenderer, r, player, lag, offX, offY);
                hudRenderer.renderTick(r, player, lag);
                break;
            case MENU:
                menuRenderer.renderTick(r, lag, menu);
                break;
        }
    }

    private float getMapOffsetX(TileMap map, Player player, Renderer r, float lag, float interScale)
    {
        float ix = (player.getLastX() + (player.getX() - player.getLastX()) * lag) * interScale * TileRenderer.TILE_SIZE;
        float offX = (-ix + r.getViewWidth() * 0.5f) - TileRenderer.TILE_SIZE * 0.5f * interScale;

        float minOffX = -mapRenderer.getWidth(map) + r.getViewWidth();
        if(offX < minOffX)
        {
            return cam.getCamOffsetX(minOffX, minOffX, lag, interScale);
        }
        else if(offX > 0.0f)
        {
            return cam.getCamOffsetX(0.0f, minOffX, lag, interScale);
        }
        return cam.getCamOffsetX(offX, minOffX, lag, interScale);
    }

    private float getMapOffsetY(TileMap map, Player player, Renderer r, float lag, float interScale)
    {
        float viewHeight = r.getViewHeight() - HUDRenderer.OFFSET_Y;
        float iy = (player.getLastY() + (player.getY() - player.getLastY()) * lag) * interScale * TileRenderer.TILE_SIZE;
        float offY = (-iy + viewHeight * 0.5f) - TileRenderer.TILE_SIZE * 0.5f * interScale;

        float minOffY = -mapRenderer.getHeight(map) + viewHeight;
        if(offY < minOffY)
        {
            return cam.getCamOffsetY(minOffY, minOffY, lag, interScale);
        }
        else if(offY > 0.0f)
        {
            return cam.getCamOffsetY(0.0f, minOffY, lag, interScale);
        }
        return cam.getCamOffsetY(offY, minOffY, lag, interScale);
    }

    @Override
    public void onStop()
    {
    }
}
