package scenes;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.ggj2k20.game.GameMain;

import Hole.HoleController;
import helpers.GameInfo;
import helpers.GameManager;
import huds.GamePlayButtons;
import waterfall.Waterfall;
import waterfall.WaterfallController;


public class GamePlay implements Screen
{
    private GameMain gameMain;

    private OrthographicCamera mainCamera;

    private Texture bgImage;

    private FitViewport gameViewport;

    private HoleController holeController;

    private static World world;

    private boolean timerIsSet = false;

    private GamePlayButtons gamePlayButtons;

    private WaterfallController waterfallController;

    public GamePlay(GameMain gameMain)
    {
        this.gameMain = gameMain;

        mainCamera = new OrthographicCamera(GameInfo.W, GameInfo.H);
        mainCamera.position.set(GameInfo.W / 2f, GameInfo.H / 2f, 0);

        gameViewport = new FitViewport(GameInfo.W, GameInfo.H);

        world = new World(new Vector2(0, 0), true); // gravity of the world

        holeController = new HoleController(world, gameMain);

        bgImage = new Texture("Backgrounds/Dam Final.jpeg");

        gamePlayButtons = new GamePlayButtons(gameMain);
    }

    void update(float delta)
    {
        if (!timerIsSet)
        {
            Timer.schedule(myTimerTask, 2f, 2f, GameInfo.HOLES_NUMBER);
            timerIsSet = true;
        }
    }

    void handleInput(float dt)
    {
    }

    @Override
    public void show()
    {

    }

    @Override
    public void render(float delta)
    {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        gameMain.getBatch().begin();

        update(delta);

        gameMain.getBatch().draw(bgImage, 0, 0);

        waterfallController = gamePlayButtons.getWaterfallController();

        waterfallController.drawWaterfall(gameMain.getBatch());
        waterfallController.drawWaterfallAnimation(gameMain.getBatch());

        gameMain.getBatch().end();

        gameMain.getBatch().setProjectionMatrix(gamePlayButtons.getStage().getCamera().combined);
        gamePlayButtons.getStage().draw(); // to draw button to the screen
    }

    @Override
    public void resize(int width, int height)
    {
        gameViewport.update(width, height);
    }

    @Override
    public void pause()
    {

    }

    @Override
    public void resume()
    {

    }

    @Override
    public void hide()
    {

    }

    @Override
    public void dispose()
    {
        bgImage.dispose();
        gamePlayButtons.getStage().dispose();
    }

    private Timer.Task myTimerTask = new Timer.Task()
    {
        @Override
        public void run()
        {
            if (!GameManager.getInstance().isPaused)
            {
                if (GameManager.getInstance().drawNext)
                {
                    gamePlayButtons.addHole();
                }
                else
                {
                    gamePlayButtons.createGameOverPanel();
                }
            }
        }
    };

    public static World getWorld()
    {
        return world;
    }
}
