package com.tofly.ghrq_route.module.emergencyHandle;

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.emergencyHandle.controller.EmergencyCallBack;
import com.tofly.ghrq_route.module.emergencyHandle.controller.EmergencyController;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyOverData;
import com.tofly.ghrq_route.module.job.NotifyMsgDaoHelper;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;
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.date.DateUtils;
import com.tofly.ghrq_route.common.util.file.FileUtil;

import java.util.List;

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

public class EmergencyTool {

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

    private static EmergencyTool emergencyTool = null;

    private EmergencyController emergencyController;

    private EmergencyDaoHelper emergencyDaoHelper;

    private NotifyMsgDaoHelper notifyMsgDaoHelper;

    private EmergencyTool(){
        emergencyController = new EmergencyController();
        emergencyDaoHelper = EmergencyDaoHelper.getInstance();
        notifyMsgDaoHelper = NotifyMsgDaoHelper.getInstance();
    }

    public static synchronized EmergencyTool getInstance(){
        if (emergencyTool == null){
            emergencyTool = new EmergencyTool();
        }
        return emergencyTool;
    }


    /**
     * 应急调度列表
     * */

    /*从服务器获取应急调度列表*/
    public void getEmergencyListOnline(Context context, int select_state, int work_state, String keyword, int pageindex, EmergencyCallBack emergencyCallBack){
        getEmergencyList(context, select_state, work_state, keyword, pageindex, emergencyCallBack);
    }

    /**
     * 获取应急调度列表
     */
    private void getEmergencyList(final Context context, int select_state, int work_state, String keyword, int pageindex, final EmergencyCallBack emergencyCallBack){
        final int pagesize = C.DEFAULT_LOAD_PAGESIZE;
        emergencyController.getEmergencyList(context, select_state, work_state, keyword, pageindex, pagesize, new EmergencyCallBack(){
            @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 (emergencyCallBack != null)
                    emergencyCallBack.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 uploadNewEmergency(final Context context, final EmergencyHandleData handleData, final List<TroublePictureBean> photos,
                                  final List<TroubleAudioBean> audios, final List<TroubleAudioBean> files, final EmergencyCallBack emergencyCallBack) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && handleData != null) {
            emergencyController.addEmergencyNew(context, handleData, photos, audios, files, new EmergencyCallBack() {
                @Override
                public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                    super.onSetData(isSuccess, errorCode, errMsg);
                    if (isSuccess) {
                        deleteEmergencyFiles(photos, audios);
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }

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

    /**
     *  应急调度派工后到位签到
     */
    public void emergencySignOver(final Context context, String emergencyId, double lng, double lat, final EmergencyCallBack emergencyCallBack) {
        emergencyController.emergencySignOver(context, emergencyId, lng, lat, new EmergencyCallBack() {
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                if (isSuccess) {

                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

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

    /**
     *  应急调度上报后派工处理
     */
    public void emergencyAssignHandle(final Context context, EmergencyHandleData handleData, List<TroublePictureBean> signs, List<TroublePictureBean> photos,
                                       List<TroubleAudioBean> audios, List<TroubleAudioBean> files, final EmergencyCallBack emergencyCallBack) {
        emergencyController.emergencyAssignHandle(context, handleData, signs, photos, audios, files, new EmergencyCallBack() {
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    deleteEmergencyFiles(signs, photos, audios);
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

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

    /**
     *  调度中心抢修派工
     */
    public void emergencyScheduleAssign(final Context context, EmergencyHandleData handleData, final EmergencyCallBack emergencyCallBack) {
        emergencyController.emergencyScheduleAssign(context, handleData, new EmergencyCallBack() {
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                if (isSuccess) {

                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

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

    /**
     *  责任人抢修派工
     */
    public void emergencyRepairAssign(final Context context, EmergencyHandleData handleData, List<TroublePictureBean> photos,
                                      List<TroubleAudioBean> audios, List<TroubleAudioBean> files, final EmergencyCallBack emergencyCallBack) {
        emergencyController.emergencyRepairAssign(context, handleData, photos, audios, files, new EmergencyCallBack() {
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    deleteEmergencyFiles(null, photos, audios);
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

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

    /**
     *  应急调度抢修处理
     */
    public void emergencyRepairHandle(final Context context, EmergencyHandleData handleData, List<TroublePictureBean> photos,
                                      List<TroubleAudioBean> audios, List<TroubleAudioBean> files, final EmergencyCallBack emergencyCallBack) {
        emergencyController.emergencyRepairHandle(context, handleData, photos, audios, files, new EmergencyCallBack() {
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    deleteEmergencyFiles(null, photos, audios);
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

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

    private void deleteEmergencyFiles(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 deleteEmergencyFiles(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 boolean isEmergencyOver(EmergencyHandleData emergencyData) {
        return (emergencyData != null &&
                emergencyData.getSignTime() != null && !emergencyData.getSignTime().isEmpty());
    }

    public boolean isEmergencyOver(String emergencyId) {
        EmergencyOverData overData = getLocalEmergencyOverData(emergencyId);
        return (overData != null && overData.getIsOver() == 1);
    }

    public void setEmergencyOver(String emergencyId) {
        EmergencyOverData overData = new EmergencyOverData();
        overData.setId(emergencyId);
        overData.setIsOver(1);
        overData.setOverTime(DateUtils.getDate());
        saveEmergencyOverData(overData);
    }

    public EmergencyOverData getLocalEmergencyOverData(String emergencyId){
        return emergencyDaoHelper.getEmergencyOverData(emergencyId);
    }

    private void saveEmergencyOverData(EmergencyOverData overData){
        if (overData != null) {
            emergencyDaoHelper.insertOrUpdateEmergencyOverData(overData);
        }
    }

    public void deleteSavedEmergencyOverData(EmergencyOverData overData) {
        emergencyDaoHelper.deleteSavedEmergencyOverData(overData);
    }

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

    public List<NotifyManageBean> getUnNotifyMsgBean() {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            return notifyMsgDaoHelper.getEmergencyUnNotifyMsgBean(userBean.getUserid());
        }
        return null;
    }

    public void setMsgBeanNotified(List<NotifyManageBean> msgBeans) {
        if (msgBeans != null) {
            for (NotifyManageBean msgBean : msgBeans) {
                msgBean.setIsNotify(true);
            }
            notifyMsgDaoHelper.upadteNotifyMsgBeans(msgBeans);
        }
    }

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


}
