package com.jihe.punchnet.sdlan.network

// import sdlanproto.Message.*
import com.google.protobuf.kotlin.toByteString
import com.jihe.punchnet.protobuf.PunchProto
import com.jihe.punchnet.protobuf.PunchProto.SDLStunProbe
import com.jihe.punchnet.protobuf.PunchProto.SDLStunProbeReply
import com.jihe.punchnet.protobuf.PunchProto.SDLStunRequest
import com.jihe.punchnet.sdlan.config.SDLanMulticastConfig
import com.jihe.punchnet.sdlan.logs.TerminalLogger
import com.jihe.punchnet.sdlan.utils.AES
import com.jihe.punchnet.sdlan.utils.RSA
import com.jihe.punchnet.sdlan.utils.UniqueNodeID
import com.jihe.punchnet.sdlan.utils.generateRandomMAC
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.withTimeoutOrNull
import java.net.InetSocketAddress
import java.net.SocketAddress
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong

class DeviceConfig (
    var mtu: Int,
    var mac: Mac,
    var ip: IPSubnet,
) {
    constructor(mtu: Int): this(
        mtu,
        ByteArray(6).toByteString(),
        IPSubnet(0, 0),
    )
}


class NodeConfig (
    val baseDir: String,
    val name: String,
    val nodeUUID: String,

    val allowRouting: Boolean,
    val dropMulticast: Boolean,

    val allowP2P: Boolean,
    val mtu: Int,
    val tos: Int,
    val registerSuperInterval: Byte,

    val registerTTL: Int,
    val localPort: Int,

    val supernode: SDLanSock,
    val natServer1: InetSocketAddress,
    val natServer2: InetSocketAddress
)

class NodeStats private constructor(
    // communicate with p2p
    val txP2P: AtomicLong,
    val rxP2P: AtomicLong,
    // communicate with server
    val txSup: AtomicLong,
    val rxSup: AtomicLong,
    // communicate through broadcast
    val txBroadcast: AtomicLong,
    val rxBroadcast: AtomicLong,

    // last time communicate with server
    val lastSup: AtomicLong,
    // last time communicate with the edge
    val lastP2P: AtomicLong,
) {
    constructor(): this(
        AtomicLong(0),
        AtomicLong(0),
        AtomicLong(0),
        AtomicLong(0),
        AtomicLong(0),
        AtomicLong(0),
        AtomicLong(0),
        AtomicLong(0),
    )
}

class NatProbeCookie {
    val cookieid: AtomicInteger = AtomicInteger(1)
    val cookiemap = ConcurrentHashMap<Int, SendChannel<SDLStunProbeReply>>()

    fun getChannelWithID(id: Int): SendChannel<SDLStunProbeReply>? {
        return this.cookiemap.get(id)
    }

    suspend fun sendMessage(id: Int, value: SDLStunProbeReply) {
        cookiemap.remove(id)?.send(value)
    }

    fun addChannel(channel: SendChannel<SDLStunProbeReply>): Int {
        val ck = cookieid.getAndAdd(1)
        cookiemap.set(ck, channel)
        return ck
    }

    fun remove(id: Int) {
        cookiemap.remove(id)
    }
}

