package de.frajul.endlessroll.views;

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.ToolType;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.screens.ToolShopScreen;

/**
 * Created by Julian on 03.06.2017.
 */

public class ToolInspector implements View.OnClickListener {

    private GameActivity gameActivity;
    private ToolShopScreen toolShopScreen;

    private TextView title;
    private ImageView imageView;
    private PriceButton priceButton;

    private ToolUpgradeView toolUpgradeView0;
    private ToolUpgradeView toolUpgradeView1;

    private ToolType toolType;
    private boolean locked;

    public ToolInspector(ToolShopScreen toolShopScreen, GameActivity gameActivity, View layout) {
        this.toolShopScreen = toolShopScreen;
        this.gameActivity = gameActivity;
        Typeface typeface = gameActivity.getTypeface();
        title = (TextView) layout.findViewById(R.id.tool_inspector_title);
        title.setTypeface(typeface);
        imageView = (ImageView) layout.findViewById(R.id.tool_inspector_imageview);
        priceButton = new PriceButton(gameActivity, typeface,
                layout.findViewById(R.id.tool_inspector_pricebutton), this);
        toolUpgradeView0 = new ToolUpgradeView(this, gameActivity, typeface,
                layout.findViewById(R.id.tool_inspector_toolupgrade0));
        toolUpgradeView1 = new ToolUpgradeView(this, gameActivity, typeface,
                layout.findViewById(R.id.tool_inspector_toolupgrade1));
    }

    public void update(ToolType toolType, boolean locked) {
        this.toolType = toolType;
        this.locked = locked;
        this.title.setText(locked ? R.string.tool_name_unknown : toolType.getName());
        this.imageView.setImageDrawable(gameActivity.getResources().getDrawable(
                locked ? R.drawable.tools_lockedbutton : toolType.getButtonDrawable()));
        priceButton.init(R.string.price_button_buy, toolType.getPrice(), R.drawable.currency_star);
        priceButton.setLayoutVisible(toolType.isBought() || locked ? View.GONE : View.VISIBLE);
        priceButton.setLayoutEnabled(toolType.getPrice() <= gameActivity.getUser().getStarCount());

        toolUpgradeView0.update(toolType.getUpgrades()[0], toolType.isBought(),
                gameActivity.getUser().getEnergyCount());
        toolUpgradeView0.setLayoutVisiblity(locked ? View.GONE : View.VISIBLE);

        if (toolType.getUpgrades().length <= 1 || locked)
            toolUpgradeView1.setLayoutVisiblity(View.GONE);
        else {
            toolUpgradeView1.setLayoutVisiblity(View.VISIBLE);
            toolUpgradeView1.update(toolType.getUpgrades()[1], toolType.isBought(),
                    gameActivity.getUser().getEnergyCount());
        }
    }

    public void onToolUpgraded(int price) {
        toolShopScreen.onToolUpgraded(price);
        update(toolType, locked);
    }

    @Override
    public void onClick(View v) {
        toolType.setBought(true);
        update(toolType, locked);
        toolShopScreen.onToolBought(toolType.getPrice(), toolType);
    }
}
