package com.mytatvarnsdk.myView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.media.ExifInterface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class CircleImageView extends View {
    private Paint paint;
    private Bitmap bitmap;

    private float viewWidth, viewHeight;

    public CircleImageView(Context context) {
        super(context);
        initView();
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth = getMeasuredWidth();
        viewHeight = getMeasuredHeight();
    }

    public void setImageResoure(String path, Bitmap bitmaps, int measuredWidth, int measuredHeight) {
        if (bitmaps != null) {
            viewWidth = measuredWidth;
            viewHeight = measuredHeight;
            bitmap = handleBitmap(path,bitmaps);
            Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
        }
    }


    public int setImageOrientation(String imagePath) {
        int rotationAngle = 0;
        try {
            ExifInterface exif = new ExifInterface(imagePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotationAngle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotationAngle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotationAngle = 270;
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rotationAngle;
    }

    private Bitmap handleBitmap(String path,Bitmap bm) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        float density = getResources().getDisplayMetrics().density;
        float dW = density * 40;
        float scaleWidth;
        // 计算缩放比例
        if (viewWidth > 0) {
            scaleWidth = viewWidth / Math.min(height, width);
        } else {
            scaleWidth = dW / Math.min(height, width);
        }
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postRotate(setImageOrientation(path));
        matrix.postScale(scaleWidth, scaleWidth);
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitmap != null) {
            canvas.drawCircle(Math.min(viewWidth, viewHeight) / 2, Math.min(viewWidth, viewHeight) / 2, Math.min(viewWidth, viewHeight) / 2, paint);
        }
    }
}
