package de.frajul.endlessroll.main.screens;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.tools.ToolSlot;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.main.DataStorageHandler;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.tutorial.ToolShopTutorial;
import de.frajul.endlessroll.user.LevelUpBounties;
import de.frajul.endlessroll.user.ToolSlotSettings;
import de.frajul.endlessroll.views.ToolInspector;
import de.frajul.endlessroll.views.ToolOfferSlot;
import de.frajul.endlessroll.views.TopBar;

/**
 * Created by Julian on 08.07.2016.
 */
public class ToolShopScreen extends Screen<RelativeLayout> implements View.OnClickListener {

    private ToolSlotSettings slotSettings;
    private ToolOfferSlot selectedToolOfferSlot;

    private TopBar topBar;
    private LinearLayout toolOfferTopRow;
    private LinearLayout toolOfferBottomRow;
    private List<ImageView> toolSlotViews = new ArrayList<>();
    private List<ToolOfferSlot> toolOfferSlots = new ArrayList<>();

    private Animation pulse;

    private ToolInspector toolInspector;
    private ToolShopTutorial tutorial;

    public ToolShopScreen(GameActivity gameActivity) {
        super(ScreenType.TOOL_SHOP, gameActivity, R.layout.toolshop);
        this.slotSettings = gameActivity.getUser().getToolSlotSettings();
        this.tutorial = gameActivity.getTutorialManager().getToolShopTutorial();
        tutorial.setToolShopScreen(this);
        pulse = AnimationUtils.loadAnimation(gameActivity, R.anim.pulse);

        topBar = super.createTopBar(R.id.toolshop_topbar);
        toolSlotViews.add(getToolSlotView(R.id.toolshop_slot1));
        toolSlotViews.add(getToolSlotView(R.id.toolshop_slot2));
        toolSlotViews.add(getToolSlotView(R.id.toolshop_slot3));
        toolSlotViews.add(getToolSlotView(R.id.toolshop_slot4));
        toolOfferTopRow = (LinearLayout) layout.findViewById(R.id.toolshop_tool_offer_top_row);
        toolOfferBottomRow = (LinearLayout) layout
                .findViewById(R.id.toolshop_tool_offer_bottom_row);

        toolInspector = new ToolInspector(this, gameActivity,
                layout.findViewById(R.id.toolshop_toolinspector));

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 0, 10, 0);

        int i = 0;
        for (ToolType type : ToolType.values()) {
            i++;
            ToolOfferSlot slot = new ToolOfferSlot(this, gameActivity, gameActivity.getTypeface(),
                    type);
            toolOfferSlots.add(slot);
            if (i > 5)
                toolOfferBottomRow.addView(slot.getLayout(), params);
            else
                toolOfferTopRow.addView(slot.getLayout(), params);
        }
    }

    private ImageView getToolSlotView(int id) {
        FrameLayout slotLayout = (FrameLayout) layout.findViewById(id);
        ImageView imageView = (ImageView) slotLayout.findViewById(R.id.toolslot_image);
        imageView.setOnClickListener(this);
        return imageView;
    }

    @Override
    public void prepareToBeShown() {
        topBar.update();
        slotSettings.unlockSlotsIfLevelReached(gameActivity.getUser().getLevelUpBounties());
        onToolOfferSlotSelected(toolOfferSlots.get(0));

        for (int i = 0; i < toolSlotViews.size(); i++) {
            ToolSlot toolSlot = slotSettings.get(i);
            ImageView toolSlotView = toolSlotViews.get(i);
            toolSlotView.setImageResource(toolSlot.getDrawable());
        }
        for (ToolOfferSlot toolOfferSlot : toolOfferSlots) {
            boolean locked = gameActivity.getUser().getLevelUpBounties().isToolLocked(toolOfferSlot.getToolType());
            toolOfferSlot.setLocked(locked);
            toolOfferSlot.updateBackgroundColor();
        }
        tutorial.onToolShopScreenPrepare();
    }

    public void onToolBought(int price, ToolType toolType) {
        topBar.showStarcountDecrease(-price);
        gameActivity.getUser().increaseStarCount(-price, false);
        topBar.update();
        DataStorageHandler dataStorageHandler = gameActivity.getDataStorageHandler();
        dataStorageHandler.getDatabase().open();
        dataStorageHandler.getDatabase().writeToolData();
        dataStorageHandler.getDatabase().close();
        for (ToolOfferSlot toolOfferSlot : toolOfferSlots) {
            toolOfferSlot.updateBackgroundColor();
        }
        tutorial.onToolBought();
    }

    public void onToolUpgraded(int price) {
        topBar.showEnergycountDecrease(-price);
        gameActivity.getUser().increaseEnergyCount(-price, false);
        topBar.update();
        DataStorageHandler dataStorageHandler = gameActivity.getDataStorageHandler();
        dataStorageHandler.getDatabase().open();
        dataStorageHandler.getDatabase().writeToolData();
        dataStorageHandler.getDatabase().close();
    }

    public void onToolOfferSlotSelected(ToolOfferSlot slot) {
        selectedToolOfferSlot = slot;
        for (ToolOfferSlot toolOfferSlot : toolOfferSlots)
            toolOfferSlot.setSelected(toolOfferSlot.equals(slot));
        toolInspector.update(slot.getToolType(), slot.isLocked());
        tutorial.onToolSelected();
    }

    private void onToolEquipped(int index) {
        slotSettings.changeToolSlotType(index, selectedToolOfferSlot.getToolType());
        for (int i = 0; i < toolSlotViews.size(); i++) {
            ToolSlot toolSlot = slotSettings.get(i);
            ImageView view = toolSlotViews.get(i);
            view.setImageResource(toolSlot.getDrawable());
        }
        tutorial.onToolEquipped();
    }

    public void stopTutorialAnimations(){
        for (ToolOfferSlot toolOfferSlot : toolOfferSlots)
            if(toolOfferSlot.getToolType() == ToolType.SPRING)
                toolOfferSlot.getLayout().clearAnimation();

        toolInspector.clearPriceButtonAnimation();

        toolSlotViews.get(0).clearAnimation();
    }

    public void tutorialStartSpringOfferSlotPulse(){
        for (ToolOfferSlot toolOfferSlot : toolOfferSlots)
            if(toolOfferSlot.getToolType() == ToolType.SPRING)
                toolOfferSlot.getLayout().startAnimation(pulse);
    }

    public void tutorialStartSpringBuyButtonPulse(){
        toolInspector.startPriceButtonAnimation(pulse);
    }

    public void tutorialStartSpringToolSlotPulse(){
         toolSlotViews.get(0).startAnimation(pulse);
    }

    public void writeToolShopTutorialIsFinished(){
        gameActivity.getDataStorageHandler().writeToolShopTutorialFinished(true);
    }


    @Override
    public void onBackKeyDown() {
        stopTutorialAnimations();
        gameActivity.getDataStorageHandler().writeUserData(gameActivity.getUser());
        flipToCaller();
    }

    @Override
    public void onClick(View v) {
        ImageView toolSlotView = (ImageView) v;
        int index = toolSlotViews.indexOf(toolSlotView);
        if (canSelectedToolBePutInSlot(index)) {
            onToolEquipped(index);
        }
    }

    private boolean canSelectedToolBePutInSlot(int slotIndex) {
        return slotIndex != -1 && !slotSettings.get(slotIndex)
                .isLocked() && selectedToolOfferSlot != null && selectedToolOfferSlot.getToolType()
                .isBought();
    }

    public ToolType getSelectedToolType() {
        return selectedToolOfferSlot.getToolType();
    }

}
