package de.frajul.endlessroll.views;

import android.graphics.Typeface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.game.Game;
import de.frajul.endlessroll.main.screens.GameScreen;
import de.frajul.endlessroll.main.screens.Screen;

/**
 * Created by Julian on 09.07.2016.
 */
public class GameOverMessage implements View.OnClickListener {

    private Animation fadeIn;
    private GameActivity gameActivity;
    private Game game;

    private View layout;
    private TopBar topBar;
    private Button tryAgain;
    private Button toMenu;

    public GameOverMessage(Game game, GameScreen gameScreen, GameActivity gameActivity, View layout) {
        this.game = game;
        this.gameActivity = gameActivity;
        this.layout = layout;
        layout.setVisibility(View.GONE);
        Typeface typeface = gameActivity.getTypeface();
        fadeIn = AnimationUtils.loadAnimation(gameActivity, R.anim.fade_in);
        topBar = new TopBar(gameActivity, gameScreen,
                layout.findViewById(R.id.game_over_message_topbar));
        topBar.setShopsEnabled(true);
        TextView title = (TextView) layout.findViewById(R.id.game_over_message_title);
        title.setTypeface(typeface);
        tryAgain = (Button) layout.findViewById(R.id.game_over_message_try_again);
        tryAgain.setTypeface(typeface);
        tryAgain.setOnClickListener(this);
        toMenu = (Button) layout.findViewById(R.id.game_over_message_to_menu);
        toMenu.setTypeface(typeface);
        toMenu.setOnClickListener(this);
    }

    public void fadeIn() {
        topBar.update();
        layout.startAnimation(fadeIn);
        layout.setVisibility(View.VISIBLE);
    }

    public boolean isVisible(){
        return layout.getVisibility() == View.VISIBLE;
    }

    public void prepareToBeShown(){
        topBar.update();
    }

    private void hide() {
        layout.clearAnimation();
        layout.setVisibility(View.GONE);
        gameActivity.getSoundManager().stopAllSounds();
    }

    @Override
    public void onClick(View v) {
        if (v.equals(tryAgain)) {
            hide();
            game.restartLevel();
        } else if (v.equals(toMenu)) {
            hide();
            game.toLevelsScreen();
        }
    }
}
