package com.sheep.gamegroup.util;

import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.SheepApp;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by realicing on 2018/11/27.
 * realicing@sina.com
 */
public class QiNiuUploadUtil {

    public static final String BASE_URL = "http://cdn.video.17xmy.com/";
    private static QiNiuUploadUtil instance = new QiNiuUploadUtil();

    public static QiNiuUploadUtil getInstance() {
        return instance;
    }
//
//    public static final String TAG_VIDEO = "video";
//    public static final String TAG_IMAGE = "image";
//
//    @StringDef({TAG_VIDEO, TAG_IMAGE})
//    public @interface TAG {
//    }

    //上传视频或者图片,不传key
    public void uploadFile(final String path, final UpCompletionHandler upCompletionHandler, final UpProgressHandler upProgressHandler) {
        SheepApp.getInstance().getNetComponent().getApiService().getVideoToken()
                .subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        String token = baseMessage.getData(String.class);
                        uploadManager.put(path, null, token, upCompletionHandler, new UploadOptions(null, null, false, upProgressHandler, null));
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        upCompletionHandler.complete(null, null, null);
                    }
                });
    }
    public static final int UPLOAD_TYPE_NONE = 0;
    public static final int UPLOAD_TYPE_START = 1;
    public static final int UPLOAD_TYPE_SUCCESS = 2;
    public static final int UPLOAD_TYPE_FAIL = 3;

//    public static String getKey(String path, String md5, @TAG String tag) {
//        Uri uri = Uri.parse(path);
//        String lLastPathSegment = uri.getLastPathSegment();
//        String name = FileUtil.getFileName(lLastPathSegment, TextUtils.equals(TAG_VIDEO, tag) ? "mp4" : "png");
//        return String.format(Locale.CHINA, "%s_%s_%s_%s", tag, DataUtil.getInstance().getUserId(), md5, name);
//    }


    private UploadManager uploadManager = new UploadManager();

    public UploadManager getUploadManager() {
        return uploadManager;
    }
}
