package de.frajul.endlessroll.main.screens;

import android.graphics.Typeface;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.MyGlSurfaceView;
import de.frajul.endlessroll.main.game.StartScene;
import de.frajul.endlessroll.sqlDatabase.MyDatabase;
import de.frajul.endlessroll.views.ConfirmDialog;

/**
 * Created by Julian on 07.07.2016.
 */
public class StartScreen extends GLScreen<RelativeLayout> implements View.OnClickListener, ConfirmDialog.ConfirmDialogListener {

    private final int CALL_ID_CONFIRM_EXIT = 1;
    private final int CALL_ID_CONFIRM_FINISH_WORLD = 2;

    private StartScreenRendering rendering;

    private Button play;
    private Button unlockLevels;
    private Button gain90EP;
    private Button finishFirstWorld;
    private Button toGlTestScreen;

    private ConfirmDialog confirmDialog;

    public StartScreen(GameActivity gameActivity, MyGlSurfaceView glSurfaceView) throws Exception {
        super(ScreenType.START, gameActivity, R.layout.start_screen, glSurfaceView);

        Typeface typeface = gameActivity.getTypeface();
        play = createButton(R.id.startscreen_play, typeface);
        play.startAnimation(AnimationUtils.loadAnimation(gameActivity, R.anim.rotate));
        unlockLevels = createButton(R.id.startscreen_unlock_levels, typeface);
        gain90EP = createButton(R.id.startscreen_gain_90_ep, typeface);
        finishFirstWorld = createButton(R.id.startscreen_finish_world_1, typeface);
        toGlTestScreen = createButton(R.id.startscreen_to_gl_test_screen, typeface);

        confirmDialog = new ConfirmDialog(gameActivity, this);

        rendering = new StartScreenRendering(gameActivity);
        glView.addRendering(rendering);
    }

    private Button createButton(@IdRes int id, Typeface typeface) {
        Button button = (Button) layout.findViewById(id);
        button.setTypeface(typeface);
        button.setOnClickListener(this);
        return button;
    }

    @Override
    public void prepareToBeShown() {
        gameActivity.getSoundManager().menuMusic.start();

        ((StartScene) rendering.getScene()).randomizeWorld();
        ((StartScene) rendering.getScene()).loadTexturesForCurrentWorld();
        glView.setCurrentRendering(rendering);
    }

    @Override
    public void onBackKeyDown() {
        confirmDialog.show(CALL_ID_CONFIRM_EXIT, R.string.confirm_dialog_exit);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(play)) {
            gameActivity.flipToScreen(ScreenType.WORLDS);
            glView.setCurrentRendering(null);
        } else if (v.equals(gain90EP)) {
            gameActivity.getUser().gainEp(90, true);
        } else if (v.equals(unlockLevels)) {
            gameActivity.getLevelManager().unlockAllLevels();
            gameActivity.getLevelManager().unlockAllPacks();
            MyDatabase database = gameActivity.getDataStorageHandler().getDatabase();
            database.open();
            database.writeAllLevelAndPackStatus(gameActivity.getLevelManager());
            database.close();
        } else if (v.equals(finishFirstWorld)) {
            confirmDialog
                    .show(CALL_ID_CONFIRM_FINISH_WORLD, R.string.confirm_dialog_finish_world_1);
        } else if (v.equals(toGlTestScreen))
            gameActivity.flipToScreen(ScreenType.GL_TEST);
    }


    @Override
    public void onConfirmed(int callId) {
        if (callId == CALL_ID_CONFIRM_EXIT) {
            gameActivity.exitGame();
        } else if (callId == CALL_ID_CONFIRM_FINISH_WORLD) {
            gameActivity.resetData();
            gameActivity.getUser().gainEp(320, false);
            gameActivity.getLevelManager().finishFirstPack();
            gameActivity.showNewFinishedPlayerShapeTasks();
            gameActivity.getTutorialManager().getToolShopTutorial().setFinished(true);

            gameActivity.getDataStorageHandler().writeUserData(gameActivity.getUser());
            gameActivity.getDataStorageHandler().writeToolShopTutorialFinished(true);
            MyDatabase database = gameActivity.getDataStorageHandler().getDatabase();
            database.open();
            database.writeAllLevelAndPackStatus(gameActivity.getLevelManager());
            database.close();
        }
    }
}
