package fcb.project.manager.base.service.custom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import fcb.project.manager.base.entity.*;
import fcb.project.manager.core.feignInterfaces.PanoFeign;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 2 * @Author: Abner
 * 3 * @Date: 2021/1/26 19:09
 * 4
 */
@Service
@Log4j2
public class PanoService {

    @Autowired
    PanoFeign panoFeign;

    public boolean noticeVrAuditStatus(String houseId, String token , int status){

        if(StringUtils.isBlank(houseId)){
            return false;
        }
        CommonRspBean rspBean = panoFeign.auditedVrNotice(houseId, status , token );
        log.info("通知720VR项目审核结果{}，720返回结果:{}" , status , JSON.toJSONString(rspBean));
        if(null == rspBean || null == rspBean.getCode() || rspBean.getCode().compareTo(0) != 0){
            return false;
        }
        log.info("成功通知720VR项目审核结果");
        return true;
    }


    public boolean noticeDeleteVr(String houseId, String token){

        if(StringUtils.isBlank(houseId)){
            return false;
        }
        CommonRspBean rspBean = panoFeign.deleteVrNotice(houseId , token);
        log.info("通知720删除V项目，720返回结果:{}" , JSON.toJSONString(rspBean));
        if(null == rspBean || null == rspBean.getCode() || rspBean.getCode().compareTo(0) != 0){
            return false;
        }
        log.info("成功通知720VR项目已经删除");
        return true;
    }

    public List<PanoInitSceneBean> getAllTypeSceneNums(String houseId, String token) {
        if (StringUtils.isBlank(houseId)) {
            return null;
        }
        PanoInitSceneTotalRsp rspBean = panoFeign.getAllTypeSceneNums(houseId , token);
        log.info("获取房源的初始化场景数据，从720返回结果:{}" , JSON.toJSONString(rspBean));
        if(null == rspBean || null == rspBean.getCode() || rspBean.getCode().compareTo(0) != 0){
            return null;
        }
        if(CollectionUtils.isEmpty(rspBean.getData())){
            return null;
        }
        return rspBean.getData();
    }

    public List<String> getKanKanSceneNums(String houseId, String token) {
        if (StringUtils.isBlank(houseId)) {
            return null;
        }
        SevenTwoZeroSceneRspBean sevenTwoZeroSceneRspBean = null;
        try {
            sevenTwoZeroSceneRspBean  =  panoFeign.getFdkanaknSceneNum(token , houseId , "3" , "house");
        }catch (Exception e){
            log.info("调用720出现异常:{}" , e);
            return null;
        }
        log.info("720返回结果:{}", JSON.toJSONString(sevenTwoZeroSceneRspBean));
        if (null == sevenTwoZeroSceneRspBean || null == sevenTwoZeroSceneRspBean.getCode()) {
            log.info("720返回结果解析失败");
            return null;
        }
        if (sevenTwoZeroSceneRspBean.getCode().compareTo(0) != 0) {
            log.info("720返回非成功:code={},msg={}", sevenTwoZeroSceneRspBean.getCode(), sevenTwoZeroSceneRspBean.getMsg());
            return null;
        }
        if (CollectionUtils.isEmpty(sevenTwoZeroSceneRspBean.getData())) {
            log.info("720返回的list为空，无需操作");
            return null;
        }

        return sevenTwoZeroSceneRspBean.getData();
    }

    public TreeMap<String , Object> getHouseVrlinks(List<String> houseIds , String token){

        if(CollectionUtils.isEmpty(houseIds)){
            return new TreeMap<>();
        }

        Map<String , Object> body = new HashMap<>();
        body.put("houseIds" , houseIds);
        body.put("pageNum" , 1);
        body.put("pageSize" , houseIds.size());
        CommonRspBean rspBean = panoFeign.getVrLinks(body , token);
        log.info("从720获取最新vrlink的预览路径的返回值为:{}" , JSON.toJSONString(rspBean));
        if(null == rspBean || null == rspBean.getCode() || rspBean.getCode() != 0){
            log.info("调用720获取最新的预览路径失败");
            return new TreeMap<>();
        }
        Map<String ,Object> jsonObject = null;
        try {
           jsonObject = (Map<String, Object>) rspBean.getData();
        }catch (Exception e){
            log.info("解析data数据失败");
            return new TreeMap<>();
        }
        if(null == jsonObject){
            log.info("解析720返回的data失败");
            return new TreeMap<>();
        }
        if(!jsonObject.containsKey("list")){
            log.info("解析720返回的data缺失list");
            return new TreeMap<>();
        }
        List<Map<String, Object>> list = null;
        try {
            list = (List<Map<String, Object>>) jsonObject.get("list");
        }catch (Exception e){
            log.info("720返回的data的list转换失败");
            return new TreeMap<>();
        }
        if(null == list || list.isEmpty()){
            log.info("720返回的data的list为空");
            return new TreeMap<>();
        }
        TreeMap<String , Object> resultTree = new TreeMap<>();
        for (int i = 0; i < list.size(); i++) {
            resultTree.putAll(list.get(i));
        }
        return resultTree;
    }

