package com.sheep.gamegroup.util;

import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.widget.TextView;

import com.sheep.gamegroup.util.viewHelper.CacheImageUtil;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.io.File;
import java.util.Locale;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/7.
 * realicing@sina.com
 * 图片显示类
 */
public class DetailImageGetter implements Html.ImageGetter {

    private int textSize;
    private int maxWidth;
    private TextView textView;
    private String content;

    public DetailImageGetter(TextView textView, String content, int maxWidth) {
        this.textView = textView;
        this.content = content;
        this.maxWidth = maxWidth;
        textSize = G.getRealPix(60);
    }

    //非表情图片尝试充满TextView显示
    public static final boolean IMG_FULL = true;

    @Override
    public Drawable getDrawable(final String source) {
        Resources resources = SheepApp.getInstance().getResources();
        if (source.startsWith("http")) {
            String path = CacheImageUtil.getCacheImg(source);
            if (path != null) {
                Drawable drawable = Drawable.createFromPath(path);
                if (drawable != null) {
                    int minimumWidth = drawable.getMinimumWidth();
                    int minimumHeight = drawable.getMinimumHeight();
                    int imgWidth, imgHeight, imgLeft = 0;
                    if (IMG_FULL) {
                        imgWidth = maxWidth;
                        imgHeight = minimumHeight * maxWidth / minimumWidth;
                    } else {
                        imgWidth = G.getRealPix(minimumWidth);
                        imgHeight = G.getRealPix(minimumHeight);
                        if (imgWidth > maxWidth && maxWidth > 0) {
                            imgHeight = (int) (maxWidth * imgHeight * 1.0f / imgWidth);
                            imgWidth = maxWidth;
                        } else {
                            imgLeft = (maxWidth - imgWidth) / 2;
                            imgWidth += imgLeft;
                        }
                    }
                    drawable.setBounds(imgLeft, 0, imgWidth, imgHeight);
                    return drawable;
                }
            }
        } else {
            int id = resources.getIdentifier(source, "mipmap", BuildConfig.APPLICATION_ID);
            if (id > 0) {
                Drawable drawable = resources.getDrawable(id);
                drawable.setBounds(0, 0, textSize, textSize);
                return drawable;
            }
        }
        Drawable drawable = resources.getDrawable(R.drawable.loading_01);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        CacheImageUtil.cacheImg(source, new Action1<File>() {
            @Override
            public void call(File file) {
                if (file != null)
                    ViewUtil.setH5ImgText(textView, content, maxWidth);
            }
        });
        return drawable;
    }
}