package de.frajul.endlessroll.main.screens;

import android.app.AlertDialog;
import android.app.Dialog;
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 android.widget.ToggleButton;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.MyGlSurfaceView;
import de.frajul.endlessroll.rendering.Rendering;
import de.frajul.endlessroll.views.ExitConfirmDialog;

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

    private Rendering rendering;

    private Button play;
    private Button unlockLevels;
    private Button gain90EP;
    private Button toGlTestScreen;
    private Button settings;

    private ExitConfirmDialog exitConfirmDialog;

    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);
        toGlTestScreen = createButton(R.id.startscreen_to_gl_test_screen, typeface);
        settings = (Button) layout.findViewById(R.id.startscreen_settings);
        settings.setOnClickListener(this);

        exitConfirmDialog = new ExitConfirmDialog(gameActivity);

        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() {
        glView.setCurrentRendering(rendering);
    }

    @Override
    public void onBackKeyDown() {
        exitConfirmDialog.show();
    }

    @Override
    public void onClick(View v) {
        if (v.equals(play)) {
            gameActivity.flipToScreen(ScreenType.WORLDS);
            glView.setCurrentRendering(null);
        } else if (v.equals(settings)) {
            gameActivity.flipToScreen(ScreenType.SETTINGS);
            glView.setCurrentRendering(null);
        } else if (v.equals(gain90EP)) {
            gameActivity.getUser().gainEp(90);
        } else if (v.equals(unlockLevels)) {
            gameActivity.getLevelManager().unlockAllLevels();
            gameActivity.getLevelManager().unlockAllPacks();
        } else if (v.equals(toGlTestScreen))
            gameActivity.flipToScreen(ScreenType.GL_TEST);
    }


}
