package de.frajul.endlessroll.main.tutorial;

import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.screens.Screen;
import de.frajul.endlessroll.main.screens.ToolShopScreen;
import de.frajul.endlessroll.user.LevelUpBounties;
import de.frajul.endlessroll.views.TopBar;

/**
 * Created by Julian on 10.06.2017.
 */

public class ToolShopTutorial {

    private GameActivity gameActivity;
    private LevelUpBounties levelUpBounties;

    private ToolShopScreen toolShopScreen;
    private boolean finished = false;

    public ToolShopTutorial(GameActivity gameActivity) {
        this.gameActivity = gameActivity;
        levelUpBounties = new LevelUpBounties(0);
    }

    public void setToolShopScreen(ToolShopScreen toolShopScreen) {
        this.toolShopScreen = toolShopScreen;
    }

    public void onTopBarUpdate(TopBar topBar) {
        if (!finished && isSpringUnlocked()) {
            boolean notOnToolShopScreen = topBar.getParent() != Screen.ScreenType.TOOL_SHOP;
            if (notOnToolShopScreen && topBar.isToolShopButtonEnabled())
                topBar.startToolShopButtonPulse();
        }
    }

    public void onToolShopScreenPrepare() {
        onToolShopUpdate();
    }

    public void onToolSelected() {
        onToolShopUpdate();
    }

    public void onToolBought() {
        onToolShopUpdate();
    }

    public void onToolEquipped() {
        if (toolShopScreen.getSelectedToolType() == ToolType.SPRING) {
            toolShopScreen.stopTutorialAnimations();
            finished = true;
            toolShopScreen.writeToolShopTutorialIsFinished();
        }
    }

    private void onToolShopUpdate() {
        if (!finished && isSpringUnlocked()) {
            boolean springSelected = toolShopScreen.getSelectedToolType() == ToolType.SPRING;
            boolean springBought = ToolType.SPRING.isBought();

            boolean isSelectSpring = !springSelected;
            boolean isBuySpring = springSelected && !springBought;
            boolean isEquipSpring = springSelected && springBought;

            if (isSelectSpring) {
                toolShopScreen.stopTutorialAnimations();
                toolShopScreen.tutorialStartSpringOfferSlotPulse();
            } else if (isBuySpring) {
                toolShopScreen.stopTutorialAnimations();
                toolShopScreen.tutorialStartSpringBuyButtonPulse();
            } else if (isEquipSpring) {
                toolShopScreen.stopTutorialAnimations();
                toolShopScreen.tutorialStartSpringToolSlotPulse();
            }
        }
    }

    private boolean isSpringUnlocked() {
        levelUpBounties.loadAllForLevel(gameActivity.getUser().getLevel());
        return !levelUpBounties.isToolLocked(ToolType.SPRING);
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }
}
