package com.nokia.domainb.capability;

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

import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nokia.domainb.capability.entity.Route;

import cn.chinaunicom.open.common.Const;
import cn.chinaunicom.open.nlgxptconnection.COMPConnection;
import cn.chinaunicom.open.nlgxptconnection.COMPConnectionContext;
import cn.chinaunicom.open.nlgxptconnection.impl.COMPJsonConnection;

/**
 * 路由查询
 */
public class QryRouteAll extends AbstractQuery {
    // 测试环境url
    private static final String urlTest = "http://10.124.150.230:8000/api/microservice/routes/qryrouteall/v1";
    // 生产环境url
    private static final String url = "http://192.168.70.130:8000/api/microservice/routes/qryrouteall/v1";

    public String query(String phoneNumber) {
        return query(phoneNumber, false);
    }

    // 使用RestTemplate查询
    public String query(String phoneNumber, boolean test) {
        // 整体包装
        JSONObject compParamObject = new JSONObject();
        // UNI_BSS_HEAD
        compParamObject.put("UNI_BSS_HEAD", getUniBSSHead(test));
        // UNI_BSS_BODY
        JSONObject uniBSSBody = new JSONObject();
        // QRYROUTEALL_REQ
        JSONObject qryRouteAllReq = new JSONObject();
        // REQ
        JSONObject req = new JSONObject();
        // 查询号码
        req.put("ROUTE_VALUE", phoneNumber);
        // 网别 支持 30固话 40宽带 50移网
        req.put("NET_TYPE_CODE", 50);
        qryRouteAllReq.put("REQ", req);
        uniBSSBody.put("QRYROUTEALL_REQ", qryRouteAllReq);
        compParamObject.put("UNI_BSS_BODY", uniBSSBody);

        String realUrl = test ? urlTest : url;

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(realUrl,
                new HttpEntity<>(compParamObject, getHeaders()), String.class);
        return response.getBody();
    }

    public COMPConnectionContext queryWithSdk(String phoneNumber) {
        return queryWithSdk(phoneNumber, false);
    }

    /**
     * 使用能力商店SDK查询
     */
    public COMPConnectionContext queryWithSdk(String phoneNumber, boolean test) {
        String realAppSecret = test ? appSecretTest : appSecret;
        String realUrl = test ? urlTest : url;
        COMPConnection compConnection = new COMPJsonConnection(appId, realAppSecret);
        // REQ
        Map<String, Object> req = new HashMap<>();
        // 查询号码
        req.put("ROUTE_VALUE", phoneNumber);
        // 网别 支持 30固话 40宽带 50移网
        req.put("NET_TYPE_CODE", "50");
        // QRYROUTEALL_REQ
        Map<String, Object> qryRouteAllReq = new HashMap<>();
        qryRouteAllReq.put("REQ", req);
        Map<String, Object> uniBssBody = new HashMap<>();
        uniBssBody.put("QRYROUTEALL_REQ", qryRouteAllReq);
        return compConnection.excute(Const.HttpMethodType.POST, realUrl, uniBssBody, false);
    }

    public Route query2(String phoneNumber, boolean test) {
        String realAppSecret = test ? appSecretTest : appSecret;
        String realUrl = test ? urlTest : url;
        COMPConnection compConnection = new COMPJsonConnection(appId, realAppSecret);
        // REQ
        Map<String, Object> req = new HashMap<>();
        // 查询号码
        req.put("ROUTE_VALUE", phoneNumber);
        // 网别 支持 30固话 40宽带 50移网
        req.put("NET_TYPE_CODE", "50");
        // QRYROUTEALL_REQ
        Map<String, Object> qryRouteAllReq = new HashMap<>();
        qryRouteAllReq.put("REQ", req);
        Map<String, Object> uniBssBody = new HashMap<>();
        uniBssBody.put("QRYROUTEALL_REQ", qryRouteAllReq);
        COMPConnectionContext context = compConnection.excute(Const.HttpMethodType.POST, realUrl, uniBssBody, false);
        JSONObject result = JSON.parseObject(context.getResponseContext()).getJSONObject("UNI_BSS_BODY")
                .getJSONObject("QRYROUTEALL_RSP").getJSONObject("RSP");
        if ("0000".equals(result.get("SUB_CODE"))) {
            // 确认调用成功
            Route route = JSON.parseObject(result.getJSONArray("DATA").getString(0), Route.class);
            result = result.getJSONArray("DATA").getJSONObject(0);
            // 1：drds 号码在用状态 且为新架构号码 2：oracle 号码在用状态 且为cBss常态化号码 3：非CB 号码在cBss系统没有路由记录
            // 4：drds 号码失效状态 且为新架构号码 5：oracle 号码失效状态 且为cBss常态化号码
            System.out.println(result);
           
            System.out.println(route);
        }
        return null;
    }

}
