package com.tofly.ghrq_route.module.trouble.controller;

import android.content.Context;

import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.module.trouble.domain.AuditSchemeBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.module.trouble.service.ITroubleService;
import com.tofly.ghrq_route.module.trouble.service.TroubleServiceImpl;
import com.tofly.ghrq_route.module.trouble.service.TroubleServiceImplOffline;

import java.util.List;

/**
 * Created by Amu on 2017/3/8.
 */

public class TroubleController {
    /**
     * 是否在綫
     */
    boolean mbOnline;
    /**
     * 上下文
     */
    Context mContext;
    /**
     * Trouble接口实现对象
     */
    ITroubleService troubleService;

    public TroubleController(Context context){
        mbOnline = true;
        mContext = context;
    }

    public TroubleController(boolean online, Context context){
        mbOnline = online;
        mContext = context;
        troubleService = new TroubleServiceImpl(context);
    }

    private void setSignService(){
        boolean isOffline = OfflineWorkTool.isOffline();
        if (isOffline){
            troubleService = new TroubleServiceImplOffline(mContext);
        }else {
            troubleService = new TroubleServiceImpl(mContext);
        }
    }

    private void setSignService(boolean online){
        if (online){
            troubleService = new TroubleServiceImpl(mContext);
        }else {
            troubleService = new TroubleServiceImplOffline(mContext);
        }
    }

    private void setOnlineService(){
        troubleService = new TroubleServiceImpl(mContext);
    }

    /**
     * 获取隐患设备类型信息
     * @param userid 用户id
     * @param pageindex 当前页index
     * @param pagesize  每页数据大小
     * @param troubleCallBack
     */
    public void getTroubleList(Context context, String userid, int select_state, String keyword, int pageindex, int pagesize,
                               final TroubleCallBack troubleCallBack){
        setSignService();
        troubleService.getTroubleList(context, userid, select_state, keyword, pageindex, pagesize, troubleCallBack);
    }

    /**
     *  增加隐患
     * @param troubleBean  隐患数据
     * @param photos 隐患的图片
     * @param audios 隐患的录音
     * @param troubleCallBack
     */
    public void addTroubleNew(Context context, TroubleBean troubleBean, List<TroublePictureBean> photos,
                              List<TroubleAudioBean> audios, List<TroubleAudioBean> files, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.addTroubleNew(context, troubleBean, photos, audios, files, troubleCallBack);
    }

    /**
     *  值班人员现场勘察
     * @param troubleBean  隐患数据
     * @param photos 隐患的图片
     * @param audios 隐患的录音
     * @param troubleCallBack
     */
    public void ondutyUserCheckTrouble(Context context, TroubleBean troubleBean, List<TroublePictureBean> photos,
                              List<TroubleAudioBean> audios, List<TroubleAudioBean> files, final TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.ondutyUserCheckTrouble(context, troubleBean, photos, audios, files, troubleCallBack);
    }

    /**
     *  承办班班长审核派工
     * @param troubleBean  隐患数据
     * @param troubleCallBack
     */
    public void undertakerVerifyTrouble(Context context, TroubleBean troubleBean, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.undertakerVerifyTrouble(context, troubleBean, troubleCallBack);
    }

    /**
     *  巡检人员现场核实
     * @param troubleBean  隐患数据
     * @param photos 隐患的图片
     * @param audios 隐患的录音
     * @param troubleCallBack
     */
    public void undertakerHandleTrouble(Context context, TroubleBean troubleBean, List<TroublePictureBean> photos,
                                       List<TroubleAudioBean> audios, List<TroubleAudioBean> files, final TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.undertakerHandleTrouble(context, troubleBean, photos, audios, files, troubleCallBack);
    }

    /**
     *  副部长审核派工
     * @param troubleBean  隐患数据
     * @param troubleCallBack
     */
    public void underSectaryVerifyAssignTrouble(Context context, TroubleBean troubleBean, AuditSchemeBean schemeBean, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.underSectaryVerifyAssignTrouble(context, troubleBean, schemeBean, troubleCallBack);
    }

    /**
     *  责任班班长编制方案
     * @param troubleId  隐患数据
     * @param troubleCallBack
     */
    public void ondutyMakeScheme(Context context, String troubleId, int flag, AuditSchemeBean schemeBean, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.ondutyMakeScheme(context, troubleId, flag, schemeBean, troubleCallBack);
    }

