package de.frajul.endlessroll.main.screens;

import android.view.Gravity;
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.levels.LevelPack;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.views.TopBar;
import de.frajul.endlessroll.views.WorldButton;
import de.frajul.endlessroll.views.WorldButtonOnClickListener;

/**
 * Created by Julian on 07.07.2016.
 */
public class WorldsScreen extends Screen<RelativeLayout> implements WorldButtonOnClickListener {

    private List<WorldButton> worldButtons = new ArrayList<>();
    private TopBar topBar;
    private LinearLayout buttonLayout;

    public WorldsScreen(GameActivity gameActivity) {
        super(ScreenType.WORLDS, gameActivity, R.layout.worlds);
        topBar = super.createTopBar(R.id.worlds_topbar);
        buttonLayout = (LinearLayout) layout.findViewById(R.id.worlds_layout);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        params.setMargins(25, 0, 25, 0);

        for (LevelPack levelPack : gameActivity.getLevelManager()) {
            WorldButton button = new WorldButton(gameActivity, levelPack, this);
            buttonLayout.addView(button.getView(), params);
            worldButtons.add(button);
        }
    }

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

        topBar.update();
        for (WorldButton button : worldButtons)
            button.update();
    }

    @Override
    public void onBackKeyDown() {
        flipTo(ScreenType.START);
    }

    @Override
    public void onClick(WorldButton worldButton) {
        LevelPack levelPack = worldButton.getLevelPack();
        if (!levelPack.isLocked()) {
            gameActivity.onWorldSelected(levelPack);
            gameActivity.flipToScreen(ScreenType.LEVELS);
        }
    }
}
