package com.kfzs.duanduan.react.upfile;


import com.nanchen.compresshelper.CompressHelper;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by howie on 2017/5/11.
 */

public class UpFileUtils {
    //    private static String URL_RN_UP_IMAGE = "http://shopapi.bandaimi.com/v1/upload_pic/avatar";
    private static String URL_RN_UP_IMAGE = "http://static.kuaifazs.com/sdkkitfile.php";

    //上传图片
    public static void upImage(File file, final UpFileListener upFileListener) {
        OkHttpUtils.post()//
                .addFile("uploadfile", "1.png", file)//
                .url(URL_RN_UP_IMAGE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        upFileListener.Progress(String.valueOf(Math.round(progress * 100)), -1);
                    }

                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        return response.body().string();
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        upFileListener.Failure(e.toString());
                    }

                    @Override
                    public void onResponse(Object response, int id) {
                        upFileListener.Success(response.toString(), -1);
                    }
                });

    }

    public static void upImages(ArrayList<String> photos, final UpFileListener upFileListener) {
        upImages(photos, upFileListener, 0);
    }

    public static void upImages(final ArrayList<String> photos, final UpFileListener upFileListener, final int index) {
        if (index < photos.size()) {
            File file;
            if ((file = new File(photos.get(index))).exists()) {
                File newFile = CompressHelper.getDefault(SheepApp.getInstance()).compressToFile(file);
//                org.xutils.common.util.FileUtil.copy(newFile.getAbsolutePath(), ClassFileHelper.DIR+"/1.png");
                OkHttpUtils.post()
                        .addFile("uploadfile", "1.png", newFile)//
                        .url(URL_RN_UP_IMAGE)
                        .build()//
                        .execute(new Callback() {
                            @Override
                            public void inProgress(float progress, long total, int id) {
                                super.inProgress(progress, total, id);
                                upFileListener.Progress(String.valueOf(Math.round(progress * 100)), index);
                            }

                            @Override
                            public Object parseNetworkResponse(Response response, int id) throws Exception {
                                return response.body().string();
                            }

                            @Override
                            public void onError(Call call, Exception e, int id) {
                                upFileListener.Failure(e.toString());
                            }

                            @Override
                            public void onResponse(Object response, int id) {
                                upFileListener.Success(response.toString(), index);
                                upImages(photos, upFileListener, index + 1);
                            }
                        });
            }else {//文件不存在就不用上传
                upImages(photos, upFileListener, index + 1);
            }
        } else
            upFileListener.Success("全部上传完成", -1);

    }
}
