package com.cooleshow.base.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;


import com.cooleshow.base.R;

import androidx.appcompat.widget.AppCompatTextView;


public class ColorTextView extends AppCompatTextView {
    private int ctvBackgroundColor;
    private int cllBorderColor;
    private int cllBorderWidth;
    /**
     * 圆角大小
     */
    private int mCornerSize;

    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mtitleBound;
    private Paint mtitlePaint;

    public ColorTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    public void setCtvBackgroundColor(int ctvBackgroundColor) {
        this.ctvBackgroundColor = ctvBackgroundColor;
        postInvalidate();
    }

    /**
     * 获得我自定义的样式属性
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public ColorTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        /**
         * 获得我们所定义的自定义样式属性
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorTextView, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.ColorTextView_ctvBackground) {//默认为白色
                ctvBackgroundColor = a.getColor(attr, Color.WHITE);
            } else if (attr == R.styleable.ColorTextView_ctvCornerSize) {//默认圆角为0
                mCornerSize = a.getDimensionPixelSize(attr, 0);
            }
            if (attr == R.styleable.ColorTextView_ctvBorderColor) {//默认圆角为0
                cllBorderColor = a.getColor(attr, Color.WHITE);
            }
            if (attr == R.styleable.ColorTextView_ctvBorderWidth) {//默认圆角为0
                cllBorderWidth = a.getDimensionPixelSize(attr, 0);
            }
        }

        a.recycle();
        mtitleBound = new Rect();
        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(ctvBackgroundColor);
        paint2 = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint2.setColor(cllBorderColor);
        paint2.setAntiAlias(true);
        paint2.setStrokeWidth(cllBorderWidth);
        paint2.setStyle(Paint.Style.STROKE);
    }

    Paint paint;
    Paint paint2;
    RectF rec;
    RectF rec2;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rec = new RectF(cllBorderWidth*1.0f/2, cllBorderWidth*1.0f/2,
                getMeasuredWidth()-cllBorderWidth*1.0f/2,
                getMeasuredHeight()-cllBorderWidth*1.0f/2);
        rec2 = new RectF(cllBorderWidth*1.0f/2, cllBorderWidth*1.0f/2,
                getMeasuredWidth()-cllBorderWidth*1.0f/2,
                getMeasuredHeight()-cllBorderWidth*1.0f/2);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(ctvBackgroundColor);
        paint2.setColor(cllBorderColor);
        if (cllBorderWidth != 0) {
            canvas.drawRoundRect(rec,mCornerSize,mCornerSize, paint);
            canvas.drawRoundRect(rec2,mCornerSize,mCornerSize, paint2);
        } else {
            canvas.drawRoundRect(rec, mCornerSize, mCornerSize, paint);
        }
        super.onDraw(canvas);
    }
}