    public String getOneVrLink(String houseId , String token){

        if(StringUtils.isNoneBlank(houseId , token)){
            CommonRspBean rspBean = panoFeign.getOneVrlink(houseId , token);
            log.info("从720获取最新单个vrlink的预览路径的返回值为:{}" , JSON.toJSONString(rspBean));
            if(null == rspBean || null == rspBean.getCode() || rspBean.getCode() != 0){
                log.info("调用720获单个取最新的预览路径失败");
                return "";
            }
            PanoSceneBean panoSceneBean = null;

            try {
                panoSceneBean = JSONObject.parseObject(JSON.toJSONString(rspBean.getData()) , PanoSceneBean.class);

            }catch (Exception e){
                log.info("720返回的data转换失败");
                return "";
            }
            if(null == panoSceneBean){
                log.info("720返回的data为空");
                return "";
            }
            return panoSceneBean.getWebSite();
        }
        return "";
    }

    public Map<String ,Object> getStatistics(String houseId , String token){

        if(StringUtils.isNoneBlank(houseId , token)){
            CommonRspBean rspBean = panoFeign.statistics(houseId , token);
            log.info("从720获取最新statistics的预览路径的返回值为:{}" , JSON.toJSONString(rspBean));
            if(null == rspBean || null == rspBean.getCode() || rspBean.getCode() != 0){
                log.info("调用720获取最新的预览路径失败");
                return null;
            }
            Map<String ,Object> jsonObject = null;
            try {
                jsonObject = (Map<String, Object>) rspBean.getData();
            }catch (Exception e){
                log.info("解析data数据失败");
                return null;
            }
            return jsonObject;
        }
        return null;
    }

    public List<String> getAllSceneNums(String houseId, String token) {
        if (StringUtils.isBlank(houseId)) {
            return null;
        }
        SevenTwoZeroSceneRspBean sevenTwoZeroSceneRspBean = null;
        try {
            sevenTwoZeroSceneRspBean  =  panoFeign.getAllSceneNum(token , houseId);
        }catch (Exception e){
            log.info("调用720出现异常:{}" , e);
            return null;
        }
        log.info("720返回结果:{}", JSON.toJSONString(sevenTwoZeroSceneRspBean));
        if (null == sevenTwoZeroSceneRspBean || null == sevenTwoZeroSceneRspBean.getCode()) {
            log.info("720返回结果解析失败");
            return null;
        }
        if (sevenTwoZeroSceneRspBean.getCode().compareTo(0) != 0) {
            log.info("720返回非成功:code={},msg={}", sevenTwoZeroSceneRspBean.getCode(), sevenTwoZeroSceneRspBean.getMsg());
            return null;
        }
        if (CollectionUtils.isEmpty(sevenTwoZeroSceneRspBean.getData())) {
            log.info("720返回的list为空，无需操作");
            return null;
        }

        return sevenTwoZeroSceneRspBean.getData();
    }

    public Map<String, Object> listPanoSceneWithTypes(String id, String token) {
        if (StringUtils.isBlank(id)) {
            throw new RuntimeException("调用feign-listPanoSceneWithTypes方法时id为空");
        }
        log.info("调用feign-listPanoSceneWithTypes入参值-{}", id);
        SceneRspBean sceneRspBean = null;
        try {
            sceneRspBean  =  panoFeign.listPanoSceneWithTypes(token , id);
        }catch (Exception e){
            log.info("调用720出现异常:{}" , e);
            throw new RuntimeException("调用feign-listPanoSceneWithTypes方法时返回异常");
        }
        Map<String, Object> rs = (Map<String, Object>) sceneRspBean.getData();
        log.info("调用feign-listPanoSceneWithTypes方法时返回数据-{}", rs);
        return rs;
    }
}
