package de.frajul.endlessroll.views;

import android.graphics.Typeface;
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.LevelPack;
import de.frajul.endlessroll.main.GameActivity;

/**
 * Created by Julian on 01.08.2016.
 */
public class WorldButton implements View.OnClickListener {

    private GameActivity gameActivity;
    private LevelPack levelPack;
    private WorldButtonOnClickListener clickListener;

    private View layout;
    private TextView title;
    private ImageView previewImage;
    private View lock;
    private TextView levelCount;
    private TextView starCount;
    private TextView energyCount;

    public WorldButton(GameActivity gameActivity, LevelPack levelPack, WorldButtonOnClickListener clickListener) {
        this.gameActivity = gameActivity;
        this.levelPack = levelPack;
        this.clickListener = clickListener;

        LayoutInflater inflater = LayoutInflater.from(gameActivity);
        layout = inflater.inflate(R.layout.world_button, null);
        layout.setOnClickListener(this);

        Typeface typeface = gameActivity.getTypeface();
        title = (TextView) layout.findViewById(R.id.worldbutton_title);
        title.setTypeface(typeface);
        previewImage = (ImageView) layout.findViewById(R.id.worldbutton_preview);
        lock = layout.findViewById(R.id.worldbutton_lock);
        levelCount = (TextView) layout.findViewById(R.id.worldbutton_levelcount);
        levelCount.setTypeface(typeface);
        starCount = (TextView) layout.findViewById(R.id.worldbutton_starcount);
        starCount.setTypeface(typeface);
        energyCount = (TextView) layout.findViewById(R.id.worldbutton_energycount);
        energyCount.setTypeface(typeface);

        update();
    }

    public void update() {
        title.setText(levelPack.getName());
        previewImage.setImageDrawable(
                gameActivity.getResources().getDrawable(levelPack.getWorld().getPreviewId()));
        levelCount.setText(gameActivity
                .getString(R.string.world_button_count_format_dd, levelPack.getFinishedLevelCount(),
                        levelPack.getLevels().size()));
        starCount.setText(gameActivity
                .getString(R.string.world_button_count_format_dd, levelPack.getCollectedStarCount(),
                        levelPack.getAvailableStars()));
        energyCount.setText(gameActivity.getString(R.string.world_button_count_format_dd,
                levelPack.getCollectedEnergyCount(), levelPack.getAvailableEnergy()));
        setLockVisible(levelPack.isLocked());
    }

    public void setLockVisible(boolean locked) {
        int visibility = locked ? View.VISIBLE : View.GONE;
        lock.setVisibility(visibility);
    }

    public LevelPack getLevelPack() {
        return levelPack;
    }

    public View getView() {
        return layout;
    }

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