package de.frajul.endlessroll.main.screens;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.views.TopBar;

/**
 * Created by Julian on 13.02.2016.
 */
public abstract class Screen<V extends ViewGroup> {

    public enum ScreenType {
        NONE(-1),
        PRE_START(0),
        START(1),
        WORLDS(2),
        LEVELS(3),
        GAME(4),
        TOOL_SHOP(5),
        GL_TEST(6),
        SETTINGS(7),
        SHAPE_SHOP(8);

        private int inFlipperPosition;

        ScreenType(int inFlipperPosition) {
            this.inFlipperPosition = inFlipperPosition;
        }

        public int getInFlipperPosition() {
            return inFlipperPosition;
        }
    }

    private ScreenType type;
    protected ScreenType caller;
    protected V layout;
    protected GameActivity gameActivity;

    public Screen(ScreenType type, GameActivity gameActivity, @LayoutRes int layoutId) {
        this.type = type;
        this.gameActivity = gameActivity;
        layout = inflateLayout(gameActivity, layoutId);
    }

    private V inflateLayout(Context context, @LayoutRes int layoutId) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return (V) inflater.inflate(layoutId, null);
    }

    protected TopBar createTopBar(@IdRes int id) {
        return new TopBar(gameActivity, this, layout.findViewById(id));
    }

    public void setCaller(ScreenType caller) {
        this.caller = caller;
    }

    public abstract void prepareToBeShown();

    public abstract void onBackKeyDown();

    protected void flipToCaller() {
        if (caller != null)
            gameActivity.flipToScreen(caller);
    }

    protected void flipTo(ScreenType type) {
        gameActivity.flipToScreen(type);
    }

    public ScreenType getType() {
        return type;
    }

    public V get() {
        return layout;
    }

}
