package de.frajul.endlessroll.views;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;

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

/**
 * Created by Julian on 23.01.2016.
 */
public class ShortMenu implements View.OnClickListener {

    private Game game;
    private Random random;
    private Animation slideLeft;
    private Animation slideRight;
    private Animation slideTop;

    private View layout;
    private TopBar topBar;
    private TextView continueView;
    private TextView restartView;
    private TextView exitView;

    public ShortMenu(final Game game, GameActivity gameActivity, View layout) {
        this.game = game;
        this.layout = layout;
        Typeface typeface = gameActivity.getTypeface();
        topBar = new TopBar(gameActivity, Screen.ScreenType.GAME,
                layout.findViewById(R.id.shortmenu_topbar));
        continueView = (TextView) layout.findViewById(R.id.shortmenu_continue);
        continueView.setTypeface(typeface);
        continueView.setOnClickListener(this);
        restartView = (TextView) layout.findViewById(R.id.shortmenu_restart);
        restartView.setTypeface(typeface);
        restartView.setOnClickListener(this);
        exitView = (TextView) layout.findViewById(R.id.shortmenu_exit);
        exitView.setTypeface(typeface);
        exitView.setOnClickListener(this);

        random = new Random();
        Context context = game.getContext();
        slideLeft = AnimationUtils.loadAnimation(context, R.anim.slide_in_left);
        slideRight = AnimationUtils.loadAnimation(context, R.anim.slide_in_right);
        slideTop = AnimationUtils.loadAnimation(context, R.anim.slide_in_top);
    }

    public void startAnims() {
        startRandomAnimation(continueView);
        startRandomAnimation(restartView);
        startRandomAnimation(exitView);
        topBar.startAnimation(slideTop);
    }

    public void setVisible(boolean visible) {
        layout.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        if (visible)
            topBar.update();
    }

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

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

    private void startRandomAnimation(View view) {
        float r = random.nextFloat();
        if (r >= 0.5)
            view.startAnimation(slideRight);
        else
            view.startAnimation(slideLeft);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(continueView)) {
            setVisible(false);
            game.continueGame();
        } else if (v.equals(restartView)) {
            setVisible(false);
            game.restartLevel();
        } else if (v.equals(exitView)) {
            setVisible(false);
            game.toLevelsScreen();
        }
    }
}
