package com.mayi.fastdevelop.view;

import android.text.TextUtils;
import android.widget.ImageView;

import com.mayi.fastdevelop.R;
import com.squareup.picasso.Picasso;

import java.io.File;

/**
 * 加载图片
 */
public class LoadingPictures {
    public static int error = R.mipmap.loading_pictures_error_icon;
    public static int placeholder = R.mipmap.loading_pictures_placeholder_icon;

    //加载圆角图片
    public static void loadUriCircleCornerForm(String url, ImageView img) {
        if (TextUtils.isEmpty(url)) {
            Picasso.with(img.getContext()).load(error).transform(new CircleCornerForm()).into(img);
        } else {
            Picasso.with(img.getContext())
                    .load(url)
                    .resize(2000, 2000)
                    .onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
                    .transform(new CircleCornerForm())
                    .placeholder(placeholder)
                    .error(error)
                    .into(img);
        }
    }

    //加载圆形图片
    public static void loadUriCircleTransform(String url, ImageView img) {
        if (TextUtils.isEmpty(url)) {
            Picasso.with(img.getContext()).load(error).transform(new CircleTransform()).into(img);
        } else {
            Picasso.with(img.getContext())
                    .load(url)
                    .resize(2000, 2000)
                    .onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
                    .transform(new CircleTransform())
                    .placeholder(placeholder)
                    .error(error)
                    .into(img);
        }
    }

    //加载网络图片
    public static void loadUri(String url, ImageView img) {
        if (TextUtils.isEmpty(url)) {
            Picasso.with(img.getContext()).load(error).into(img);
        } else {
            Picasso.with(img.getContext())
                    .load(url)
                    .resize(2000, 2000)
                    .onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
                    .placeholder(placeholder)
                    .error(error)
                    .into(img);
        }
    }

    //加载文件图片
    public static void loadFile(File file, ImageView img) {
        if (file == null) {
            Picasso.with(img.getContext()).load(error).into(img);
        } else {
            Picasso.with(img.getContext())
                    .load(file)
                    .resize(2000, 2000)
                    .placeholder(placeholder)
                    .error(error)
                    .into(img);
        }
    }

    //加载文件图片
    public static void loadFile(String pathname, ImageView img) {
        loadFile(new File(pathname), img);
    }

}
