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.ProgressBar;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.screens.Screen;
import de.frajul.endlessroll.user.User;

/**
 * Created by Julian on 08.07.2016.
 */
public class TopBar implements View.OnClickListener {

    private GameActivity gameActivity;
    private View layout;
    private Screen parent;
    private Screen.ScreenType customCallerForScreenSwitch = Screen.ScreenType.NONE;

    private Animation starDecreaseAnimation;
    private Animation energyDecreaseAnimation;
    private Animation toolShopPulse;

    private TextView levelDisplay;
    private ProgressBar levelProgress;
    private TextView starCount;
    private TextView energyCount;
    private Button backButton;
    private Button settingsButton;
    private Button toolshopButton;
    private Button shapeshopButton;
    private TextView starCountDecrease;
    private TextView energyCountDecrease;

    public TopBar(GameActivity gameActivity, Screen parent, View layout) {
        this.gameActivity = gameActivity;
        this.parent = parent;
        this.layout = layout;

        starDecreaseAnimation = AnimationUtils.loadAnimation(gameActivity, R.anim.decrease);
        energyDecreaseAnimation = AnimationUtils.loadAnimation(gameActivity, R.anim.decrease);
        toolShopPulse = AnimationUtils.loadAnimation(gameActivity, R.anim.pulse);

        Typeface typeface = gameActivity.getTypeface();
        levelDisplay = (TextView) layout.findViewById(R.id.topbar_leveldisplay);
        levelDisplay.setTypeface(typeface);
        levelProgress = (ProgressBar) layout.findViewById(R.id.topbar_levelprogress);
        starCount = (TextView) layout.findViewById(R.id.topbar_starcount);
        starCount.setTypeface(typeface);
        energyCount = (TextView) layout.findViewById(R.id.topbar_energycount);
        energyCount.setTypeface(typeface);
        backButton = (Button) layout.findViewById(R.id.topbar_back_button);
        backButton.setOnClickListener(this);
        settingsButton = (Button) layout.findViewById(R.id.topbar_settings);
        settingsButton.setOnClickListener(this);
        toolshopButton = (Button) layout.findViewById(R.id.topbar_toolshop);
        toolshopButton.setOnClickListener(this);
        shapeshopButton = (Button) layout.findViewById(R.id.topbar_shapeshop);
        shapeshopButton.setOnClickListener(this);
        if (parent.getType() == Screen.ScreenType.SETTINGS)
            settingsButton.setEnabled(false);
        if (parent.getType() == Screen.ScreenType.TOOL_SHOP)
            toolshopButton.setEnabled(false);
        if (parent.getType() == Screen.ScreenType.SHAPE_SHOP)
            shapeshopButton.setEnabled(false);
        if (parent.getType() == Screen.ScreenType.GAME) {
            backButton.setEnabled(false);
            toolshopButton.setEnabled(false);
            shapeshopButton.setEnabled(false);
        }
        starCountDecrease = (TextView) layout.findViewById(R.id.topbar_starcount_decrease);
        starCountDecrease.setTypeface(typeface);
        energyCountDecrease = (TextView) layout.findViewById(R.id.topbar_energycount_decrease);
        energyCountDecrease.setTypeface(typeface);
    }

    public void setShopsEnabled(boolean enabled) {
        toolshopButton.setEnabled(enabled);
        shapeshopButton.setEnabled(enabled);
    }

    public void startAnimation(Animation animation) {
        layout.startAnimation(animation);
    }

    public void update() {
        toolshopButton.clearAnimation();

        User user = gameActivity.getUser();
        levelDisplay
                .setText(gameActivity.getString(R.string.topbar_level_format_d, user.getLevel()));
        levelProgress.setProgress(user.getEp());
        starCount.setText(user.getStarCount() + "");
        energyCount.setText(user.getEnergyCount() + "");

        gameActivity.getTutorialManager().onTopBarUpdate(this);
    }

    public void startToolShopButtonPulse() {
        toolshopButton.startAnimation(toolShopPulse);
    }

    public void showStarcountDecrease(int decrease) {
        starCountDecrease.setText(decrease + "");
        starCountDecrease.startAnimation(starDecreaseAnimation);
    }

    public void showEnergycountDecrease(int decrease) {
        energyCountDecrease.setText(decrease + "");
        energyCountDecrease.startAnimation(energyDecreaseAnimation);
    }

    public void setCustomCallerForScreenSwitch(Screen.ScreenType customCallerForScreenSwitch) {
        this.customCallerForScreenSwitch = customCallerForScreenSwitch;
    }

    @Override
    public void onClick(View v) {
        if (v.equals(backButton)) {
            parent.onBackKeyDown();
        }
        if (v.equals(toolshopButton)) {
            flipToScreen(Screen.ScreenType.TOOL_SHOP);
        } else if (v.equals(shapeshopButton)) {
            flipToScreen(Screen.ScreenType.SHAPE_SHOP);
        } else if (v.equals(settingsButton)) {
            flipToScreen(Screen.ScreenType.SETTINGS);
        }
    }

    private void flipToScreen(Screen.ScreenType screenType) {
        if (customCallerForScreenSwitch != Screen.ScreenType.NONE)
            gameActivity.flipToScreen(screenType, customCallerForScreenSwitch);
        gameActivity.flipToScreen(screenType);
    }

    public Screen.ScreenType getParentType() {
        return parent.getType();
    }

    public boolean isToolShopButtonEnabled() {
        return toolshopButton.isEnabled();
    }
}
