package com.tofly.ghrq_route.module.survey;

import android.content.Context;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.module.job.NotifyMsgDaoHelper;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;
import com.tofly.ghrq_route.module.survey.controller.SurveyCallBack;
import com.tofly.ghrq_route.module.survey.controller.SurveyController;
import com.tofly.ghrq_route.module.survey.domain.SurveyBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.file.FileUtil;

import java.util.List;

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

public class SurveyTool {

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

    private static SurveyTool surveyTool = null;

    private SurveyController surveyController;

    private NotifyMsgDaoHelper notifyMsgDaoHelper;

    private SurveyTool(){
        surveyController = new SurveyController();
        notifyMsgDaoHelper = NotifyMsgDaoHelper.getInstance();
    }

    public static synchronized SurveyTool getInstance(){
        if (surveyTool == null){
            surveyTool = new SurveyTool();
        }
        return surveyTool;
    }


    /**
     * 测绘申请
     * */

    /*从服务器获取测绘申请列表*/
    public void getSurveyListOnline(Context context, int select_state, int work_state, String keyword, int pageindex, SurveyCallBack surveyCallBack){
        getSurveyList(context, select_state, work_state, keyword, pageindex, surveyCallBack);
    }

    private void getSurveyList(final Context context, int select_state, int work_state, String keyword, int pageindex, final SurveyCallBack surveyCallBack){
        final int pagesize = C.DEFAULT_LOAD_PAGESIZE;
        surveyController.getSurveyList(context, select_state, work_state, keyword, pageindex, pagesize, new SurveyCallBack(){
            @Override
            public void onGetDatas(boolean isSuccess, int errorCode, String errMsg, List emergencyList, int total) {
                super.onGetDatas(isSuccess, errorCode, errMsg, emergencyList, total);
                if (isSuccess){
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

                if (surveyCallBack != null)
                    surveyCallBack.onGetDatas(isSuccess, errorCode, errMsg, emergencyList, total);
            }
        });
    }


    /*public void loadEmergencyInfo(final Context context, String emergencyId, final EmergencyCallBack emergencyCallBack){
        if (emergencyId != null && !emergencyId.isEmpty()) {
            emergencyController.getEmergencyInfo(context, emergencyId, new EmergencyCallBack() {
                @Override
                public void onGetData(boolean isSuccess, int errorCode, String errMsg, Object data) {
                    super.onGetData(isSuccess, errorCode, errMsg, data);
                    if (isSuccess) {
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (emergencyCallBack != null)
                        emergencyCallBack.onGetData(isSuccess, errorCode, errMsg, data);
                }
            });
        }
    }*/

    /*上传测绘前记录申请*/
    public void uploadSurveyBefore(final Context context, final SurveyBean surveyBean, final List<TroublePictureBean> photos,
                                   final List<TroubleAudioBean> audios, final List<TroubleAudioBean> files, final SurveyCallBack surveyCallBack) {
        if (surveyBean != null) {
            surveyController.addSurveyBefore(context, surveyBean, photos, audios, files, new SurveyCallBack() {
                @Override
                public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                    super.onSetData(isSuccess, errorCode, errMsg);
                    if (isSuccess) {
                        deleteSurveyFiles(photos, audios);
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }

                    if (surveyCallBack != null){
                        surveyCallBack.onSetData(isSuccess, errorCode, errMsg);
                    }
                }
            });
        }
    }

    private void deleteSurveyFiles(List<TroublePictureBean> photos, List<TroubleAudioBean> audios) {
        if (photos != null){
            for (TroublePictureBean pictureBean : photos){
                String file_path = pictureBean.getFilepath();
                if (file_path != null && FileUtil.fileExists(file_path)){
                    FileUtil.deleteFile(file_path);
                }
            }
        }
        if (audios != null && audios.size() > 0) {
            for (TroubleAudioBean audioBean : audios) {
                String file = audioBean.getPath();
                FileUtil.deleteFile(file);
            }
        }
    }

    private void deleteSurveyFiles(List<TroublePictureBean> signs, List<TroublePictureBean> photos,
                                      List<TroubleAudioBean> audios) {
        if (signs != null){
            for (TroublePictureBean pictureBean : signs){
                String file_path = pictureBean.getFilepath();
                if (file_path != null && FileUtil.fileExists(file_path)){
                    FileUtil.deleteFile(file_path);
                }
            }
        }
        if (photos != null){
            for (TroublePictureBean pictureBean : photos){
                String file_path = pictureBean.getFilepath();
                if (file_path != null && FileUtil.fileExists(file_path)){
                    FileUtil.deleteFile(file_path);
                }
            }
        }
        if (audios != null && audios.size() > 0) {
            for (TroubleAudioBean audioBean : audios) {
                String file = audioBean.getPath();
                FileUtil.deleteFile(file);
            }
        }
    }

    public NotifyManageBean getNotifyMsgBean(String surveyId) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            return notifyMsgDaoHelper.getSurveyNotifyMsgBean(userBean.getUserid(), surveyId);
        }
        return null;
    }

    public void setMsgBeanRead(NotifyManageBean msgBean) {
        if (msgBean != null) {
            msgBean.setIsRead(true);
            notifyMsgDaoHelper.upadteNotifyMsgBean(msgBean);
        }
    }
}
