package com.swago.room.adapter.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.swago.baseswago.util.DpPxUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @date 2022/1/18 21:07
 * description:
 */
public class HtmlImageGetter implements Html.ImageGetter{

    List<String> tempImgKeys = new ArrayList<>();
    private final Context mContext;
    private final TextView mTextView;
    private int suitHeight = 15;
    private HashSet<Target> targets;
    private HashSet<GifDrawable> gifDrawables;

    public HtmlImageGetter(Context context, TextView textView, int suitHight, List<String> tempImgKey) {
        tempImgKeys = (tempImgKey);
        this.mContext = context;
        this.mTextView = textView;
        this.suitHeight = suitHight;
        targets = new HashSet<>();
        gifDrawables = new HashSet<>();
    }

    @Override
    public Drawable getDrawable(String source) {
        final UrlDrawable urlDrawable = new UrlDrawable();
        for (String s : tempImgKeys) {
            if (source.contains(s)) {
                return urlDrawable;
            }
        }
        RequestBuilder load;
        final Target target;
        if (isGif(source)) {
            load = Glide.with(mContext).asGif().load(source);
            target = new GifTarget(urlDrawable);
        } else {
            load = Glide.with(mContext).asBitmap().load(source);
            target = new BitmapTarget(urlDrawable, source);
        }
        targets.add(target);
        load.into(target);
        return urlDrawable;
    }


    private static boolean isGif(String path) {
        int index = path.lastIndexOf('.');
        return index > 0 && "gif".toUpperCase().equals(path.substring(index + 1).toUpperCase());
    }

    private class GifTarget extends SimpleTarget<GifDrawable> {
        private final UrlDrawable urlDrawable;


        private GifTarget(UrlDrawable urlDrawable) {
            this.urlDrawable = urlDrawable;

        }

        @Override
        public void onResourceReady(GifDrawable resource, Transition<? super GifDrawable> glideAnimation) {
            int w = DpPxUtil.INSTANCE.getScreenWidth();
            int hh = resource.getIntrinsicHeight();
            int ww = resource.getIntrinsicWidth();
            int high = hh * (w - 50) / ww;
            Rect rect = new Rect(20, 20, w - 30, high);
            resource.setBounds(rect);
            urlDrawable.setBounds(rect);
            urlDrawable.setDrawable(resource);
            gifDrawables.add(resource);
            resource.setCallback(mTextView);
            resource.start();
            resource.setLoopCount(GifDrawable.LOOP_FOREVER);
            mTextView.setText(mTextView.getText());
            mTextView.invalidate();
        }
    }

    private class BitmapTarget extends SimpleTarget<Bitmap> {
        private final UrlDrawable urlDrawable;
        private String mUrl;

        private BitmapTarget(UrlDrawable urlDrawable, String url) {
            this.urlDrawable = urlDrawable;
            this.mUrl = url;
        }

        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> glideAnimation) {
            Drawable drawable = new BitmapDrawable(resource);
            int width = resource.getWidth();
            int height = resource.getHeight();
            float type = width * 1f / height;
            setBounds(drawable, type);
            mTextView.setText(mTextView.getText());
            mTextView.invalidate();
        }

        private void setBounds(Drawable drawable, float type) {
            int heights = DpPxUtil.INSTANCE.dip2px(suitHeight);
            int widths = (int) (heights * type);
            drawable.setBounds(0, 0, widths, heights);
            urlDrawable.setBounds(0, 0, widths, heights);
            urlDrawable.setDrawable(drawable);
        }
    }
}
