package de.frajul.endlessroll.views;

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.screens.Screen;
import de.frajul.endlessroll.user.LevelBounty;

/**
 * Created by Julian on 15.07.2016.
 */
public class LevelupMessage implements View.OnClickListener, BountyMessage.ScreenSwitchCaller {

    private GameActivity gameActivity;
    private FrameLayout layout;

    private TextView levelView;
    private TextView text;
    private LinearLayout unlockMessages;

    public LevelupMessage(GameActivity gameActivity) {
        this.gameActivity = gameActivity;

        Typeface typeface = gameActivity.getTypeface();
        LayoutInflater inflater = LayoutInflater.from(gameActivity);
        layout = (FrameLayout) inflater.inflate(R.layout.levelup_message, null);
        layout.setOnClickListener(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setVisibility(View.GONE);
        levelView = (TextView) layout.findViewById(R.id.levelup_level);
        levelView.setTypeface(typeface);
        text = (TextView) layout.findViewById(R.id.levelup_text);
        text.setTypeface(typeface);
        unlockMessages = (LinearLayout) layout.findViewById(R.id.levelup_unlocklist);
    }

    public void show(int level) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 0, 5);

        levelView.setText(level + "");

        LevelBounty bounty = gameActivity.getUser().getLevelUpBounties().get(level);
        if (bounty != null)
            for (BountyMessage message : bounty.createBountyMessages(gameActivity, this))
                unlockMessages.addView(message.getLayout(), params);

        layout.setVisibility(View.VISIBLE);
    }

    private void hide() {
        layout.setVisibility(View.GONE);
        unlockMessages.removeAllViews();
    }

    public FrameLayout getLayout() {
        return layout;
    }

    @Override
    public void onClick(View v) {
        hide();
    }


    @Override
    public void switchScreen(Screen.ScreenType screenType) {
        gameActivity.flipToScreen(screenType);
        hide();
    }
}
