//
//  SystemConfig.swift
//  sdlan
//
//  Created by 安礼成 on 2024/6/3.
//

import Foundation

struct SystemConfig {
    // 版本设置
    static let version: Int = 1
    
    static let version_name = "1.1"
    
    static let build: Int = 123
    
    // 渠道相关
    static let channel = "appstore"
    
    static let serverHost = "root.punchsky.com"
    
    // stun探测辅助服务器ip
    static let stunAssistHost = "root.punchsky.com"
    
    // 获取系统信息
    static let systemInfo: String = {
        let version = ProcessInfo.processInfo.operatingSystemVersion
        return "macOS \(version.majorVersion).\(version.minorVersion)"
    }()
    
    static func getOptions(networkId: UInt32,
                           networkDomain: String,
                           ip: String,
                           maskLen: UInt8,
                           accessToken: String,
                           identityId: UInt32,
                           hostname: String,
                           exitNodeIp: String?) -> [String: NSObject] {
//        guard let serverIp = DNSResolver.resolveAddrInfos(serverHost).first,
//              let stunAssistIp = DNSResolver.resolveAddrInfos(stunAssistHost).first else {
//            return nil
//        }
        
        let clientId = getClientId()
        let mac = getMacAddress()
        
        var options = [
            "version": version as NSObject,
            "client_id": clientId as NSObject,
            "access_token": accessToken as NSObject,
            "identity_id": identityId as NSObject,
            "server_host": serverHost as NSObject,
            "stun_assist_host": stunAssistHost as NSObject,
            "hostname": hostname as NSObject,
            "network_address": [
                "network_id": networkId as NSObject,
                "ip": ip as NSObject,
                "mask_len": maskLen as NSObject,
                "mac": mac as NSObject,
                "network_domain": networkDomain as NSObject
            ] as NSObject
        ]
        
        if let exitNodeIp {
            options["exit_node_ip"] = exitNodeIp as NSObject
        }
        
        return options
    }
    
    public static func getClientId() -> String {
        let userDefaults = UserDefaults.standard
        if let uuid = userDefaults.value(forKey: "gClientId") as? String {
            return uuid
        } else {
            let uuid = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
            userDefaults.setValue(uuid, forKey: "gClientId")
            
            return uuid
        }
    }
    
    // 获取mac地址
    public static func getMacAddress() -> Data {
        let key = "gMacAddress2"
        
        let userDefaults = UserDefaults.standard
        if let mac = userDefaults.value(forKey: key) as? Data {
            return mac
        }
        else {
            let mac = generateMacAddress()
            userDefaults.setValue(mac, forKey: key)
            
            return mac
        }
    }
    
    public static func macAddressString(mac: Data, separator: String = ":") -> String {
        return mac.map { String(format: "%02X", $0) }
            .joined(separator: separator)
    }
    
    // 随机生成mac地址
    private static func generateMacAddress() -> Data {
        var macAddress = [UInt8](repeating: 0, count: 6)
        for i in 0..<6 {
            macAddress[i] = UInt8.random(in: 0...255)
        }
        
        return Data(macAddress)
    }
    
}
