package com.gh.gamecenter.common.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.widget.TextView;

import androidx.core.content.res.ResourcesCompat;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.core.HaloApp;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

public class PicassoImageGetter implements Html.ImageGetter {
    private TextView textView = null;

    public PicassoImageGetter() {
    }

    public PicassoImageGetter(TextView target) {
        textView = target;
    }

    @Override
    public Drawable getDrawable(String source) {
        BitmapDrawablePlaceHolder drawable = new BitmapDrawablePlaceHolder();

        Context context = HaloApp.getInstance();
        ImageUtils.getPicasso()
                .load(source)
                .transform(new CircleTransformation())
                .placeholder(ResourcesCompat.getDrawable(context.getResources(), R.drawable.personal_user_default_icon, context
                        .getTheme()))
                .into(drawable);

        return drawable;
    }

    private class BitmapDrawablePlaceHolder extends BitmapDrawable implements Target {
        protected Drawable drawable;

        @Override
        public void draw(final Canvas canvas) {
            if (drawable != null) {
                Paint.FontMetricsInt fmPaint = textView.getPaint().getFontMetricsInt();
                float fontHeight = fmPaint.descent - fmPaint.ascent;
                float textSize = textView.getTextSize();

                if (fontHeight - textSize > 0) {
                    canvas.translate((fontHeight - textSize) / 2, 0);
                }
                drawable.draw(canvas);
            }
        }

        @SuppressWarnings("SuspiciousNameCombination")
        public void setDrawable(Drawable drawable) {
            try {
                this.drawable = drawable;

                Paint.FontMetricsInt fmPaint = textView.getPaint().getFontMetricsInt();
                int fontHeight = fmPaint.descent - fmPaint.ascent;
                int textSize = (int) textView.getTextSize();

                drawable.setBounds(0, 0, textSize, textSize);
                setBounds(0, 0, fontHeight, fontHeight);
                if (textView != null) {
                    textView.setText(textView.getText());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            setDrawable(new BitmapDrawable(HaloApp.getInstance()
                    .getResources(), bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            setDrawable(placeHolderDrawable);
        }
    }

    private class CircleTransformation implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());

            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
            if (squaredBitmap != source) {
                source.recycle();
            }

            Bitmap.Config config = source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
            Bitmap bitmap = Bitmap.createBitmap(size, size, config);

            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            BitmapShader shader = new BitmapShader(squaredBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
            paint.setAntiAlias(true);

            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);

            squaredBitmap.recycle();
            return bitmap;
        }

        @Override
        public String key() {
            return "circle";
        }
    }
}