package com.secureline.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

public class SecureLineStatusBadge extends TextView {
    public static final int TYPE_NEUTRAL = 0;
    public static final int TYPE_ACTIVE = 1;
    public static final int TYPE_WARNING = 2;
    public static final int TYPE_DANGER = 3;
    public static final int TYPE_INFO = 4;

    public SecureLineStatusBadge(Context context) { this(context, null); }
    public SecureLineStatusBadge(Context context, AttributeSet attrs) { this(context, attrs, 0); }
    public SecureLineStatusBadge(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setGravity(Gravity.CENTER);
        setTextSize(11);
        setAllCaps(true);
        setLetterSpacing(0.08f);
        setTypeface(android.graphics.Typeface.create("sans-serif-medium", android.graphics.Typeface.NORMAL));
        setPadding(dp(11), dp(6), dp(11), dp(6));
        int type = TYPE_NEUTRAL;
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SecureLineStatusBadge);
            setText(a.getText(R.styleable.SecureLineStatusBadge_slBadgeText));
            type = a.getInt(R.styleable.SecureLineStatusBadge_slBadgeType, TYPE_NEUTRAL);
            a.recycle();
        }
        setBadgeType(type);
    }

    public void setBadgeType(int type) {
        int textColor;
        int surfaceColor;
        int borderColor;
        switch (type) {
            case TYPE_ACTIVE:
                textColor = R.color.sl_color_text_primary;
                surfaceColor = R.color.sl_color_blue;
                borderColor = R.color.sl_color_blue_light;
                break;
            case TYPE_WARNING:
                textColor = R.color.sl_color_warning;
                surfaceColor = R.color.sl_color_warning_surface;
                borderColor = R.color.sl_color_warning_border;
                break;
            case TYPE_DANGER:
                textColor = R.color.sl_color_danger_soft;
                surfaceColor = R.color.sl_color_danger_surface;
                borderColor = R.color.sl_color_danger_border;
                break;
            case TYPE_INFO:
                textColor = R.color.sl_color_blue_pale;
                surfaceColor = R.color.sl_color_info_surface;
                borderColor = R.color.sl_color_info_border;
                break;
            default:
                textColor = R.color.sl_color_text_secondary;
                surfaceColor = R.color.sl_color_pressed;
                borderColor = R.color.sl_color_border_strong;
        }
        setTextColor(getResources().getColor(textColor, getContext().getTheme()));
        GradientDrawable bg = new GradientDrawable();
        bg.setColor(getResources().getColor(surfaceColor, getContext().getTheme()));
        bg.setStroke(dp(1), getResources().getColor(borderColor, getContext().getTheme()));
        bg.setCornerRadius(dp(999));
        setBackground(bg);
    }
    private int dp(int value) { return Math.round(value * getResources().getDisplayMetrics().density); }
}
