package de.frajul.endlessroll.views;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.tools.ToolUpgrade;

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

    private ToolInspector toolInspector;
    private Context context;

    private View layout;
    private TextView title;
    private ImageView imageView;
    private TextView valueOld;
    private TextView valueNew;
    private PriceButton priceButton;

    private ToolUpgrade upgrade;
    private boolean toolBought;
    private int availableEnergy;

    public ToolUpgradeView(ToolInspector toolInspector, Context context, Typeface typeface, View layout) {
        this.toolInspector = toolInspector;
        this.context = context;
        this.layout = layout;

        title = (TextView) layout.findViewById(R.id.tool_upgrade_title);
        title.setTypeface(typeface);
        imageView = (ImageView) layout.findViewById(R.id.tool_upgrade_imageview);
        valueOld = (TextView) layout.findViewById(R.id.tool_upgrade_value_old);
        valueOld.setTypeface(typeface);
        valueNew = (TextView) layout.findViewById(R.id.tool_upgrade_value_new);
        valueNew.setTypeface(typeface);
        priceButton = new PriceButton(context, typeface,
                layout.findViewById(R.id.tool_upgrade_pricebutton), this);
    }

    public void update(ToolUpgrade toolUpgrade, boolean toolBought, int availableEnergy) {
        this.upgrade = toolUpgrade;
        this.toolBought = toolBought;
        this.availableEnergy = availableEnergy;
        this.title.setText(context.getString(R.string.tool_upgrade_title_format_sd,
                context.getString(toolUpgrade.getType().getName()), toolUpgrade.getCurrentLevel()));
        this.imageView.setImageDrawable(
                context.getResources().getDrawable(toolUpgrade.getType().getDrawable()));
        updateValueViews();
        priceButton.init(R.string.price_button_upgrade, toolUpgrade.getPrice(),
                R.drawable.currency_energy);
        priceButton.setLayoutVisible(isAtMaxLevel() ? View.GONE : View.VISIBLE);
        priceButton.setLayoutEnabled(toolBought && toolUpgrade.getPrice() <= availableEnergy);
    }

    public void setLayoutVisiblity(int visiblity) {
        layout.setVisibility(visiblity);
    }

    private void updateValueViews() {
        String unit = upgrade.getType().getUnit();
        valueOld.setText(upgrade.getValueAtCurrentLevel() + unit);
        valueNew.setText(upgrade.getValueAtNextLevel() + unit);
        if (isAtMaxLevel())
            valueNew.setText(R.string.tool_upgrade_value_max);
    }

    private boolean isAtMaxLevel() {
        return upgrade.getCurrentLevel() >= upgrade.getMaxLevel();
    }

    @Override
    public void onClick(View v) {
        upgrade.setCurrentLevel(upgrade.getCurrentLevel() + 1);
        update(upgrade, toolBought, availableEnergy);
        toolInspector.onToolUpgraded(upgrade.getPrice());
    }
}
