package com.tsi.navi.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

import com.tsi.navi.R;

public class OutlineTextView extends androidx.appcompat.widget.AppCompatTextView {

    private boolean textStroke = true;
    private int textStrokeColor;
    private float textStrokeWidth = 20.0f;

    public OutlineTextView(Context context) {
        super(context);
    }

    public OutlineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public OutlineTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (textStroke) {
            ColorStateList states = getTextColors();
            getPaint().setStyle(Paint.Style.STROKE);
            getPaint().setStrokeWidth(textStrokeWidth);
            setTextColor(textStrokeColor);
            super.onDraw(canvas);
            getPaint().setStyle(Paint.Style.FILL);
            setTextColor(states);
        }
        super.onDraw(canvas);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView);
        textStroke = typeArray.getBoolean(R.styleable.OutlineTextView_textStroke, true);
        textStrokeWidth = typeArray.getFloat(R.styleable.OutlineTextView_textStrokeWidth, 6.0f);
        textStrokeColor = typeArray.getColor(R.styleable.OutlineTextView_textStrokeColor, 0xFFFFFFFF);
    }

}
