package de.frajul.endlessroll.main.screens;

import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.shapes.PlayerShape;
import de.frajul.endlessroll.entities.shapes.PlayerShapeButtonOnClickListener;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.views.PlayerShapeButton;
import de.frajul.endlessroll.views.ShapeInspector;
import de.frajul.endlessroll.views.TopBar;

/**
 * Created by Julian on 08.07.2016.
 */
public class PlayerShapeShopScreen extends Screen<RelativeLayout> implements PlayerShapeButtonOnClickListener {

    private PlayerShapeButton activeButton;

    private ShapeInspector shapeInspector;

    private TopBar topBar;
    private LinearLayout topRow;
    private LinearLayout bottomRow;

    public PlayerShapeShopScreen(GameActivity gameActivity) {
        super(ScreenType.SHAPE_SHOP, gameActivity, R.layout.shape_shop);
        topBar = super.createTopBar(R.id.shape_shop_topbar);
        topRow = (LinearLayout) layout.findViewById(R.id.shape_shop_topRow);
        bottomRow = (LinearLayout) layout.findViewById(R.id.shape_shop_bottomRow);
        shapeInspector = new ShapeInspector(gameActivity,
                layout.findViewById(R.id.shape_shop_shape_inspector));
    }

    private void createViews() {
        topRow.removeAllViews();
        bottomRow.removeAllViews();
        int i = 0;
        int totalShapes = PlayerShape.values().length;
        for (PlayerShape playerShape : PlayerShape.values()) {
            playerShape.getUnlockTask().update(gameActivity.getLevelManager());
            boolean lastInRow = i == totalShapes / 2 || i == totalShapes - 1;
            boolean locked = !playerShape.getUnlockTask()
                    .isConditionFulfilled();
            PlayerShapeButton button = new PlayerShapeButton(gameActivity, playerShape, this,
                    locked, !lastInRow);
            if (i < totalShapes / 2 + 1)
                topRow.addView(button.getView());
            else
                bottomRow.addView(button.getView());

            if (playerShape.equals(gameActivity.getUser().getCurrentPlayerShape())) {
                this.activeButton = button;
                button.startRotating();
            }

            i++;
        }

        shapeInspector.update(activeButton.getPlayerShape(), activeButton.isLocked());
    }

    @Override
    public void prepareToBeShown() {
        gameActivity.getSoundManager().menuMusic.start();

        createViews();
        topBar.update();
        topBar.setCustomCallerForScreenSwitch(super.caller);
    }

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

    @Override
    public void onClick(PlayerShapeButton button) {
        shapeInspector.update(button.getPlayerShape(), button.isLocked());
        if (!button.isLocked()) {
            activeButton.stopRotating();
            activeButton = button;
            activeButton.startRotating();
            gameActivity.getUser().setCurrentPlayerShape(button.getPlayerShape());
        }
    }
}
