package kz.arta.synergy.workflow.mobile.android.common.components;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;

import info.hoang8f.widget.FButton;
import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.common.utils.Utils;

/**
 * Created by neox on 31.10.14.
 * Основные кнопки приложения (Сохранить, Готово и прочие)
 * Трех цветов: зеленый, красный, серый
 */
public class AButton extends FButton {

    public static final String GREEN = "GREEN";
    public static final String RED = "RED";
    public static final String GRAY = "GRAY";
    public static final int MARGINS = 15;
    public static final int HEIGHT = 2;
    public static final int RADIUS = 1;

    public AButton(Context context) {
        this(context, null);
    }

    public AButton(Context context, AttributeSet attrs) {

        super(context, attrs);

        final int pixelMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGINS, getResources().getDisplayMetrics());
        final int pixelHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, HEIGHT, getResources().getDisplayMetrics());
        final int pixelRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, RADIUS, getResources().getDisplayMetrics());

        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) (getResources().getDimension(R.dimen.height_buttons)));

        lp.setMargins(0, pixelMargin, 0, 0);

        setTextColor(getResources().getColor(R.color.text_color_buttons));
        setTextSize(Utils.getPxFromDp(R.dimen.text_size_buttons, getContext()));
        setTypeface(Fonts.getMedium(getContext()));
        setLayoutParams(lp);
        setShadowHeight(pixelHeight);
        setCornerRadius(pixelRadius);
        setMaxLines(1);
        setEllipsize(TextUtils.TruncateAt.END);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AButton);
        for (int i = 0; i < a.getIndexCount(); ++i) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.AButton_color1:
                    setBackground(a.getString(attr));
                    break;
            }
        }
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getLineCount() > 1) {
            setTextSize(Utils.getPxFromDp(R.dimen.text_size_buttons_2_lines, getContext()));
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setBackground(String type) {
        switch (type) {
            case GREEN:
                setButtonColor(getResources().getColor(R.color.green_button));
                break;
            case RED:
                setButtonColor(getResources().getColor(R.color.red_button));
                break;
            case GRAY:
                setButtonColor(getResources().getColor(R.color.gray_button));
                break;
        }
    }
}
