package com.uiui.zyappstore.bean;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.uiui.zyappstore.R;

import java.security.MessageDigest;

/**
 * 加载圆形头像带白色边框
 */
public class GlideCircleWithBorder extends BitmapTransformation {
    private static final String ID = "com.bumptech.glide.load.resource.bitmap.CenterCrop";
    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    private Paint mBorderPaint;
    private float mBorderWidth;
    private Context mContext;

    public GlideCircleWithBorder(Context context) {
        this.mContext = context;
    }

//    public GlideCircleWithBorder(Context context, int borderWidth, int borderColor) {
//        this.mContext = context;
//        mBorderWidth = Resources.getSystem().getDisplayMetrics().density * borderWidth;
//        mBorderPaint = new Paint();
//        mBorderPaint.setDither(true);
//        mBorderPaint.setAntiAlias(true);
//        mBorderPaint.setColor(borderColor);
//        mBorderPaint.setStyle(Paint.Style.STROKE);
//        mBorderPaint.setStrokeWidth(mBorderWidth);
//    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
//        int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderWidth / 2));
//        int x = (source.getWidth() - size) / 2;
//        int y = (source.getHeight() - size) / 2;
//        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
//        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
//        if (result == null) {
//            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
//        }
        //创建画笔 画布 手动描绘边框
//        Canvas canvas = new Canvas(result);
//        Paint paint = new Paint();
//        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
//        paint.setAntiAlias(true);
//        float r = size / 2f;
//        canvas.drawCircle(r, r, r, paint);
//        if (mBorderPaint != null) {
//            float borderRadius = r - mBorderWidth / 2;
//            canvas.drawCircle(r, r, borderRadius, mBorderPaint);
//        }
//        Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);

        Bitmap mask = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mask);
        //获取背景
        int width = mask.getWidth();
        int height = mask.getHeight();
        Bitmap result = pool.get(width, height, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        }

        Bitmap bitmapScale = Bitmap.createScaledBitmap(source, width, height, true);
        Canvas canvas = new Canvas();
        Paint paint = new Paint();
        canvas.setBitmap(result);
        canvas.drawBitmap(mask, 0, 0, paint);
        canvas.drawBitmap(bitmapScale, 0, 0, paint);
        Bitmap result2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas2 = new Canvas();
        Paint paint2 = new Paint();
        canvas2.setBitmap(result2);
        canvas2.drawBitmap(mask, 0, 0, paint2);
        paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas2.drawBitmap(result, 0, 0, paint2);

//        Bitmap bitmapshadow = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mask_shadow);
//        Bitmap bitmap = Bitmap.createScaledBitmap(bitmapshadow, width + 2, height + 2, true);
//        Canvas canvas3 = new Canvas();
//        Paint paint3 = new Paint();
//        canvas3.setBitmap(bitmap);
//        canvas3.drawBitmap(bitmapshadow, 0, 0, paint3);
//        canvas3.drawBitmap(result2, 1, 1, paint3);

        return result2;
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof GlideCircleWithBorder;
    }

    @Override
    public int hashCode() {
        return ID.hashCode();
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);
    }
}