package de.frajul.endlessroll.views;

import android.graphics.Typeface;
import android.view.View;
import android.view.animation.Animation;
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 TextView levelView;
    private ImageView imageView;
    private PriceButton priceButton;
    private TextView description;

    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);
        levelView = (TextView) layout.findViewById(R.id.tool_inspector_level_view);
        levelView.setTypeface(typeface);
        imageView = (ImageView) layout.findViewById(R.id.tool_inspector_imageview);
        priceButton = new PriceButton(gameActivity, typeface,
                layout.findViewById(R.id.tool_inspector_pricebutton), this);
        description = (TextView) layout.findViewById(R.id.tool_inspector_description);
        description.setTypeface(typeface);
    }

    public void update(ToolType toolType, boolean locked) {
        this.toolType = toolType;
        this.locked = locked;
        title.setText(locked ? R.string.tool_name_locked : toolType.getName());
        levelView.setText(gameActivity
                .getString(R.string.tool_level_format_d, toolType.getCurrentUpgradeLevel()));
        levelView.setVisibility(locked ? View.INVISIBLE : View.VISIBLE);
        imageView.setImageDrawable(gameActivity.getResources().getDrawable(
                locked ? R.drawable.tools_button_locked : toolType.getButtonDrawable()));

        String descriptionLocked = gameActivity.getString(R.string.tool_description_locked_format_d, gameActivity.getUser().getLevelUpBounties().getLevelToolIsUnlocked(toolType));
        String toolDescription = gameActivity.getString(toolType.getDescription());
        description.setText(locked ? descriptionLocked : toolDescription);

        if (!toolType.isBought()) {
            priceButton.init(R.string.price_button_buy, toolType.getBuyPrice(),
                    R.drawable.currency_star);
            priceButton.setLayoutVisible(locked ? View.GONE : View.VISIBLE);
            priceButton.setLayoutEnabled(
                    toolType.getBuyPrice() <= gameActivity.getUser().getStarCount());
        } else {
            priceButton.init(R.string.price_button_upgrade, toolType.getUpgradePrice(),
                    R.drawable.currency_energy);
            priceButton.setLayoutVisible(toolType.isAtMaxUpgradeLevel() ? View.GONE : View.VISIBLE);
            priceButton.setLayoutEnabled(
                    toolType.getUpgradePrice() <= gameActivity.getUser().getEnergyCount());
        }
    }

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

    @Override
    public void onClick(View v) {
        if (!toolType.isBought()) {
            toolType.setBought(true);
            update(toolType, locked);
            toolShopScreen.onToolBought(toolType.getBuyPrice(), toolType);
        } else {
            onToolUpgraded(toolType.getUpgradePrice());
        }
    }

    public void startPriceButtonAnimation(Animation animation){
        priceButton.startAnimation(animation);
    }

    public void clearPriceButtonAnimation(){
        priceButton.clearAnimation();
    }

}
