package com.fxy.common;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;

import com.fxy.baselibrary.bean.BaseEventBusBean;
import com.fxy.constant.EventCode;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Objects;

public class UploadDialog {
    private OnEventCallback callback;
    private Activity mActivity;
    private EventBus eventBus;
    private ProgressDialog progressDialog;
    private AsyncPictureUpload asyncPictureUpload ;
    private boolean currentUploadStatus = false;
    private int awaitNum = 0;

    public UploadDialog(Activity activity, OnEventCallback onEventCallback){
        callback= onEventCallback;
        mActivity = activity;
        initData();
    }
    public UploadDialog(Activity activity){
        mActivity = activity;
        initData();
    }

    private void initData(){
        asyncPictureUpload = new AsyncPictureUpload(Objects.requireNonNull(mActivity));
        asyncPictureUpload.setIsAllRe();
        progressDialog = new ProgressDialog(mActivity);
        progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // 当对话框关闭时执行的操作
                asyncPictureUpload.setStopUpload();
            }
        });
        // 注册当前Fragment作为事件监听器
        EventBus.getDefault().register(this);
    }

    /**
     * eventbus在主线程接收方法
     *
     * @param event BaseEventBusBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BaseEventBusBean event) {
        assert event != null;
        switch (event.getEventCode()){

            case EventCode.file_upload_lister:
                awaitNum = asyncPictureUpload.getAllAwaitNum();
                if (callback != null) {
                    callback.onUploadStatus("succeed");
                }

                if (awaitNum>0){
                    progressDialog.setTitle("待上传文件"+awaitNum+"个");
                }else{
                    asyncPictureUpload.setStopUpload();
                    currentUploadStatus = false;
                    progressDialog.dismiss();
                }
                break;
            case EventCode.file_upload_progress_lister:
                HashMap hashMap = (HashMap) event.getData();
                int process = Integer.parseInt(hashMap.get("process").toString());
                progressDialog.setProgress(process);
                break;
        }
    }

    public void pictureStartUpload(){
        awaitNum = asyncPictureUpload.getAllAwaitNum();
        progressDialog.setTitle("待上传文件"+awaitNum+"个");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setProgress(0);
        progressDialog.show();
        if (!currentUploadStatus){
            asyncPictureUpload.setStartUpload();
            currentUploadStatus = false;
        }
        if (asyncPictureUpload.getAwaitUploadListNum()==0){
            progressDialog.dismiss();
        }
    }

    public int getAwaitNum(){
        return asyncPictureUpload.getAllAwaitNum();
    }

    public int getFailNum(){
        return  asyncPictureUpload.getFailNum();
    }


    public void unregister() {
        eventBus.unregister(this);
        if (asyncPictureUpload!=null){
            asyncPictureUpload.setStopUpload();
            asyncPictureUpload.removeHandler();
        }
     }

    public void setCallback(OnEventCallback callback) {
        this.callback = callback;
    }
    public interface OnEventCallback {
        void onUploadStatus(String message);
    }
    public void setDebug(boolean debug) {
        asyncPictureUpload.setDebug(true);
    }

}


