package com.bc_chat.bc_base.task;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.bc_chat.bc_base.utils.Const;
import com.zhaohaoting.framework.abs.task.RxSimpleDataAsyncTask;
import com.zhaohaoting.framework.application.ZhtApplication;
import com.zhaohaoting.framework.utils.Utils;
import com.zhaohaoting.framework.utils.file.FileUtils;
import com.zhaohaoting.framework.utils.md5.MD5;

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

import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;

/**
 * author：Created by zhaohaoting on 2018/8/21
 * email：526309416@qq.com
 * desc：图片上传
 */
public class UpLoadPictureAsyncTask extends RxSimpleDataAsyncTask<List<String>> {
    private List<String> selImageList = new ArrayList<>();

    public UpLoadPictureAsyncTask(List<String> selImageList) {
        if (!Utils.listIsEmpty(selImageList)) {
            this.selImageList.addAll(selImageList);
        }
    }

    @Override
    protected Flowable<List<String>> loadDataAsyncTask() {
        final List<String> urls = new ArrayList<>();
        return Flowable.fromCallable(() -> {
            OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(Const.OSS.OSS_AK, Const.OSS.OSS_AKS);
            final OSS oss = new OSSClient(ZhtApplication.getContext(), Const.OSS.OSS_END_POINT, credentialProvider);
            for (String item : selImageList) {
                //图片压缩
                if (!item.startsWith("http")) {
                    File file;
                    File originalFile = new File(item);
                    if (originalFile.length() <= 1024 * 200) {
                        file = originalFile;
                    } else {
                        file = FileUtils.compressImageBitmap(item);
                    }
                    String path = item;
                    if (file.exists()) {
                        path = file.getAbsolutePath();
                    }
                    int index = path.lastIndexOf(".");
                    String filePath = path.substring(0, index);
                    String fileSuffix = path.substring(index);

                    String objectKey;
                    try {
                        objectKey = "image/" + MD5.getStringMD5(filePath) + fileSuffix;
                    } catch (Exception e) {
                        objectKey = "image/" + MD5.getStringMD5(path) + ".jpg";
                    }
                    PutObjectRequest put = new PutObjectRequest(Const.OSS.BUCKET_NAME, objectKey, path);
                    oss.putObject(put);
                    String url = oss.presignPublicObjectURL(Const.OSS.BUCKET_NAME, objectKey);
                    urls.add(url);
                } else {
                    urls.add(item);
                }
            }
            return urls;
        }).subscribeOn(Schedulers.io());
    }
}
