package com.casic.dcms.utils.retrofit;

import com.casic.dcms.utils.AuthenticationHelper;
import com.casic.dcms.utils.LocaleConstant;
import com.google.gson.JsonObject;
import com.pengxh.androidx.lite.utils.RetrofitFactory;
import com.pengxh.androidx.lite.utils.SaveKeyValues;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Observable;

public class RetrofitServiceManager {
    private static final String TAG = "RetrofitServiceManager";

    private static final RetrofitService api = RetrofitFactory.createRetrofit(
            (String) SaveKeyValues.getValue(LocaleConstant.IP_KEY, "http://111.198.10.15:11409"),
            RetrofitService.class,
            true
    );

//    private static final RetrofitService shopApi = RetrofitFactory.createRetrofit(
//            "http://111.198.10.15:11409", RetrofitService.class
//    );

    /**
     * 验证PublicKey
     */
    public static Observable<ResponseBody> authenticate() {
        return api.getPublicKey();
    }

    /**
     * 登录并获取Token
     */
    public static Observable<ResponseBody> login(String username, String key) {
        return api.login(username, key);
    }

    /**
     * 获取用户信息
     */
    public static Observable<ResponseBody> getUserData() {
        return api.getUserData(AuthenticationHelper.getToken());
    }

    /**
     * 获取地图地址
     */
    public static Observable<ResponseBody> getMapConfig() {
        return api.getMapConfig(AuthenticationHelper.getToken());
    }

    /**
     * 大类列表查询
     */
    public static Observable<ResponseBody> getCaseLargeType(String eorc) {
        return api.getCaseLargeType(AuthenticationHelper.getToken(), eorc);
    }

    /**
     * 大类列表查询
     */
    public static Observable<ResponseBody> getCaseSmallType(String eorc, String typeId) {
        return api.getCaseSmallType(AuthenticationHelper.getToken(), eorc, typeId);
    }

    /**
     * 提交案卷
     */
    public static Observable<ResponseBody> submitCase(
            String isNeedVerify, String isNeedRevisit, String longitude, String latitude,
            String source, String caseLevel, String eorc, String casetypeCode,
            String casetypeDetailCode, String areaCode, String streetCode, String communityCode,
            String gridId, String description, String fieldintro, String fileIdVerify,
            String audioIdVerify, String videoIdVerify, int bzhours, int bzminis
    ) {
        return api.submitCase(
                AuthenticationHelper.getToken(), isNeedVerify, isNeedRevisit, longitude,
                latitude, source, caseLevel, eorc, casetypeCode, casetypeDetailCode, areaCode,
                streetCode, communityCode, gridId, description, fieldintro, fileIdVerify,
                audioIdVerify, videoIdVerify, bzhours, bzminis
        );
    }

    /**
     * 退出登录
     */
    public static Observable<ResponseBody> loginOut() {
        return api.loginOut(AuthenticationHelper.getToken());
    }

    /**
     * 修改密码
     */
    public static Observable<ResponseBody> getChangePasswordResult(String oldPwd, String newPwd) {
        return api.changePassword(AuthenticationHelper.getToken(), oldPwd, newPwd);
    }

    /**
     * 修改头像
     */
    public static Observable<ResponseBody> updateUserInfo(String id, String account, String name, String deptid, String roleid, String avatar, String sex, String birthday, String phone) {
        JsonObject param = new JsonObject();
        param.addProperty("id", id);
        param.addProperty("account", account);
        param.addProperty("name", name);
        param.addProperty("deptid", deptid);
        param.addProperty("roleid", roleid);
        param.addProperty("avatar", avatar);
        param.addProperty("sex", sex);
        param.addProperty("birthday", birthday);
        param.addProperty("phone", phone);
        RequestBody requestBody = RequestBody.create(param.toString(), MediaType.parse("application/json;charset=utf8"));
        return api.updateUserInfo(AuthenticationHelper.getToken(), requestBody);
    }

    /**
     * 获取部门树
     */
    public static Observable<ResponseBody> getDeptTree(String pid) {
        return api.getDeptTree(AuthenticationHelper.getToken(), pid);
    }

    /**
     * 获取通讯录
     */
    public static Observable<ResponseBody> getPhoneBook(String deptid) {
        return api.getPhoneBook(AuthenticationHelper.getToken(), deptid);
    }

