package nl.joozey.games.asmallworld;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
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 OutroScene implements Scene {

    private SceneControl sceneControl;

    private Texture planetTex;
    private Texture planetLightsTex;
    private Texture backgroundTex;
    private Texture shipLeftTex;

    private Sprite planetSprite;
    private Sprite planetLightsSprite;
    private Sprite shipSprite;
    private Sprite scoreSprite;

    private Texture coreCrystalTex;
    private Sprite coreCrystal;

    private SpriteTextWriter spriteTextWriter;
    private Texture scoreTex;

    private Texture thanksTex;
    private Sprite thanksSprite;

    private float sceneTime;

    private Music music2;

    public OutroScene() {
        Statics.score += 100000;
        planetTex = new Texture("planet.png");
        planetLightsTex = new Texture("planet_lighting.png");
        backgroundTex = new Texture("background.png");
        shipLeftTex = new Texture("shipleft.png");

        scoreTex = new Texture("score.png");
        scoreSprite = new Sprite(scoreTex);
        scoreSprite.setPosition(
                Gdx.graphics.getWidth() * .5f - scoreSprite.getWidth() * .5f,
                Gdx.graphics.getHeight() - 100);
        spriteTextWriter = new SpriteTextWriter(
                String.valueOf(Statics.score),
                Gdx.graphics.getWidth() * .5f - 6 * 7,
                Gdx.graphics.getHeight() - 120);

        planetSprite = new Sprite(planetTex);
        planetLightsSprite = new Sprite(planetLightsTex);
        shipSprite = new Sprite(shipLeftTex);
        shipSprite.setScale(1f);
        shipSprite.setPosition(300, 240);

        coreCrystalTex = new Texture("core.png");
        coreCrystal = new Sprite(coreCrystalTex);
        coreCrystal.setPosition(
                shipSprite.getX() + shipSprite.getWidth() * .5f - coreCrystal.getWidth() * .5f,
                shipSprite.getY() - coreCrystal.getHeight() * .7f
        );

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

        thanksTex = new Texture("thanks.png");
        thanksSprite = new Sprite(thanksTex);
        thanksSprite.setPosition(Gdx.graphics.getWidth() * .5f - thanksTex.getWidth() * .5f, 50);
    }

    @Override
    public void render(SpriteBatch batch) {
        batch.draw(backgroundTex, 0, 0);
        planetSprite.draw(batch);
        planetLightsSprite.draw(batch);
        coreCrystal.draw(batch);
        shipSprite.draw(batch);

        spriteTextWriter.draw(batch);
        scoreSprite.draw(batch);

        thanksSprite.draw(batch);
    }

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

        spriteTextWriter.dispose();
        scoreTex.dispose();
        coreCrystalTex.dispose();
    }

    @Override
    public void act(float deltaTime) {

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

        sceneTime += deltaTime;

        if (sceneTime < 5) {
            coreCrystal.setPosition(
                    shipSprite.getX() + shipSprite.getWidth() * .5f - coreCrystal.getWidth() * .5f + 15,
                    shipSprite.getY() - coreCrystal.getHeight() * .7f + sceneTime * 15f
            );
        } else if (sceneTime < 15) {
            shipSprite.translateX(deltaTime * -70f);
            coreCrystal.translateX(deltaTime * -70f);
        }

        planetSprite.setRotation(sceneTime * 5f);
    }

    @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) {
        sceneControl.showIntro();
        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;
    }
}
