package com.remax.visualnovel.widget.glidetransformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;

import java.security.MessageDigest;

import jp.wasabeef.glide.transformations.BitmapTransformation;

public class DiffCornersTransformation extends BitmapTransformation {

    private float topLeft, topRight, bottomRight, bottomLeft;

    public DiffCornersTransformation(float topLeft, float topRight,
                                     float bottomRight, float bottomLeft) {
        this.topLeft = topLeft;
        this.topRight = topRight;
        this.bottomRight = bottomRight;
        this.bottomLeft = bottomLeft;
    }


    @Override
    protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {

        Bitmap result = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // 创建圆角路径
        Path path = new Path();
        float[] radii = new float[] {
                topLeft, topLeft, topRight, topRight,
                bottomRight, bottomRight, bottomLeft, bottomLeft
        };

        RectF rect = new RectF(0, 0, outWidth, outHeight);
        path.addRoundRect(rect, radii, Path.Direction.CW);

        canvas.clipPath(path);
        canvas.drawBitmap(toTransform, null, rect, paint);

        return result;
    }

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

    private String getCacheKey() {
        return "DifferentCornersTransformation-" + topLeft + "-" + topRight + "-" + bottomRight + "-" + bottomLeft;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DiffCornersTransformation that = (DiffCornersTransformation) o;

        if (Float.compare(that.topLeft, topLeft) != 0) return false;
        if (Float.compare(that.topRight, topRight) != 0) return false;
        if (Float.compare(that.bottomRight, bottomRight) != 0) return false;
        return Float.compare(that.bottomLeft, bottomLeft) == 0;
    }

    @Override
    public int hashCode() {
        int result = (topLeft != +0.0f ? Float.floatToIntBits(topLeft) : 0);
        result = 31 * result + (topRight != +0.0f ? Float.floatToIntBits(topRight) : 0);
        result = 31 * result + (bottomRight != +0.0f ? Float.floatToIntBits(bottomRight) : 0);
        result = 31 * result + (bottomLeft != +0.0f ? Float.floatToIntBits(bottomLeft) : 0);
        result = 31 * result + getCacheKey().hashCode();
        return result;
    }
}