package com.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.platform.entity.Result;
import com.platform.enums.ResultCodeEnum;
import com.platform.exception.CommonBaseException;
import com.platform.vos.RequestScene;
import com.platform.vos.ReturnDTO;
import com.platform.vos.ZhiHouseUserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
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.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class ZhiHouseService {

    @Value("${scenePath}")
    private String zhiHouseHost;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 登录指房宝
     * @param username
     * @param password
     * @return
     */
    public ResponseEntity<String> login(String username, String password) {
        ZhiHouseUserLoginVo userLoginVo = new ZhiHouseUserLoginVo();
        userLoginVo.setUserName(username);
        userLoginVo.setUserPassword(password);
        userLoginVo.setArea("1234");
        String url = zhiHouseHost + "api/platform/login";
        return restTemplate.postForEntity(url, userLoginVo, String.class);
    }

    public ReturnDTO generateSceneEditToken(String token, String userName, String sceneNum) {
        if(ObjectUtils.isEmpty(token)){
            throw new CommonBaseException(ResultCodeEnum.D101);
        }
        Map<String, String> param = new HashMap<>();
        param.put("userName",userName);
        param.put("sceneNum",sceneNum);
        String url = zhiHouseHost + "api/platform/generateSceneEditToken";
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);
        HttpEntity<Object> formEntity = new HttpEntity<>(param, headers);
        ResponseEntity<ReturnDTO> responseEntity = restTemplate.postForEntity(url,formEntity,ReturnDTO.class);
        if(responseEntity.getStatusCode()!= HttpStatus.OK){
            throw new CommonBaseException(ResultCodeEnum.D100,"连接失败");
        }
        if (responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
            throw new CommonBaseException(ResultCodeEnum.D100,responseEntity.getBody().getError());
        }

        return responseEntity.getBody();
    }

    /**
     * 获取四维看看场景数据
     * @param username
     * @return
     */
    public ReturnDTO sync4dkkSceneData(String username) {
        Map<String ,Object> params = new HashMap<>(1);
        params.put("phone",username);
        String url = zhiHouseHost + "api/platform/sync4dkkSceneData";
        ResponseEntity<ReturnDTO> responseEntity = restTemplate.postForEntity(url, params, ReturnDTO.class);
        if(responseEntity.getStatusCode()!= HttpStatus.OK){
            throw new CommonBaseException(ResultCodeEnum.D100);
        }
        if (responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
            throw new CommonBaseException(ResultCodeEnum.D100,responseEntity.getBody().getError());
        }
        return responseEntity.getBody();
    }

    public JSONObject getSceneFromZhiHouse(RequestScene requestScene){
        String url = zhiHouseHost + "scene/list";
        if(org.apache.commons.lang3.StringUtils.isBlank(url )){
            throw new CommonBaseException(ResultCodeEnum.D101 , "入参不全，无法获取四维场景列表");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", requestScene.getToken());
        HttpEntity<Object> formEntity = new HttpEntity<>(requestScene, headers);
        ResponseEntity<ReturnDTO> responseEntity = restTemplate.postForEntity(url,formEntity,ReturnDTO.class);
        if(responseEntity.getStatusCode()!= HttpStatus.OK){
            throw new CommonBaseException(ResultCodeEnum.D100);
        }
        if (responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
            throw new CommonBaseException(ResultCodeEnum.D100,responseEntity.getBody().getError());
        }
        //把信息封装为json
        return JSONObject.parseObject(JSONObject.toJSONString(responseEntity.getBody().getMessage()));
    }

    public void bind4dkkUser(String fdkkUser, String fdkkPassword, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);
        Map<String ,Object> params = new HashMap<>(1);
        params.put("fdkkUser",fdkkUser);
        params.put("fdkkPassword",fdkkPassword);
        HttpEntity<Object> formEntity = new HttpEntity<>(params, headers);
        String url = null;
        if (ObjectUtils.isEmpty(fdkkUser) || ObjectUtils.isEmpty(fdkkPassword)) {
            url = zhiHouseHost.concat("/user/unBind4dkkUser");
        } else {
            url = zhiHouseHost.concat("/user/bind4dkkUser");
        }
        ResponseEntity<ReturnDTO> responseEntity = restTemplate.postForEntity(url, formEntity, ReturnDTO.class);
        if(responseEntity.getStatusCode()!= HttpStatus.OK){
            throw new CommonBaseException(ResultCodeEnum.D100);
        }
        if (responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
            throw new CommonBaseException(ResultCodeEnum.D100,responseEntity.getBody().getError());
        }
    }
}
