package com.fdkankan.scene.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.HttpCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.scene.service.IFdkkLaserService;
import com.fdkankan.scene.vo.SceneBySnCodeVo;
import com.fdkankan.scene.vo.SceneNumVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IFdkkLaserServiceImpl implements IFdkkLaserService {

    private final String UPDATE_SCENE_STATUS_URL_TEMPLATE="/indoor/%s/scene/useStatus/%s";
    private final String DISABLE_COOPERATION_SCENE_URL="/laser/4dage/scene/cooperation/disable";
    private final String ENABLE_COOPERATION_CAMERA_URL="/laser/4dage/scene/cooperation/cameraSave";
    private final String GET_LASER_SCENE_USER_URL="/laser/4dage/getSceneNum";
    private final String GET_LASER_SCENE_CODE_URL="/laser/4dage/scene/getScenesBySnCode";
    private final String GET_LASER_SCENE_CNT_URL="/laser/4dage/scene/getSceneNumByCamera";

    @Value("${4dkk.laserService.host}")
    private String laserHost;

    private RestTemplate restTemplate = new RestTemplate();

    public void updateSceneStatus(String sceneCode,int status){
        String url = laserHost + String.format(UPDATE_SCENE_STATUS_URL_TEMPLATE,sceneCode,status);
        ResponseEntity<Result> response = restTemplate.getForEntity(url, Result.class);
        if(response.getStatusCode() != HttpStatus.OK){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7001);
        }
        if(response.getBody().getCode() != HttpCode.SC_SUCCESS){
            throw new BusinessException(response.getBody().getCode(), response.getBody().getMsg());
        }
    }

    /**
     * 自研激光转台取消协作
     */
    @Override
    public void disableCooperation(String snCode, String cooperationUserName) {
        List<Map<String,String>> params = new ArrayList<>();
        Map<String,String> param = new HashMap<>();
        param.put("snCode",snCode);
        param.put("cooperationUserName",cooperationUserName);
        params.add(param);

        String url = laserHost + DISABLE_COOPERATION_SCENE_URL;
        log.info("自研激光转台取消协作，url:{},params:{}",DISABLE_COOPERATION_SCENE_URL, JSONObject.toJSONString(params));
        ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url,params, Result.class);
        log.info("自研激光转台相机同步，url:{},params:{},结果:{}",url, JSONObject.toJSONString(params),JSONObject.toJSONString(responseEntity.getBody()));
        if(responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != HttpStatus.OK.value()){
            log.error("激光场景状态同步失败!");
        }
    }
    /**
     * 自研激光转台添加协作
     */
    @Override
    public void enableCameraCooperation(String snCode, String cooperationUserName) {
        List<Map<String,String>> params = new ArrayList<>();
        Map<String,String> param = new HashMap<>();
        param.put("snCode",snCode);
        param.put("cooperationUserName",cooperationUserName);
        params.add(param);

        String url = laserHost + ENABLE_COOPERATION_CAMERA_URL;
        log.info("自研激光转台相机协作，url:{},params:{}",ENABLE_COOPERATION_CAMERA_URL, JSONObject.toJSONString(params));
        ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url,params, Result.class);
        log.info("自研激光转台相机协作，url:{},params:{},结果:{}",url, JSONObject.toJSONString(params),JSONObject.toJSONString(responseEntity.getBody()));
        if(responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != HttpStatus.OK.value()){
            log.error("自研激光转台相机协作失败!");
        }
    }

    /**
     * 根据用户获取激光相机数量
     * @param token
     * @return
     */
    @Override
    public SceneNumVo getLaserSceneNumByUser(String token) {
        SceneNumVo sceneNumVo = new SceneNumVo();
        try {
            String url = laserHost + GET_LASER_SCENE_USER_URL;
            HttpHeaders headers = new HttpHeaders();
            headers.add("fdToken", token);
            HttpEntity<Object> formEntity = new HttpEntity<>( headers);
            log.info("获取激光转台相机场景数，url:{},fdToken:{}",GET_LASER_SCENE_USER_URL,token);
            ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url,formEntity, Result.class);
            log.info("获取激光转台相机场景数，url:{},fdToken:{},结果:{}",url,token,JSONObject.toJSONString(responseEntity.getBody()));
            if(responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != HttpStatus.OK.value()){
                log.error("自研激光转台相机协作失败!");
                return sceneNumVo;
            }
            sceneNumVo = JSONObject.parseObject(JSONObject.toJSONString(responseEntity.getBody().getData()), SceneNumVo.class);
            sceneNumVo.setTotalNum(sceneNumVo.getSceneNum() + sceneNumVo.getCooperationSceneNum());
        }catch (Exception e){
            log.error("自研激光转台相机协作失败!",e);
        }
        return sceneNumVo ;
    }

    /**
     * 根据相机编码获取激光场景
     */
    public  List<SceneBySnCodeVo> getScenesBySnCode(String snCode, String token) {
        List<SceneBySnCodeVo> sceneVo = new ArrayList<>();
        try {
            String url = laserHost + GET_LASER_SCENE_CODE_URL;
            Map<String, String> param = new HashMap<>();
            param.put("snCode",snCode);
            HttpHeaders headers = new HttpHeaders();
            headers.add("fdToken", token);
            HttpEntity<Object> formEntity = new HttpEntity<>(param, headers);
            log.info("获取激光转台场景，url:{},param:{},fdToken:{}",GET_LASER_SCENE_USER_URL,param,token);
            ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url,formEntity, Result.class);
            log.info("获取激光转台场景，url:{},param:{},fdToken:{},结果:{}",url,param,token,JSONObject.toJSONString(responseEntity.getBody()));
            if(responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != HttpStatus.OK.value()){
                log.error("自研激光转台相机协作失败!");
                return sceneVo;
            }
            sceneVo = JSONObject.parseObject(JSONObject.toJSONString(responseEntity.getBody().getData()), List.class);
        }catch (Exception e){
            log.error("自研激光转台相机协作失败!",e);
        }
        return sceneVo ;
    }

    /**
     * 根据 snCode 获取激光场景场景数量
     */
    public Integer getLaserSceneNum(String snCode){
        Map<String, String> params = new HashMap<>();
        params.put("snCode",snCode);
        try {
            String url = laserHost + GET_LASER_SCENE_CNT_URL;
            log.info("获取激光转台相机场景数，url:{},params:{}",GET_LASER_SCENE_CNT_URL, JSONObject.toJSONString(params));
            ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url,params, Result.class);
            log.info("获取激光转台相机场景数，url:{},params:{},结果:{}",url, JSONObject.toJSONString(params),JSONObject.toJSONString(responseEntity.getBody()));
            if(responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != HttpStatus.OK.value()){
                log.error("自研激光转台相机协作失败!");
            }
            return (Integer) responseEntity.getBody().getData();
        }catch (Exception e){
            log.error("自研激光转台相机协作失败!",e);
        }
        return 0;
    }
    @Data
    public static class Result<T> implements Serializable {
        private static final long serialVersionUID = -1491499610244557029L;
        public static final String SUCCESS_MSG = "操作成功";
        public static int CODE_SUCCESS = 0;
        public static int CODE_FAILURE = -1;
        public static String[] NOOP = new String[]{};

        /**
         * 处理状态：0: 成功, 1: 失败
         */
        private int code;
        /**
         * 消息
         */
        private String msg;
        /**
         * 返回数据
         */
        private T data;
    }
}