    /**
     *  部长审核方案编制
     * @param troubleBean  隐患数据
     * @param troubleCallBack
     */
    public void sectaryVerifyScheme(Context context, TroubleBean troubleBean, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.sectaryVerifyScheme(context, troubleBean, troubleCallBack);
    }

    /**
     *  负责人执行方案和回报
     * @param troubleBean  隐患数据
     * @param troubleCallBack
     */
    public void ondutyHandleScheme(Context context, TroubleBean troubleBean, String notes, List<TroublePictureBean> photos,
                                    List<TroubleAudioBean> audios, List<TroubleAudioBean> files,  TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.ondutyHandleScheme(context, troubleBean, notes, photos, audios, files, troubleCallBack);
    }

    /**
     *  副部长审核处理回报
     * @param troubleBean  隐患数据
     * @param troubleCallBack
     */
    public void undersectaryVerifyHandle(Context context, TroubleBean troubleBean, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.undersectaryVerifyHandle(context, troubleBean, troubleCallBack);
    }

    /**
     *  修改设备管线基本信息
     */
    public void updatePipeDevice(Context context, TroubleBean troubleBean, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.updatePipeDevice(context, troubleBean, troubleCallBack);
    }

    /**
     * 获取隐患录音
     * @param token
     * @param url
     * @param troubleCallBack
     */
    public void getTroubleAudio(String token, String url,
                               final TroubleCallBack troubleCallBack){
        setSignService(true);
        troubleService.getTroubleAudio(token, url, troubleCallBack);
    }

    /**
     * 获取隐患录音
     * @param url
     * @param troubleCallBack
     */
    public void getTroubleFile(Context context, String url, String fileName, String dirPath, final TroubleCallBack troubleCallBack){
        setSignService(true);
        troubleService.getTroubleFile(context, url, fileName, dirPath, troubleCallBack);
    }

    /**
     * 获取隐患设备类型信息
     * @param context
     * @param troubleCallBack
     */
    public void getTroubleInfo(Context context, String troubleid, boolean online, TroubleCallBack troubleCallBack){
        setSignService(online);
        troubleService.getTroubleInfo(context, troubleid, troubleCallBack);
    }

    /**
     * 获取隐患事件级别信息
     * @param context
     * @param troubleCallBack
     */
    public void getTroubleEventType(Context context, TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.getTroubleEventType(context, troubleCallBack);
    }

    /**
     *  添加隐患录入信息
     */
    public void addTroubleRecord(String token, String tableid, List<TroublePictureBean> photos, List<TroubleAudioBean> audios, String notes, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.addTroubleRecord(token, tableid, photos, audios, notes, troubleCallBack);
    }

    /**
     *  获取隐患录入历史
     */
    public void getTroubleRecordList(String token, String tableid, String searchDate, int pageindex, int pagesize, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.getTroubleRecordList(token, tableid, searchDate, pageindex, pagesize, troubleCallBack);
    }

    /**
     *  获取最近的隐患录入历史
     */
    public void getLastTroubleRecord(String token, String tableid, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.getTroubleRecordList(token, tableid, null, 1, 1, troubleCallBack);
    }

    /**
     *  添加隐患清除录入信息
     */
    public void addTroubleClearRecord(String token, String troubleid, String plan_point_id, List<TroublePictureBean> photos, List<TroubleAudioBean> audios, String notes, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.addTroubleClearRecord(token, troubleid, plan_point_id, photos, audios, notes, troubleCallBack);
    }

    /**
     *  添加隐患录入信息
     */
    public void removeTroubleRecord(String token, String tableid, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.removeTroubleRecord(token, tableid, troubleCallBack);
    }

    /**
     *  获取隐患清除数据
     */
    public void getTroubleClearRecordList(String token, String troubleId, String childplanid, int pageindex, int pagesize, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.getTroubleClearRecordList(token, troubleId, childplanid, pageindex, pagesize, troubleCallBack);
    }

    /**
     *  获取最近的隐患清除录入历史
     */
    public void getLastTroubleClearRecord(String token, String troubleId, String childPlanId, final TroubleCallBack troubleCallBack){
        setOnlineService();
        troubleService.getTroubleClearRecordList(token, troubleId, childPlanId, 1, 1, troubleCallBack);
    }

    /**
     * 获取服务器限制安装文件(.aa文件)最新信息
     */
    public void getRestrictFileInfo(Context context, final TroubleCallBack troubleCallBack) {
        setOnlineService();
        troubleService.getRestrictFileInfo(context, troubleCallBack);
    }
}
