package com.usky.cdi.service.sip.device;

import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * GB28181 MANSCDP+xml 目录应答（Catalog / DeviceInfo）。
 */
final class Gb28181CatalogBuilder {

    private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

    private Gb28181CatalogBuilder() {
    }

    static String buildCatalogResponse(String sn, String deviceId, String channelId, String channelName) {
        String now = LocalDateTime.now().format(TIME_FMT);
        String name = StringUtils.hasText(channelName) ? channelName : "Channel-1";
        return "<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n"
                + "<Response>\r\n"
                + "<CmdType>Catalog</CmdType>\r\n"
                + "<SN>" + escapeXml(sn) + "</SN>\r\n"
                + "<DeviceID>" + escapeXml(deviceId) + "</DeviceID>\r\n"
                + "<SumNum>1</SumNum>\r\n"
                + "<DeviceList Num=\"1\">\r\n"
                + "<Item>\r\n"
                + "<DeviceID>" + escapeXml(channelId) + "</DeviceID>\r\n"
                + "<Name>" + escapeXml(name) + "</Name>\r\n"
                + "<Manufacturer>USky</Manufacturer>\r\n"
                + "<Model>Gb28181Simulator</Model>\r\n"
                + "<Owner>Owner</Owner>\r\n"
                + "<CivilCode>" + civilCode(deviceId) + "</CivilCode>\r\n"
                + "<Address>Simulator</Address>\r\n"
                + "<Parental>0</Parental>\r\n"
                + "<ParentID>" + escapeXml(deviceId) + "</ParentID>\r\n"
                + "<SafetyWay>0</SafetyWay>\r\n"
                + "<RegisterWay>1</RegisterWay>\r\n"
                + "<Secrecy>0</Secrecy>\r\n"
                + "<Status>ON</Status>\r\n"
                + "<Event>ADD</Event>\r\n"
                + "<RegisterTime>" + now + "</RegisterTime>\r\n"
                + "</Item>\r\n"
                + "</DeviceList>\r\n"
                + "</Response>\r\n";
    }

    static String buildDeviceInfoResponse(String sn, String deviceId) {
        String now = LocalDateTime.now().format(TIME_FMT);
        return "<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n"
                + "<Response>\r\n"
                + "<CmdType>DeviceInfo</CmdType>\r\n"
                + "<SN>" + escapeXml(sn) + "</SN>\r\n"
                + "<DeviceID>" + escapeXml(deviceId) + "</DeviceID>\r\n"
                + "<Result>OK</Result>\r\n"
                + "<DeviceName>Gb28181Simulator</DeviceName>\r\n"
                + "<Manufacturer>USky</Manufacturer>\r\n"
                + "<Model>Gb28181Simulator</Model>\r\n"
                + "<Firmware>1.0</Firmware>\r\n"
                + "<Channel>1</Channel>\r\n"
                + "<RegisterTime>" + now + "</RegisterTime>\r\n"
                + "</Response>\r\n";
    }

    static String extractXmlValue(String xml, String tag) {
        if (!StringUtils.hasText(xml) || !StringUtils.hasText(tag)) {
            return null;
        }
        String open = "<" + tag + ">";
        String close = "</" + tag + ">";
        int start = xml.indexOf(open);
        if (start < 0) {
            return null;
        }
        start += open.length();
        int end = xml.indexOf(close, start);
        if (end < 0) {
            return null;
        }
        return xml.substring(start, end).trim();
    }

    private static String civilCode(String deviceId) {
        if (deviceId != null && deviceId.length() >= 6) {
            return deviceId.substring(0, 6);
        }
        return "340200";
    }

    private static String escapeXml(String value) {
        if (value == null) {
            return "";
        }
        return value.replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("\"", "&quot;")
                .replace("'", "&apos;");
    }
}