class Node private constructor (
    // 当前的packet id
    val packetID: AtomicInteger,
    // 自身所处的network的id
    val networkID: AtomicInteger,
    // 所处网络的token
    var token: String,

    // tun设备（vpnservice）的配置
    val deviceConfig: DeviceConfig,
    // interface的抽象
    var iface: Iface?,

    // val authorized: AtomicBoolean,

    // rsa info
    val rsa: RSA,
    // aes info
    val aes: AES,
    // var encryptKey: ByteArray,

    val config: NodeConfig,

    val pendingPeers: PeerMap,
    val knownPeers: PeerMap,

    // 自身的公网ip
    val outerIPV4: AtomicInteger,
    val udpSockV4: SDLanSocket,

    val udpSockMulticast: SDLanSocket?,
    val multicastSock: SDLanSock,

    val stats: NodeStats,

    // send message to socket, if connected, and authorized
    var toSocket: SendChannel<ByteArray>,

    val startStopChannel: SendChannel<StartStopChanInfo>,

    val mac: Mac = generateRandomMAC(),
    var nat_type: NatType = NatType.Invalid,
    val natProbeCookie: NatProbeCookie = NatProbeCookie(),
) {
    companion object {
        private var instance: Node? = null

        @Synchronized
        fun initialize(
            config: NodeConfig,
            v4Sock: SDLanSocket,
            multicastSock: SDLanSocket?,
            token: String,
            rsa: RSA,
            iface: Iface,
            toSocket: SendChannel<ByteArray>,
            startStopChannel: SendChannel<StartStopChanInfo>,
        ) : Node{
            if (instance == null) {
                instance = Node(
                    config,
                    v4Sock,
                    multicastSock,
                    token,
                    rsa,
                    iface,
                    toSocket,
                    startStopChannel,
                )
            }
            return instance!!
        }

        fun getInstance(): Node {
            return instance ?: throw IllegalStateException("Node is not initialized")
        }
    }

    private constructor(
        // pubkey: String,
        config: NodeConfig,
        v4Sock: SDLanSocket,
        multicastSock: SDLanSocket?,
        token: String,
        rsa: RSA,
        iface: Iface,
        toSocket: SendChannel<ByteArray>,
        startStopChannel: SendChannel<StartStopChanInfo>,
        // privateKey: ByteArray,
        // mtu: Int
    ): this (
        packetID = AtomicInteger(1),
        networkID = AtomicInteger(0),
        token = token,
        deviceConfig = DeviceConfig(config.mtu),
        iface = iface,
        rsa = rsa,
        aes = AES.getAES(),
        config = config,
        pendingPeers = PeerMap(),
        knownPeers = PeerMap(),

        outerIPV4 = AtomicInteger(0),
        udpSockV4 = v4Sock,
        udpSockMulticast = multicastSock,

        toSocket = toSocket,
        startStopChannel = startStopChannel,

        multicastSock = SDLanSock(
            IPFamily.IPV4,
            SDLanMulticastConfig.MULTICAST_PORT,
            SDLanMulticastConfig.MULTICAST_V4,
        ),

        stats = NodeStats(),
    ) {
        this.deviceConfig.mac = this.mac
    }

    fun getNextPacketID(): Int {
        return packetID.getAndAdd(1)
    }

    suspend fun sendStunRequest() {
        val req = SDLStunRequest.newBuilder()
            .setCookie(0)
            .setClientId(UniqueNodeID.getUUID())
            .setNetworkId(this.networkID.get())
            .setIp(this.deviceConfig.ip.netAddr)
            .setMac(this.mac)
            .setNatType(this.nat_type.toByte().toInt())
            .setV6Info(PunchProto.SDLV6Info.getDefaultInstance())
            .build()

        val msg = encodeToUDPMessage(req, PacketType.StunRequest)
        sendToSock(this, msg, config.supernode)
    }

    suspend fun probeNatType() {
        val reply1 = this._sendAndWaitForProbeReply(StunProbeAttr.None, config.natServer1)
        if (reply1 == null) {
            nat_type = NatType.Blocked
            return
        }
        if (reply1.ip == (outerIPV4.get())) {
           if (this._sendAndWaitForProbeReply(StunProbeAttr.Peer, config.natServer1) == null) {
               // failed to get with peer, just symmetric
               nat_type = NatType.Symmetric
               return
           }
            nat_type = NatType.NoNat
            return
        }

        val reply2 = this._sendAndWaitForProbeReply(StunProbeAttr.Peer, config.natServer1)
        if (reply2 != null) {
            nat_type = NatType.FullCone
            return
        }

        val reply3 = this._sendAndWaitForProbeReply(StunProbeAttr.None, config.natServer2)
        if (reply3 == null) {
            nat_type = NatType.Blocked
            return
        }

        if ((reply1.ip != reply3.ip) || (reply1.port != reply3.port)) {
            nat_type = NatType.Symmetric
            return
        }

        val reply4 = this._sendAndWaitForProbeReply(StunProbeAttr.Port, config.natServer1)
        if (reply4 == null) {
            nat_type = NatType.PortRestricted
        } else {
            nat_type = NatType.ConeRestrict
        }

    }

    suspend fun _sendAndWaitForProbeReply(attr: StunProbeAttr, toServer: SocketAddress): SDLStunProbeReply? {
        val channel = Channel<SDLStunProbeReply>(100)
        val cookie = natProbeCookie.addChannel(channel)
        val probe = SDLStunProbe.newBuilder()
            .setAttr(attr.ordinal.toInt())
            .setCookie(cookie)
            .build()

        val msg = encodeToUDPMessage(probe, PacketType.StunProbe)
        this.udpSockV4.send_to(msg.toByteArray(), toServer)

        val k = withTimeoutOrNull(5000) {

            val response = channel.receive()
            return@withTimeoutOrNull response
        }
        natProbeCookie.remove(cookie)
        return k
    }

    suspend fun ping_to_sn() {
        val msg = encodeToTcpMessage(null, 0, PacketType.Ping).toByteArray()
        _sendDataToSocket(msg)
    }

    suspend fun _sendDataToSocket(msg: ByteArray) {

        if (aes.isAuthorized()) {
            TerminalLogger.debugf{"authorized, ping to sn"}
            toSocket.send(msg)
        } else {
            TerminalLogger.debugf{"unauthorized, not ping to sn"}
        }
    }
}
