package nl.joozey.games.asmallworld;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * Created by mint on 22-4-17.
 */

public class IntroScene implements Scene {

    private SceneControl sceneControl;

    private Texture planetTex;
    private Texture planetLightsTex;
    private Texture backgroundTex;
    private Texture shipTex;
    private Texture shipLeftTex;
    private Texture creditsTex;

    private Sprite planetSprite;
    private Sprite planetLightsSprite;
    private Sprite shipSprite;
    private Sprite creditsSprite;
    private float sceneTime;
    private boolean shipMovesLeft;

    private Music music2;

    public IntroScene() {
        Statics.score = 0;
        planetTex = new Texture("planet.png");
        planetLightsTex = new Texture("planet_lighting.png");
        backgroundTex = new Texture("background.png");
        shipTex = new Texture("ship.png");
        shipLeftTex = new Texture("shipleft.png");

        planetSprite = new Sprite(planetTex);
        planetLightsSprite = new Sprite(planetLightsTex);
        shipSprite = new Sprite(shipTex);
        shipSprite.setScale(0.8f);
        shipSprite.setPosition(-shipSprite.getWidth(), Gdx.graphics.getHeight() * .5f);

        music2 = Gdx.audio.newMusic(Gdx.files.internal("music2.ogg"));

        creditsTex = new Texture("credits.png");
        creditsSprite = new Sprite(creditsTex);
        creditsSprite.setPosition(Gdx.graphics.getWidth() * .5f - creditsTex.getWidth() * .5f, 50);
    }

    @Override
    public void render(SpriteBatch batch) {
        batch.draw(backgroundTex, 0, 0);
        if(!shipMovesLeft) {
            shipSprite.draw(batch);
        }
        planetSprite.draw(batch);
        planetLightsSprite.draw(batch);
        if(shipMovesLeft) {
            shipSprite.draw(batch);
        }
        creditsSprite.draw(batch);
    }

    @Override
    public void dispose() {
        planetTex.dispose();
        planetLightsTex.dispose();
        backgroundTex.dispose();
        shipLeftTex.dispose();
        shipTex.dispose();
        music2.dispose();
    }

    @Override
    public void act(float deltaTime) {

        if(sceneTime == 0) {
            music2.play();
            music2.setVolume(0.5f);
        }

        sceneTime += deltaTime;
        planetSprite.setRotation(sceneTime * 5f);

        if (sceneTime > 2 && sceneTime <= 9) {
            shipSprite.translateX((sceneTime - 2) * 0.5f);
        }

        if (sceneTime > 9 && sceneTime <= 12) {
            if(!shipMovesLeft) {
                shipSprite.setScale(1f);
                shipSprite.setTexture(shipLeftTex);
                shipMovesLeft = true;
            }
            shipSprite.translateX((sceneTime - 2) * -0.2f);
        }

        if (sceneTime > 15) {
            OrthographicCamera camera = sceneControl.getCamera();
            camera.zoom = 1 - (sceneTime - 15) * 0.15f;
            camera.position.y += Gdx.graphics.getDeltaTime() * 20f;
        }

        if (sceneTime > 21.5f) {
            sceneControl.showMainGame();
        }
    }

    @Override
    public void setSceneControl(SceneControl sceneControl) {
        this.sceneControl = sceneControl;
    }

    @Override
    public void restart() {
        sceneTime = 0;
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        if(character == 's') {
            sceneControl.showMainGame();
        }
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}
