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.levels.Level;
import de.frajul.endlessroll.levels.LevelPack;
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 23.04.2016.
 */
public class GoalMessage implements GoalMessageLevelButtonOnClickListener, View.OnClickListener {

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

    private View layout;
    private TopBar topBar;
    private GoalMessageLevelButton restart;
    private GoalMessageLevelButton nextLevel;
    private Button toMenu;

    public GoalMessage(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.goal_message_topbar));
        topBar.setShopsEnabled(true);
        TextView title = (TextView) layout.findViewById(R.id.goal_message_title);
        title.setTypeface(typeface);
        restart = new GoalMessageLevelButton(gameActivity, this,
                layout.findViewById(R.id.goal_message_restart));
        nextLevel = new GoalMessageLevelButton(gameActivity, this,
                layout.findViewById(R.id.goal_message_next_level));
        toMenu = (Button) layout.findViewById(R.id.goal_message_to_menu);
        toMenu.setTypeface(typeface);
        toMenu.setOnClickListener(this);
    }

    public void fadeInWithDelay(final LevelPack levelPack, final Level level) {
        this.levelPack = levelPack;

        topBar.update();
        restart.init(R.string.goal_message_restart_format_d, level);
        boolean isLastLevel = levelPack.isLastLevel(level);
        boolean hasNextWorld = gameActivity.getLevelManager().getNextLevelPack(levelPack) != null;
        nextLevel.setVisible(!isLastLevel || hasNextWorld);
        if (!isLastLevel)
            nextLevel
                    .init(R.string.goal_message_next_level_format_d, levelPack.getNextLevel(level));
        else if (hasNextWorld) {
            nextLevel.initShowToNextWorld(true, levelPack);
        }

        layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                layout.startAnimation(fadeIn);
                layout.setVisibility(View.VISIBLE);
            }
        }, 500);
    }

    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(toMenu)) {
            game.toLevelsScreen();
            hide();
        }
    }

    @Override
    public void onClick(GoalMessageLevelButton goalMessageLevelButton) {
        hide();
        if (goalMessageLevelButton.isShowingToNextWorld()) {
            gameActivity.flipToScreen(Screen.ScreenType.WORLDS);
        } else
            gameActivity.startGame(levelPack, goalMessageLevelButton.getLevel());
    }

    public void onKeyBack() {
        game.toLevelsScreen();
        hide();
    }
}
