package com.tofly.ghrq_route.module.download;

import android.content.Context;

import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.module.download.controller.DownloadController;
import com.tofly.ghrq_route.module.download.domain.DownloadData;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.user.controller.UserController;
import com.tofly.ghrq_route.module.user.domain.UserBean;

import java.util.List;

/**
 * Created by Amu on 2017/4/26.
 */

public class DownloadTool {

    private static final String TAG = DownloadTool.class.getName();

    private static DownloadTool downloadTool = null;

    private DBDaoHelper dbDaoHelper;

    private DownloadController downloadController;

    private UserController userController;

    private int pageindex = 1;
    private final int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;

    private DownloadTool(){
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    public static synchronized DownloadTool getInstance(){
        if (downloadTool == null){
            downloadTool = new DownloadTool();
        }
        return downloadTool;
    }

    public void checkConfigUpdate(final Context context){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null){
            if (downloadController == null)
                downloadController = new DownloadController(context);
//            downloadController.getDownloadList(userBean.getToken(), (isSuccess, errorCode, errMsg, list) -> {
//                if (isSuccess){
//                    boolean isNeedUpdate = checkIsNeedUpdate(context, list);
//                    if (isNeedUpdate){
//                        EventBus.getDefault().post(new DownloadEvent(DOWNLOAD_PIPEDATA_CHANGED));
//                    }
//                }else {
//                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
//                    ToastUtil.showToast(context, errMsg);
//                }
//            });
        }
    }

    private boolean checkIsNeedUpdate(Context context, List<DownloadData> dataList){
        boolean isUpdate = false;
        if (dataList != null){
            for (DownloadData data : dataList){
                DownloadData savedData = dbDaoHelper.getDownloadData(data.getId());
                if (savedData == null){ /*本地不存在*/
                    isUpdate = true;
                    break;
                }else {
                //    isUpdate = isNeedUpdate(context, data, savedData);
                    if (isUpdate)
                        break;
                }
            }
        }
        return isUpdate;
    }

//    private boolean isNeedUpdate(Context context, DownloadData downloadData, DownloadData savedData){
//        //是否存在并设置下载
//        String filename = downloadData.getCval();
//        String filePath = C.dir.data + filename;
//        int intStatus = Integer.parseInt(downloadData.getDownload_state());
//        boolean canDownload = false;
//
//        File file = new File(filePath);
//        if (!file.exists()) {
//            /*服务器状态*/
//            if (Server_Status.STATUS_UPDATED.isEqual(intStatus)) {
//                canDownload = true;
//            } else {
//                canDownload = false;
//            }
//        } else {
//            if (savedData == null){
//                canDownload = true;
//            }else {
//                int down_status = savedData.getDownload_state();
//                if (Download_State.STATE_DOWNLOADING.isEqual(down_status)) {
//                    /*正在下载*/
//                    canDownload = false;
//                } else if (Download_State.STATE_DOWNLOAD_FAILED.isEqual(down_status)) {
//                    /*下载失败*/
//                    canDownload = true;
//                } else {
//                    String updatetime = downloadData.getUpdatetime();
//                    String downloadtime = savedData.getDownloadtime();
//                    boolean timeDifferent = FileUtil.getTimeDifferent(updatetime, downloadtime);
//                    if (!timeDifferent || intStatus == 0 || intStatus == 1 || intStatus == 3) {
//                        canDownload = false;
//                    } else if (intStatus == 2) {
//                        canDownload = true;
//                    }
//                }
//            }
//        }
//        return canDownload;
//    }


    public final static String DOWNLOAD_PIPEDATA_CHANGED = "downloadPipedataChanged";
    public static class DownloadEvent {
        public final String message;
        public DownloadEvent(String message) {
            this.message = message;
        }
    }
}