    /**
     * 获取高发问题
     */
    public static Observable<ResponseBody> getHighEvent(String begTime, String endTime) {
        return api.getHighEvent(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 获取单位处置情况
     */
    public static Observable<ResponseBody> getDeptHandleCondition(String begTime, String endTime) {
        return api.getDeptHandleCondition(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 网格结案率Top10
     */
    public static Observable<ResponseBody> getGridAnalysis(String begTime, String endTime) {
        return api.getGridAnalysis(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 获取城市基础数据
     */
    public static Observable<ResponseBody> getCityBaseData() {
        return api.getCityBaseData(AuthenticationHelper.getToken());
    }

    /**
     * 获取案卷来源
     */
    public static Observable<ResponseBody> getCaseResource(String begTime, String endTime) {
        return api.getCaseResource(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 案卷趋势分析
     */
    public static Observable<ResponseBody> getCaseTendency(String begTime, String endTime) {
        return api.getCaseTendency(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 商铺处罚类型统计
     */
    public static Observable<ResponseBody> getPunishTypeStatistic(String begTime, String endTime) {
        return api.getPunishType(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 商铺处罚排行榜
     */
    public static Observable<ResponseBody> getPunishRanking(String begTime, String endTime) {
        return api.getPunishRanking(AuthenticationHelper.getToken(), begTime, endTime);
    }

    /**
     * 获取案件状态
     */
    public static Observable<ResponseBody> getCaseStatus() {
        return api.getCaseStatus(AuthenticationHelper.getToken());
    }

    /**
     * 根据时间查询案卷列表
     */
    public static Observable<ResponseBody> getCaseListResult(
            String eorc, String casetypeCode, String casetypeDetailCode, String caseState,
            String registerTimeBegin, String registerTimeEnd, String isShopCase, int offset
    ) {
        return api.getCaseList(
                AuthenticationHelper.getToken(), eorc, casetypeCode, casetypeDetailCode,
                caseState, registerTimeBegin, registerTimeEnd, isShopCase,
                LocaleConstant.PAGE_LIMIT, offset
        );
    }

    /**
     * 获取案件详情
     */
    public static Observable<ResponseBody> getCaseDetailResult(String id) {
        return api.getCaseDetail(AuthenticationHelper.getToken(), id);
    }

    /**
     * 获取案件流转记录
     */
    public static Observable<ResponseBody> getCaseProcessResult(String processId) {
        return api.getCaseProcess(AuthenticationHelper.getToken(), processId);
    }

    /**
     * 更新版本
     */
    public static Observable<ResponseBody> updateVersion() {
        return api.updateVersion(AuthenticationHelper.getToken());
    }

    /**
     * 获取核实待办案卷
     */
    public static Observable<ResponseBody> getVerifyResult(int limit, int offset) {
        return api.getVerifyResult(AuthenticationHelper.getToken(), limit, offset);
    }

    /**
     * 获取核实已办案卷
     */
    public static Observable<ResponseBody> getVerifiedResult(int limit, int offset) {
        return api.getVerifiedResult(AuthenticationHelper.getToken(), limit, offset);
    }

    /**
     * 获取核查待办案卷
     */
    public static Observable<ResponseBody> getCheckResult(int limit, int offset) {
        return api.getCheckResult(AuthenticationHelper.getToken(), limit, offset);
    }

    /**
     * 获取核查已办案卷
     */
    public static Observable<ResponseBody> getCheckedResult(int limit, int offset) {
        return api.getCheckedResult(AuthenticationHelper.getToken(), limit, offset);
    }

    /**
     * 上传案卷图片
     * <p>
     * 系统路径static拼接图片返回路径
     * http://xx.com/static/2019-10/8050891248624f2bbefedcb196ce89cb.jpeg
     */
    public static Observable<ResponseBody> uploadImage(File image) {
        RequestBody requestBody = RequestBody.create(image, MediaType.parse("image/png"));
        return api.uploadImage(
                AuthenticationHelper.getToken(), MultipartBody.Part.createFormData("file", image.getName(), requestBody)
        );
    }

    /**
     * 上传案卷音频
     */
    public static Observable<ResponseBody> uploadAudio(File audio) {
        RequestBody requestBody = RequestBody.create(audio, MediaType.parse("audio/m4a"));
        return api.uploadAudio(
                AuthenticationHelper.getToken(), MultipartBody.Part.createFormData("file", audio.getName(), requestBody)
        );
    }

    /**
     * 上传案卷视频
     */
    public static Observable<ResponseBody> uploadVideo(File video) {
        RequestBody requestBody = RequestBody.create(video, MediaType.parse("video/mp4"));
        return api.uploadVideo(
                AuthenticationHelper.getToken(), MultipartBody.Part.createFormData("file", video.getName(), requestBody)
        );
    }

    /**
     * 五包上报
     */
    public static Observable<ResponseBody> submitShopCase(
            String isNeedVerify, String isNeedRevisit, String longitude, String latitude,
            String source, String caseLevel, String areaCode, String streetCode,
            String communityCode, String gridId, String description, String fieldintro,
            String fileIdVerify, int bzhours, int bzminis, String shopId, String shopkeeperName,
            String shopPhone
    ) {
        return api.submitShopCase(
                AuthenticationHelper.getToken(), isNeedVerify, isNeedRevisit, longitude, latitude,
                source, caseLevel, areaCode, streetCode, communityCode, gridId, description,
                fieldintro, fileIdVerify, bzhours, bzminis, shopId, shopkeeperName, shopPhone
        );
    }

    /**
     * 案卷处理分页查询，去掉三包类型的案卷
     */
    public static Observable<ResponseBody> handleCaseResult(int limit, int offset) {
        return api.getHandleResult(AuthenticationHelper.getToken(), "0", limit, offset);
    }

    /**
     * 处理已办分页查询
     */
    public static Observable<ResponseBody> handledCaseResult(int offset) {
        return api.getHandledResult(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 紧急案件分页查询
     */
    public static Observable<ResponseBody> getUrgentCase(String isSupervise, int offset) {
        return api.getUrgentCase(AuthenticationHelper.getToken(), isSupervise, LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 超时案件分页查询
     */
    public static Observable<ResponseBody> getOvertimeCase(String isSupervise, int offset) {
        return api.getOvertimeCase(AuthenticationHelper.getToken(), isSupervise, LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 人员查询
     */
    public static Observable<ResponseBody> getOnlinePerson(String deptid, String roleTips, String isOnLine) {
        return api.getOnlinePerson(AuthenticationHelper.getToken(), deptid, roleTips, isOnLine);
    }

    /**
     * 设备定位信息上传
     */
    public static Observable<ResponseBody> getPositionUploadResult(String imei, String positionLat, String positionLng) {
        return api.uploadDevicePosition(AuthenticationHelper.getToken(), imei, positionLat, positionLng);
    }

    /**
     * 获取流转下级节点
     *
     * @param caseState 字典值
     */
    public static Observable<ResponseBody> getProcessNextNodeResult(String caseState) {
        return api.getProcessNextNode(AuthenticationHelper.getToken(), caseState);
    }

    /**
     * 获取常见语句
     */
    public static Observable<ResponseBody> getCommonSentenceResult() {
        return api.getCommonSentence(AuthenticationHelper.getToken());
    }

    /**
     * 案卷处理
     * <p>
     * 案卷核实-仅添加核实意见/核实情况/核实图片
     */
    public static Observable<ResponseBody> getHandleCaseResult(
            String processId, String bizId, String currState, String changeState, String remarks, String fileId
    ) {
        return api.handleCase(
                AuthenticationHelper.getToken(), processId, bizId, currState, changeState, remarks, fileId
        );
    }

    /**
     * 案卷处理
     * <p>
     * 超时案卷
     */
    public static Observable<ResponseBody> getHandleCaseResult(
            String processId, String bizId, String currState, String changeState, String remarks,
            String fileId, String approvalResult
    ) {
        return api.handleCase(
                AuthenticationHelper.getToken(), processId, bizId, currState, changeState, remarks, fileId, approvalResult
        );
    }

    /**
     * 案卷处理
     * <p>
     * 案卷核实-热线电话和微信公众号提交的数据，需要补全信息
     * 案卷核查
     */
    public static Observable<ResponseBody> getHandleCaseResult(
            String processId, String bizId, String currState, String changeState, String remarks,
            String fileId, String eorc, String casetypeCode, String casetypeDetailCode, String lng,
            String lat, String caseLevel, String areaCode, String streetCode, String communityCode,
            String gridId, String fieldintro
    ) {
        return api.handleCase(
                AuthenticationHelper.getToken(), processId, bizId, currState, changeState, remarks,
                fileId, eorc, casetypeCode, casetypeDetailCode, lng, lat, caseLevel, areaCode,
                streetCode, communityCode, gridId, fieldintro
        );
    }

    /**
     * 案卷处理
     * <p>
     * 派遣员处理
     */
    public static Observable<ResponseBody> getDispatcherHandleCaseResult(
            String processId, String bizId, String currState, String changeState, String remarks, String approvalResult, String operationKey, String taskUserId
    ) {
        return api.dispatcherHandleCase(
                AuthenticationHelper.getToken(), processId, bizId, currState, changeState, remarks, approvalResult, operationKey, taskUserId
        );
    }

    /**
     * 紧急要件-超时督办
     */
    public static Observable<ResponseBody> getUpdateSuperviseResult(String id, String superviseRemarks) {
        return api.updateSuperviseRemarks(AuthenticationHelper.getToken(), id, superviseRemarks);
    }

    /**
     * 获取首页九宫格数据
     */
    public static Observable<ResponseBody> getHomePageData(String resourceType) {
        return api.getHomePageData(AuthenticationHelper.getToken(), resourceType);
    }

    /**
     * 个推推送cid注册
     */
    public static Observable<ResponseBody> getRegisterResult(String clientId) {
        return api.registerPush(AuthenticationHelper.getToken(), clientId);
    }

    /**
     * 公厕列表
     */
    public static Observable<ResponseBody> getToilets() {
        return api.getToilets(AuthenticationHelper.getToken());
    }

    /**
     * 公厕打扫签到
     */
    public static Observable<ResponseBody> getCleanToiletResult(
            String toiletId, String userId, String lng, String lat, String startTime, String endTime, String photo, String workContent
    ) {
        return api.cleanToilet(
                AuthenticationHelper.getToken(), toiletId, userId, lng, lat, startTime, endTime, photo, workContent
        );
    }

    /**
     * 公厕打扫记录
     */
    public static Observable<ResponseBody> getCleanRecordResult(String userId, String keywords, String beginDate, String endDate, int offset) {
        return api.getToiletCleanRecord(
                AuthenticationHelper.getToken(), userId, keywords, beginDate, endDate, LocaleConstant.PAGE_LIMIT, offset
        );
    }

    /**
     * 三包待办
     */
    public static Observable<ResponseBody> getPackageWorkListResult(String isShop, int offset) {
        return api.getPackageWorkListPage(AuthenticationHelper.getToken(), isShop, LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 三包已办
     */
    public static Observable<ResponseBody> getCompletedListResult(String isShop, int offset) {
        return api.getCompletedListPage(AuthenticationHelper.getToken(), isShop, LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 三包处罚
     */
    public static Observable<ResponseBody> getHandlePackageResult(
            String id, String punishType, String punishRemarks, String punishTime, String punishImage
    ) {
        return api.handlePackageCase(
                AuthenticationHelper.getToken(), id, punishType, punishRemarks, punishTime, punishImage
        );
    }

    /**
     * 商铺变更
     */
    public static Observable<ResponseBody> getShopChangeResult(
            String merchantId, String merchantName, String contactName, String contactPhone, String images
    ) {
        return api.changeShopInfo(
                AuthenticationHelper.getToken(), merchantId, "1", merchantName, contactName, contactPhone, images, "1"
        );
    }

    /**
     * 二级派遣员待办案卷
     */
    public static Observable<ResponseBody> getWorkListResult(int offset) {
        return api.getWorkListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 二级派遣员待处理案卷
     */
    public static Observable<ResponseBody> getProcessListResult(int offset) {
        return api.getProcessListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 二级派遣员已办案卷
     */
    public static Observable<ResponseBody> getCompletedListResult(int offset) {
        return api.getCompletedListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 二级缓办待处理
     */
    public static Observable<ResponseBody> getPostponeListResult(int offset) {
        return api.getPostponeListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 二级延期待处理
     */
    public static Observable<ResponseBody> getDelayedListResult(int offset) {
        return api.getDelayedListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 部门授权审核
     */
    public static Observable<ResponseBody> getCaseAuthorizeResult(int offset) {
        return api.getCaseAuthorizeListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 部门调整批示
     */
    public static Observable<ResponseBody> getCaseAdjustResult(int offset) {
        return api.getCaseAdjustListPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 获取下级处置员
     */
    public static Observable<ResponseBody> getProcessPersonResult() {
        return api.getNextProcessPerson(AuthenticationHelper.getToken(), "process");
    }

    /**
     * 根据养护类型获取养护内容
     */
    public static Observable<ResponseBody> getMaintainContentResult(String type) {
        return api.getMaintainContent(AuthenticationHelper.getToken(), type);
    }

    /**
     * 桥梁新增打卡记录
     */
    public static Observable<ResponseBody> getAddMaintainRecordResult(
            String bridgeId, String content, String maintainTime, String personId, String longitude, String latitude, String description
    ) {
        return api.addMaintainRecord(
                AuthenticationHelper.getToken(), bridgeId, content, maintainTime, personId, longitude, latitude, description);
    }

    /**
     * 桥梁养护记录
     */
    public static Observable<ResponseBody> getMaintainRecord(String startDate, String endDate, int offset) {
        return api.getMaintainRecord(AuthenticationHelper.getToken(), startDate, endDate, LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 灯箱分页列表
     */
    public static Observable<ResponseBody> getLampBoxesByPage(int offset) {
        return api.getLampBoxesByPage(AuthenticationHelper.getToken(), LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 全部灯箱
     */
    public static Observable<ResponseBody> getLampBoxes() {
        return api.getLampBoxes(AuthenticationHelper.getToken());
    }

    /**
     * 灯箱详情
     */
    public static Observable<ResponseBody> getLampBoxDetail(String lampBoxId) {
        return api.getLampBoxDetail(AuthenticationHelper.getToken(), lampBoxId);
    }

    /**
     * 控制路灯开关
     */
    public static Observable<ResponseBody> controlLampBox(String lampBoxId, String controlType) {
        return api.controlLampBox(AuthenticationHelper.getToken(), lampBoxId, controlType);
    }

    /**
     * 报警列表
     */
    public static Observable<ResponseBody> getAlarmList(int offset) {
        return api.getAlarmList(AuthenticationHelper.getToken(), "1", LocaleConstant.PAGE_LIMIT, offset);
    }

    /**
     * 单个取消报警
     */
    public static Observable<ResponseBody> getCancelAlarmResult(String id, String jobStatus, String handleMessage) {
        return api.cancelAlarm(AuthenticationHelper.getToken(), id, jobStatus, handleMessage);
    }

    /**
     * 一键取消报警
     */
    public static Observable<ResponseBody> getCancelAlarmResult(String jobStatus, String handleMessage) {
        return api.cancelAlarm(AuthenticationHelper.getToken(), jobStatus, handleMessage);
    }

    /**
     * 窨井列表
     */
    public static Observable<ResponseBody> getWellList(int offset) {
        return api.getWellList(
                AuthenticationHelper.getToken(), "wellCode", "asc", LocaleConstant.PAGE_LIMIT, offset
        );
    }

    /**
     * 单个撤防/布防
     */
    public static Observable<ResponseBody> getSingleOptionWellResult(String wellId, String bfzt) {
        return api.optionWellStatus(AuthenticationHelper.getToken(), wellId, bfzt);
    }

    /**
     * 一键撤防/布防
     */
    public static Observable<ResponseBody> getBatchOptionWellResult(String bfzt) {
        return api.optionWellStatus(AuthenticationHelper.getToken(), bfzt);
    }

    private static final RetrofitService arcgisApi = RetrofitFactory.createRetrofit(
            "http://111.198.10.15:13002", RetrofitService.class, true
    );

    /**
     * 报警窨井详情
     */
    public static Observable<ResponseBody> getWellDetailResult(String where) {
        return arcgisApi.getWellDetail(
                "json", "4326", "4326", "*", true, where
        );
    }
}
