package com.bonait.bnframework.common.image;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

import com.bin.david.form.data.form.IForm;
import com.bonait.bnframework.R;
import com.bonait.bnframework.common.image.utils.LocalCacheUtils;
import com.bonait.bnframework.common.image.utils.MemoryCacheUtils;
import com.bonait.bnframework.common.image.utils.NetCacheUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义的BitmapUtils,实现三级缓存
 */
public class MyBitmapUtils {
    private NetCacheUtils mNetCacheUtils;
    private MemoryCacheUtils mMemoryCacheUtils;

    public MyBitmapUtils(){
        mMemoryCacheUtils=new MemoryCacheUtils();
        mNetCacheUtils=new NetCacheUtils(mMemoryCacheUtils);
    }

    public void disPlay(ImageView ivPic, String url) {
        ivPic.setImageResource(R.mipmap.loading2);
        //内存缓存
        String name="";
        //https://hbl-1305371387.cos.ap-chengdu.myqcloud.com/Franchisee/jccy@163.com/goods/133381212328681942.jpg
        if(url.contains("http"))
        {
            name=url.substring(url.lastIndexOf('/')+1);
        }else
        {
            name=url;
        }

        //本地缓存
        if(url!=null && !url.isEmpty())
        {
            if(LocalCacheUtils.Get().SetBitmapFile(ivPic,name,url)){
                return;
            }

            //网络缓存
            if(url.contains("http"))
            {
                mNetCacheUtils.getBitmapFromNet(ivPic,url);
            }
        }
    }
    /**
     * 判断字符串是否为URL
     *
     * @param urls 需要判断的String类型url
     * @return true:是URL；false:不是URL
     */
    public static boolean isHttpUrl(String urls) {
        boolean isurl = false;
        //设置正则表达式
        String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
                + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";
        //对比
        Pattern pat = Pattern.compile(regex.trim());
        Matcher mat = pat.matcher(urls.trim());
        //判断是否匹配
        isurl = mat.matches();
        if (isurl) {
            isurl = true;
        }
        return isurl;
    }
}
