package com.nokia.domainb.capability;

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.entity.Route;
import com.nokia.domainb.util.Utils;
import de.siegmar.fastcsv.writer.CsvWriter;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Set;

public class QryRouteAllTest {
    @Test
    void testQuery() {
        QryRouteAll qryRouteAll = new QryRouteAll();
//        String result = qryRouteAll.query("18503369666");
//        System.out.println(result);
//        Set<String> phones = Collections.singleton("13739798862");
        Set<String> phones = Utils.readPhones();
        String phonesPath = "phones.txt";
        String filePath = "result/路由查询.csv";
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(phonesPath));
             OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(filePath)), "GBK");
             CsvWriter csv = CsvWriter.builder().build(osw);) {
            csv.writeRow("手机号", "时间", "调用时长", "结果");
            for (String t : phones) {
                LocalDateTime startTime = LocalDateTime.now();
                long startTimestamp = System.currentTimeMillis();
                String result = qryRouteAll.query(t);
                long endTimestamp = System.currentTimeMillis();
                long cost = endTimestamp - startTimestamp;
                csv.writeRow(t, startTime.toString(), String.valueOf(cost), result);
                JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
                JsonObject data = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                        .getAsJsonObject("QRYROUTEALL_RSP")
                        .getAsJsonObject("RSP")
                        .getAsJsonArray("DATA")
                        .get(0)
                        .getAsJsonObject();
                JsonElement userStatusElement = data.get("USER_STATUS");
                if (userStatusElement == null) {
                    System.out.println(t + " -> userStatus is null");
                    continue;
                }
                String userStatus = userStatusElement.getAsString();
                JsonElement provinceCodeElement = data.get("PROVINCE_CODE");
                if (provinceCodeElement == null) {
                    System.out.println(t + " -> provinceCode is null");
                    continue;
                }
                String provinceCode = data.get("PROVINCE_CODE").getAsString();
                JsonElement eparchyCodeElement = data.get("EPARCHY_CODE");
                if (eparchyCodeElement == null) {
                    System.out.println(t + " -> eparchyCode is null");
                }
                String eparchyCode = eparchyCodeElement.getAsString();
                boolean invalidPhone = !("1".equals(userStatus) || "2".equals(userStatus)
                        || !StringUtils.hasText(provinceCode) || !StringUtils.hasText(eparchyCode));
                if (invalidPhone) {
                    continue;
                }

                try {
                    bufferedWriter.write(t + "," + provinceCode + "," + eparchyCode + "\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void testQueryWithSdk() {
        QryRouteAll qryRouteAll = new QryRouteAll();
        COMPConnectionContext result = qryRouteAll.queryWithSdk("18631229993");
        System.out.println(result.getResponseContext());
    }

    @Test
    void testQuery2() {
        QryRouteAll qryRouteAll = new QryRouteAll();
        Route result = qryRouteAll.query2("13231899751", false);
        System.out.println(result);
    }

    @Test
    void testQuery22() {
        QryRouteAll qryRouteAll = new QryRouteAll();
        Route result = qryRouteAll.query2("13739798862", false);
        System.out.println(result);
    }
}
