package com.nokia.domainb.capability.service;

import cn.chinaunicom.open.nlgxptconnection.COMPConnectionContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.nokia.domainb.capability.Foreignproduct;
import com.nokia.domainb.capability.QryRouteAll;
import com.nokia.domainb.capability.QryUserIsUdm;
import com.nokia.domainb.capability.dto.RouteDTO;
import com.nokia.domainb.capability.dto.SaAndVolteDTO;
import com.nokia.domainb.capability.dto.SaDTO;
import com.nokia.domainb.capability.vo.RouteVO;
import com.nokia.domainb.capability.vo.SaAndVolteVO;
import com.nokia.domainb.capability.vo.SaVO;
import com.nokia.domainb.common.http.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DomainService {
    public R route(RouteDTO dto) {
        QryRouteAll qryRouteAll = new QryRouteAll();
        String result = qryRouteAll.query(dto.getPhoneNumber());
        JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
        JsonObject data = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("QRYROUTEALL_RSP")
                .getAsJsonObject("RSP")
                .getAsJsonArray("DATA")
                .get(0)
                .getAsJsonObject();
        RouteVO vo = new RouteVO();
        vo.setPhoneNumber(dto.getPhoneNumber());
        JsonElement routeTypeElement = data.get("ROUTE_TYPE");
        if (routeTypeElement != null) {
            vo.setRouteType(routeTypeElement.getAsString());
        }

        JsonElement userStatusElement = data.get("USER_STATUS");
        if (userStatusElement != null) {
            vo.setUserStatus(userStatusElement.getAsString());
        }

        JsonElement provinceCodeElement = data.get("PROVINCE_CODE");
        if (provinceCodeElement != null) {
            vo.setProvinceCode(data.get("PROVINCE_CODE").getAsString());
        }

        JsonElement eparchyCodeElement = data.get("EPARCHY_CODE");
        if (eparchyCodeElement != null) {
            vo.setEparchyCode(eparchyCodeElement.getAsString());
        }
        boolean isUnicomNumber = "1".equals(vo.getUserStatus()) || "2".equals(vo.getUserStatus());
        vo.setIsUnicomNumber(isUnicomNumber);
        return R.ok().data(vo);
    }

    public R sa(SaDTO dto) {
        R r = route(new RouteDTO(dto.getFromSystem(), dto.getPhoneNumber()));
        RouteVO routeVO = (RouteVO) r.getData();
        SaVO vo = new SaVO();
        vo.setIsUnicomNumber(false);
        vo.setIsHaveSa(false);
        vo.setIsUdmUser(false);
        if (Boolean.FALSE.equals(routeVO.getIsUnicomNumber())) {
            return R.ok().data(vo);
        }

        vo.setIsUnicomNumber(true);
        QryUserIsUdm qryUserIsUdm = new QryUserIsUdm();
        COMPConnectionContext result = qryUserIsUdm.query(dto.getPhoneNumber(), routeVO.getProvinceCode(), false);
        JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
        JsonObject data = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("QRY_USER_IS_UDM_RSP")
                .getAsJsonObject("RSP")
                .getAsJsonArray("DATA")
                .get(0)
                .getAsJsonObject();
        JsonElement isHaveSaElement = data.get("IS_HAVE_SA");
        if (isHaveSaElement != null) {
            vo.setIsHaveSa("Y".equals(isHaveSaElement.getAsString()));
        }

        JsonElement isUdmUserElement = data.get("IS_UDM_USER");
        if (isUdmUserElement != null) {
            vo.setIsUdmUser("Y".equals(isUdmUserElement.getAsString()));
        }

        return R.ok().data(vo);
    }

    public R saAndVolte(SaAndVolteDTO dto) {
        R r = route(new RouteDTO(dto.getFromSystem(), dto.getPhoneNumber()));
        RouteVO routeVO = (RouteVO) r.getData();
        SaAndVolteVO vo = new SaAndVolteVO();
        vo.setIsHebeiUnicomNumber(false);
        vo.setIsHaveSa(false);
        vo.setIsHaveVolte(false);
        // 非联通号码
        if (Boolean.FALSE.equals(routeVO.getIsUnicomNumber())) {
            return R.ok().data(vo);
        }

        Foreignproduct foreignproduct = new Foreignproduct();
        COMPConnectionContext result = foreignproduct.query(dto.getPhoneNumber(), routeVO.getProvinceCode(), false);
        JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
        String status = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("FOREIGN_PRODUCT_RSP")
                .get("STATUS")
                .getAsString();
        // 非河北号码
        if ("8888".equals(status)) {
            return R.ok().data(vo);
        }

        vo.setIsHebeiUnicomNumber(true);
        String res = result.getResponseContext();
        if (res.contains("VoLTE")) {
            vo.setIsHaveVolte(true);
        }

        if (res.contains("SA")) {
            vo.setIsHaveSa(true);
        }

        return R.ok().data(vo);
    }
}
