package com.atmob.voiceai.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.atmob.voiceai.R;

public class GradientBorderView extends View {


    private int startColor;
    private int endColor;
    private int strokeWidth;
    private Paint bgPaint;
    private Paint centerPaint;
    RectF bgRectF;
    RectF centerRectF;
    private float radius;

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

    public GradientBorderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GradientBorderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        startColor = 0xff000000;
        endColor = 0xff000000;
        strokeWidth = 12;
        centerRectF = new RectF();
        bgRectF = new RectF();

        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        bgPaint.setColor(getContext().getResources().getColor(R.color.colorPrimaryVariant));
        bgPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));

        centerPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        centerPaint.setColor(getContext().getResources().getColor(R.color.voice_record_0_20));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        bgRectF.set(0, 0, w, h);
        centerRectF.set(strokeWidth, strokeWidth, w - strokeWidth, h - strokeWidth);
        radius = h / 2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(bgRectF, radius, radius, bgPaint);
        canvas.drawRoundRect(centerRectF, radius, radius, centerPaint);
    }
}