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

import android.content.Context;

import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.service.IPlanService;
import com.tofly.ghrq_route.module.plan.service.PlanServiceImpl;
import com.tofly.ghrq_route.module.plan.service.PlanServiceImplOffline;

import java.util.List;

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

public class PlanController {

    /**
     * 是否在綫
     */
    boolean mbOnline;
    /**
     * 上下文
     */
    Context mContext;
    /**
     * Trouble接口实现对象
     */
    IPlanService planService;

    public PlanController(Context context){
        mbOnline = true;
        mContext = context;
        planService = new PlanServiceImpl(context);
    }

    public PlanController(boolean online, Context context){
        mbOnline = online;
        mContext = context;
        planService = new PlanServiceImpl(context);
    }

    private void setSignService(){
        boolean isOffline = OfflineWorkTool.isOffline();
        if (isOffline){
            planService = new PlanServiceImplOffline(mContext);
        }else {
            planService = new PlanServiceImpl(mContext);
        }
    }

    private void setService(boolean online){
        if (online){
            planService = new PlanServiceImpl(mContext);
        }else {
            planService = new PlanServiceImplOffline(mContext);
        }
    }

    private void setOnlineService(){
        planService = new PlanServiceImpl(mContext);
    }

    /**
     * 获取巡检计划列表
     * @param token
     * @param state 巡检状态
     * @param pageindex 当前页index
     * @param pagesize  每页数据大小
     * @param planCallBack
     */

    public void getPlanList(String token, String userid, int state, int period, int planmode, int pageindex, int pagesize,
                            boolean online, final PlanCallBack planCallBack){
        setService(online);
        planService.getPlanList(token, userid, state, period, planmode, pageindex, pagesize, planCallBack);
    }

    /**
     * 获取巡检计划列表(移动端)
     * @param context
     * @param state 巡检状态
     * @param pageindex 当前页index
     * @param pagesize  每页数据大小
     * @param planCallBack
     */

    public void getPlanListMobile(Context context, String userid, int state, int period, int planmode, int pageindex, int pagesize,
                            boolean online, final PlanCallBack planCallBack){
        setService(online);
        planService.getPlanListMobile(context, userid, state, period, planmode, pageindex, pagesize, planCallBack);
    }

    /**
     *  获取当日的巡检计划个数
     */
    public void getTodayPlanCount(Context context, String userid, int plan_mode, List<String> localPlanIds, final PlanCallBack planCallBack){
        setOnlineService();
        planService.getTodayPlanCount(context, userid, -1, localPlanIds, planCallBack);
    }

    /**
     *  巡检计划批量转移申请
     * @param childplanids 转移的计划id，用','隔开
     * @param note 申请转移的说明
     */
    public void transferPlanApply(Context context, List<String> childplanids, String note, final PlanCallBack planCallBack){
        setOnlineService();
        planService.transferPlanApply(context, childplanids, note, planCallBack);
    }

    /**
     * 根据巡检id获取巡检计划信息
     * @param token
     * @param plan_id 巡检计划id
     * @param planCallBack
     */
    public void getPlanInfo(String token, String plan_id, boolean online, final PlanCallBack planCallBack){
        setService(online);
        planService.getPlanInfo(token, plan_id, planCallBack);
    }

    /**
     * 获取巡检计划列表
     * @param planid planid
     * @param token
     * @param pageindex 当前页index
     * @param pagesize  每页数据大小
     * @param planCallBack
     */
    public void getPlanPointList(String token, String planid, int pageindex, int pagesize,
                            final PlanCallBack planCallBack){
        planService.getPlanPointList(token, planid, pageindex, pagesize, planCallBack);
    }

    /**
     * 获取巡检子计划列表
     * @param token
     * @param planid  主计划的id
     * @param pageindex 当前页index
     * @param pagesize  每页数据大小
     * @param planCallBack
     */
    public void getChildPlanList(String token, String planid, int pageindex, int pagesize,
                            final PlanCallBack planCallBack){
        planService.getChildPlanList(token, planid, pageindex, pagesize, planCallBack);
    }

    /**
     * 获取巡检计划列表
     * @param token
     * @param childid childid
     * @param pageindex 当前页index
     * @param pagesize  每页数据大小
     * @param planCallBack
     */
    public void getChildPlanPointList(String token, String childid,  int pageindex, int pagesize,
                                 final PlanCallBack planCallBack){
        setOnlineService();
        planService.getChildPlanPointList(token, childid, pageindex, pagesize, planCallBack);
    }

    /**
     * 根据子计划id获取子计划信息
     * @param token
     * @param childplanid childplanid
     */
    public void getChildPlanInfo(final String token, String childplanid, final PlanCallBack planCallBack){
        planService.getChildPlanInfo(token, childplanid, planCallBack);
    }

    /**
     *  获取当天巡检任务的所有巡检点
     *  @param token
     */
    public void getOfflinePathList(String token, String userid, String planIds, final PlanCallBack planCallBack){
        setOnlineService();
        planService.getOfflinePathList(token, userid, planIds, planCallBack);
    }

    /**
     *  获取当天巡检任务的所有巡检点
     *  @param token
     */
    public void getOfflinePathListByPage(String token, String userid, String planId, int pageindex, int pagesize, final PlanCallBack planCallBack){
        setOnlineService();
        planService.getOfflinePathListByPage(token, userid, planId, pageindex, pagesize, planCallBack);
    }


    /**
     *  更新当天巡检线所属点的到位情况
     */
    public void updatePathOver(String token, List<OfflinePathBean> pathBeanList, final PlanCallBack planCallBack){
        setOnlineService();
        planService.updatePathOver(token, pathBeanList, planCallBack);
    }

    /**
     *  更新当天巡检点到位情况
     */
    public void updatePointOver(String token, List<OfflinePathBean> pathBeanList, final PlanCallBack planCallBack){
        setOnlineService();
        planService.updatePointOver(token, pathBeanList, planCallBack);
    }

    /**
     *  更新当天巡检线的到位点第一次离开范围的时间
     */
    public void updatePathOffTime(String token, List<OfflinePathBean> pathBeanList, final PlanCallBack planCallBack){
        setOnlineService();
        planService.updatePathOffTime(token, pathBeanList, planCallBack);
    }

    /**
     *  向服务器申请重新计算巡检计划某天的完成率
     */
    public void applyRecalculatePlanPercent(String token, String childPlanId, String date, final PlanCallBack planCallBack){
        setOnlineService();
        planService.applyRecalculatePlanPercent(token, childPlanId, date, planCallBack);
    }

    /**
     *  获取重算完成后该计划需要更新的到位点
     */
    public void getRecalculatePlanPoints(String token, String childPlanId, final PlanCallBack planCallBack){
        setOnlineService();
        planService.getRecalculatePlanPoints(token, childPlanId, planCallBack);
    }

    /**
     *  通知服务器当前重算的计划的到位点已同步到本地 服务器设置state=3
     */
    public void updateRecalculatePlanState(String token, String childPlanId, final PlanCallBack planCallBack){
        setOnlineService();
        planService.updateRecalculatePlanState(token, childPlanId, planCallBack);
    }
}
