package ch.pizzalemon.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import ch.pizzalemon.android.R;

/**
 * Created by cimenmus on 28/09/2017.
 */

public class AppButton extends LinearLayout {

    private View rootView;
    private boolean isEnabled;
    private Button pizzalinkButton;
    private String text;

    public AppButton(Context context) {
        super(context);
        init(context);
    }

    public AppButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AppButton, 0, 0);
        try {
            isEnabled = a.getBoolean(R.styleable.AppButton_isEnabled, true);
            text = a.getString(R.styleable.AppButton_text);
        } finally {
            a.recycle();
        }
        init(context);
    }

    private void init(Context context){
        rootView = inflate(context, R.layout.layout_pizzalink_button, this);
        pizzalinkButton = (Button) rootView.findViewById(R.id.pizzalinkButton);
        pizzalinkButton.setText(text);
        setButtonAppearance(context);
    }

    public void setEnabled(Context context, boolean isEnabled){
        this.isEnabled = isEnabled;
        setButtonAppearance(context);
    }

    public void setOnClickListenrer(OnClickListener onClickListenrer){
        rootView.setOnClickListener(onClickListenrer);
    }

    private void setButtonAppearance(Context context){
        if(isEnabled){
            pizzalinkButton.setBackground(ContextCompat.getDrawable(context, R.drawable.background_button_enabled_red));
            pizzalinkButton.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }else {
            pizzalinkButton.setBackground(ContextCompat.getDrawable(context, R.drawable.background_button_disabled));
            pizzalinkButton.setTextColor(ContextCompat.getColor(context, R.color.venus));
        }
    }

}
