package com.guadou.cs_cptservices.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.guadou.cs_cptservices.R;
import com.guadou.lib_baselib.utils.CommUtils;

import androidx.appcompat.widget.AppCompatImageView;

public class CustomRound6ImageView extends AppCompatImageView {
    float width, height;
    private int defaultRadius = CommUtils.dip2px(6);

    private int leftTopRadius;
    private int rightTopRadius;
    private int rightBottomRadius;
    private int leftBottomRadius;

    public CustomRound6ImageView(Context context) {
        this(context, null);
        init(context, null);
    }

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

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

    private void init(Context context, AttributeSet attrs) {
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }

//        setBackground(getContext().getResources().getDrawable(R.drawable.iv_emergency_report_detail_pictrue));

        initAttribute(attrs);

    }

    private void initAttribute(AttributeSet attrs) {

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.custom_round_image);

        float leftTop = typedArray.getDimension(R.styleable.custom_round_image_conner_radius_left_top, defaultRadius);
        float leftBottom = typedArray.getDimension(R.styleable.custom_round_image_conner_radius_left_bottom, defaultRadius);
        float rightTop = typedArray.getDimension(R.styleable.custom_round_image_conner_radius_right_top, defaultRadius);
        float rightBottom = typedArray.getDimension(R.styleable.custom_round_image_conner_radius_right_bottom, defaultRadius);

        leftTopRadius = CommUtils.dip2px((int) leftTop);
        leftBottomRadius = CommUtils.dip2px((int) leftBottom);
        rightTopRadius = CommUtils.dip2px((int) rightTop);
        rightBottomRadius = CommUtils.dip2px((int) rightBottom);


        typedArray.recycle();

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
        int maxRight = Math.max(rightTopRadius, rightBottomRadius);
        int minWidth = maxLeft + maxRight;
        int maxTop = Math.max(leftTopRadius, rightTopRadius);
        int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
        int minHeight = maxTop + maxBottom;
        if (width >= minWidth && height > minHeight) {
            Path path = new Path();
            //四个角：右上，右下，左下，左上
            path.moveTo(leftTopRadius, 0);
            path.lineTo(width - rightTopRadius, 0);
            path.quadTo(width, 0, width, rightTopRadius);

            path.lineTo(width, height - rightBottomRadius);
            path.quadTo(width, height, width - rightBottomRadius, height);

            path.lineTo(leftBottomRadius, height);
            path.quadTo(0, height, 0, height - leftBottomRadius);

            path.lineTo(0, leftTopRadius);
            path.quadTo(0, 0, leftTopRadius, 0);

            canvas.clipPath(path);
        }
        super.onDraw(canvas);
    }

}
