package com.nokia.domainb.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.SpThreePartCheck;
import com.nokia.domainb.dto.RouteDTO;
import com.nokia.domainb.dto.SaAndVolteDTO;
import com.nokia.domainb.dto.SaDTO;
import com.nokia.domainb.dto.UserStateDTO;
import com.nokia.domainb.vo.RouteVO;
import com.nokia.domainb.vo.SaAndVolteVO;
import com.nokia.domainb.vo.SaVO;
import com.nokia.common.http.R;
import com.nokia.domainb.vo.UserStateVO;
import org.springframework.stereotype.Service;

@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 rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("QRYROUTEALL_RSP")
                .getAsJsonObject("RSP");
        String rspCode = rsp.get("RSP_CODE").getAsString();
        if (!"0000".equals(rspCode)) {
            String rspDesc = rsp.get("RSP_DESC").getAsString();
            return R.error().message(rspDesc);
        }

        RouteVO vo = new RouteVO();
        vo.setPhoneNumber(dto.getPhoneNumber());
        JsonObject data = rsp.getAsJsonArray("DATA").get(0).getAsJsonObject();
        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());
        }

        if ("3".equals(vo.getUserStatus())) {
            return R.error().message("非CB 号码在cBss系统没有路由记录");
        }

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

    public R sa(SaDTO dto) {
        R r = route(new RouteDTO(dto.getFromSystem(), dto.getPhoneNumber()));
        if (Boolean.FALSE.equals(r.getSuccess())) {
            return r;
        }

        RouteVO routeVO = (RouteVO) r.getData();
        SaVO vo = new SaVO();
        vo.setIsUnicomNumber(true);
        QryUserIsUdm qryUserIsUdm = new QryUserIsUdm();
        COMPConnectionContext result = qryUserIsUdm.query(dto.getPhoneNumber(), routeVO.getProvinceCode(), false);
        JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
        JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("QRY_USER_IS_UDM_RSP")
                .getAsJsonObject("RSP");
        String rspCode = rsp.get("RSP_CODE").getAsString();
        if (!"0000".equals(rspCode)) {
            String rspDesc = rsp.get("RSP_DESC").getAsString();
            return R.error().message(rspDesc);
        }

        JsonObject data = 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) {
        Foreignproduct foreignproduct = new Foreignproduct();
        COMPConnectionContext result = foreignproduct.query(dto.getPhoneNumber(), "18", false);
        JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
        JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("FOREIGN_PRODUCT_RSP")
                .getAsJsonObject("RSP");
        String rspCode = rsp.get("RSP_CODE").getAsString();
        if (!"0000".equals(rspCode)) {
            String rspDesc = rsp.get("RSP_DESC").getAsString();
            return R.error().message(rspDesc);
        }

        SaAndVolteVO vo = new SaAndVolteVO();
        vo.setIsHaveSa(false);
        vo.setIsHaveVolte(false);
        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);
    }

    public R userState(UserStateDTO dto) {
        SpThreePartCheck spThreePartCheck = new SpThreePartCheck();
        COMPConnectionContext result = spThreePartCheck.query(dto.getPhoneNumber(), "18", false);
        JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
        JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("SP_THREEPART_CHECK_RSP")
                .getAsJsonObject("RSP");
        String rspCode = rsp.get("RSP_CODE").getAsString();
        if (!"0000".equals(rspCode)) {
            String rspDesc = rsp.get("RSP_DESC").getAsString();
            return R.error().message(rspDesc);
        }

        JsonObject data = rsp.getAsJsonArray("DATA").get(0).getAsJsonObject();
        JsonElement userStateElement = data.getAsJsonObject("userInfo").get("userState");
        if (userStateElement == null) {
            String detail = data.get("detail").getAsString();
            return R.error().message(detail);
        }

        String userState = userStateElement.getAsString();
        return R.ok().data(new UserStateVO(userState));
    }
}
