package pathgame.gameplay;

public class Camera
{

    private final static int CAM_SPEED = 64;

    private float lastScale = 1.0f;
    private float scale = 1.0f;

    private float lastCamOffsetX = 0.0f;
    private float lastCamOffsetY = 0.0f;
    private float camOffsetX = 0.0f;
    private float camOffsetY = 0.0f;

    public void tick(Level level, Gamestate gamestate)
    {
        lastCamOffsetX = camOffsetX;
        lastCamOffsetY = camOffsetY;
        lastScale = scale;

        if(Keys.ZOOM_IN_KEY.isDown())
        {
            scale *= 1.1f;
        }
        else if(Keys.ZOOM_OUT_KEY.isDown())
        {
            scale /= 1.1f;
        }

        if(!level.getPlayer().isMoving() && gamestate.is(Gamestates.GAMEPLAY))
        {
            if(Keys.CAM_UP_KEY.isDown())
            {
                camOffsetY += CAM_SPEED;
            }
            if(Keys.CAM_DOWN_KEY.isDown())
            {
                camOffsetY -= CAM_SPEED;
            }
            if(Keys.CAM_LEFT_KEY.isDown())
            {
                camOffsetX += CAM_SPEED;
            }
            if(Keys.CAM_RIGHT_KEY.isDown())
            {
                camOffsetX -= CAM_SPEED;
            }
        }
    }

    public void limitScale(float zoomRestriction)
    {
        if(scale <= zoomRestriction)
        {
            scale = zoomRestriction;
        }
        if(lastScale <= zoomRestriction)
        {
            lastScale = zoomRestriction;
        }

    }
    
    public void reset()
    {
        camOffsetX = 0.0f;
        camOffsetY = 0.0f;
        lastCamOffsetX = 0.0f;
        lastCamOffsetY = 0.0f;
    }

    public float getInterpolatedScale(float lag)
    {
        return lastScale + (scale - lastScale) * lag;
    }
    
    public float getCamOffsetX(float offX, float minOffX, float lag, float interScale)
    {
        float interCamX = lastCamOffsetX + (camOffsetX - lastCamOffsetX) * lag;
        if(offX + interCamX > 0.0f)
        {
            camOffsetX = -offX;
            if(lastCamOffsetX > camOffsetX)
            {
                lastCamOffsetX = camOffsetX;
            }
            interCamX = lastCamOffsetX + (camOffsetX - lastCamOffsetX) * lag;
        }
        if(offX + interCamX < minOffX)
        {
            camOffsetX = minOffX - offX;
            if(lastCamOffsetX < camOffsetX)
            {
                lastCamOffsetX = camOffsetX;
            }
            interCamX = lastCamOffsetX + (camOffsetX - lastCamOffsetX) * lag;
        }
        return offX + interCamX;
    }
    
    public float getCamOffsetY(float offY, float minOffY, float lag, float interScale)
    {
        float interCamY = lastCamOffsetY + (camOffsetY - lastCamOffsetY) * lag;
        if(offY + interCamY > 0.0f)
        {
            camOffsetY = -offY;
            if(lastCamOffsetY > camOffsetY)
            {
                lastCamOffsetY = camOffsetY;
            }
            interCamY = lastCamOffsetY + (camOffsetY - lastCamOffsetY) * lag;
        }
        if(offY + interCamY < minOffY)
        {
            camOffsetY = minOffY - offY;
            if(lastCamOffsetY < camOffsetY)
            {
                lastCamOffsetY = camOffsetY;
            }
            interCamY = lastCamOffsetY + (camOffsetY - lastCamOffsetY) * lag;
        }
        return offY + interCamY;
    }
}
