package de.frajul.endlessroll.views;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.main.screens.ToolShopScreen;

/**
 * Created by Julian on 16.07.2016.
 */
public class ToolOfferSlot implements View.OnClickListener {

    private ToolShopScreen toolShopScreen;
    private ToolType toolType;
    private boolean locked;
    private boolean selected;

    private int colorDisabled;
    private int colorDisabledSelected;
    private int colorEnabled;
    private int colorEnabledSelected;

    private LinearLayout layout;
    private TextView title;
    private ImageView image;

    public ToolOfferSlot(ToolShopScreen toolShopScreen, Context context, Typeface typeface, ToolType toolType) {
        this.toolShopScreen = toolShopScreen;
        this.toolType = toolType;
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = (LinearLayout) inflater.inflate(R.layout.tool_offer_slot, null);
        layout.setOnClickListener(this);
        FrameLayout slotLayout = (FrameLayout) layout.findViewById(R.id.toolofferslot_slot);
        title = (TextView) layout.findViewById(R.id.toolofferslot_title);
        title.setTypeface(typeface);
        image = (ImageView) slotLayout.findViewById(R.id.toolslot_image);
        image.setBackgroundDrawable(createImageBackground());

        colorDisabled = context.getResources().getColor(R.color.toolslotDisabled);
        colorDisabledSelected = context.getResources().getColor(R.color.toolslotDisabledSelected);
        colorEnabled = context.getResources().getColor(R.color.toolslotEnabled);
        colorEnabledSelected = context.getResources().getColor(R.color.toolslotEnabledSelected);
    }

    private Drawable createImageBackground() {
        GradientDrawable gd = new GradientDrawable();
        gd.setCornerRadius(10);
        return gd;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
        if (locked) {
            title.setText(R.string.tool_name_locked);
            image.setImageResource(R.drawable.tools_button_locked);
        } else {
            title.setText(toolType.getName());
            image.setImageResource(toolType.getButtonDrawable());
        }
    }

    public void updateBackgroundColor() {
        GradientDrawable gd = (GradientDrawable) image.getBackground();
        if (toolType.isBought())
            gd.setColor(selected ? colorEnabledSelected : colorEnabled);
        else
            gd.setColor(selected ? colorDisabledSelected : colorDisabled);
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        updateBackgroundColor();
    }

    public boolean isLocked() {
        return locked;
    }

    public ToolType getToolType() {
        return toolType;
    }

    public LinearLayout getLayout() {
        return layout;
    }

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