package de.frajul.endlessroll.views;

import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.levels.Level;
import de.frajul.endlessroll.main.GameActivity;

/**
 * Created by Julian on 23.04.2016.
 */
public class GoalMessageLevelButton implements View.OnClickListener {

    private GameActivity gameActivity;
    private GoalMessageLevelButtonOnClickListener clickListener;
    private Level level;

    private View layout;
    private TextView text;
    private ImageView star1;
    private ImageView star2;
    private ImageView star3;
    private ImageView energy;

    public GoalMessageLevelButton(GameActivity gameActivity, GoalMessageLevelButtonOnClickListener clickListener, View layout) {
        this.gameActivity = gameActivity;
        this.clickListener = clickListener;
        this.layout = layout;
        layout.setOnClickListener(this);
        findViews(layout);
    }

    private void findViews(View layout) {
        text = (TextView) layout.findViewById(R.id.goal_message_levelbutton_textview);
        text.setTypeface(gameActivity.getTypeface());
        star1 = (ImageView) layout.findViewById(R.id.goal_message_levelbutton_star1);
        star2 = (ImageView) layout.findViewById(R.id.goal_message_levelbutton_star2);
        star3 = (ImageView) layout.findViewById(R.id.goal_message_levelbutton_star3);
        energy = (ImageView) layout.findViewById(R.id.goal_message_levelbutton_energy);
    }

    public void setVisible(boolean visible){
        layout.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public void init(@StringRes int textId, Level level) {
        this.level = level;
        text.setText(gameActivity.getString(textId, level.getId()));
        showCollectedCurrency(level.getCollectedStars(), level.isEnergyCollected());
    }

    private void showCollectedCurrency(boolean[] stars, boolean energy) {
        if (stars[0])
            this.star1.setImageResource(R.drawable.currency_star);
        if (stars[1])
            this.star2.setImageResource(R.drawable.currency_star);
        if (stars[2])
            this.star3.setImageResource(R.drawable.currency_star);
        if (energy)
            this.energy.setImageResource(R.drawable.currency_energy);
    }

    public Level getLevel() {
        return level;
    }

    public View getView(){
        return layout;
    }

    @Override
    public void onClick(View v) {
        clickListener.onClick(this);
    }
}
