import XCTest
@testable import Rosetta

final class CallPacketParityTests: XCTestCase {
    func testSignalPeerRoundTripForCallKeyExchangeAndCreateRoom() throws {
        let call = PacketSignalPeer(
            src: "02caller",
            dst: "02callee",
            sharedPublic: "",
            signalType: .call,
            roomId: ""
        )
        let keyExchange = PacketSignalPeer(
            src: "02callee",
            dst: "02caller",
            sharedPublic: "abcdef012345",
            signalType: .keyExchange,
            roomId: ""
        )
        let createRoom = PacketSignalPeer(
            src: "02caller",
            dst: "02callee",
            sharedPublic: "",
            signalType: .createRoom,
            roomId: "room-42"
        )

        let decodedCall = try decodeSignal(call)
        XCTAssertEqual(decodedCall.signalType, .call)
        XCTAssertEqual(decodedCall.src, "02caller")
        XCTAssertEqual(decodedCall.dst, "02callee")
        XCTAssertEqual(decodedCall.sharedPublic, "")
        XCTAssertEqual(decodedCall.roomId, "")

        let decodedKeyExchange = try decodeSignal(keyExchange)
        XCTAssertEqual(decodedKeyExchange.signalType, .keyExchange)
        XCTAssertEqual(decodedKeyExchange.src, "02callee")
        XCTAssertEqual(decodedKeyExchange.dst, "02caller")
        XCTAssertEqual(decodedKeyExchange.sharedPublic, "abcdef012345")
        XCTAssertEqual(decodedKeyExchange.roomId, "")

        let decodedCreateRoom = try decodeSignal(createRoom)
        XCTAssertEqual(decodedCreateRoom.signalType, .createRoom)
        XCTAssertEqual(decodedCreateRoom.src, "02caller")
        XCTAssertEqual(decodedCreateRoom.dst, "02callee")
        XCTAssertEqual(decodedCreateRoom.sharedPublic, "")
        XCTAssertEqual(decodedCreateRoom.roomId, "room-42")
    }

    func testSignalPeerRoundTripForBusyAndPeerDisconnectedShortFormat() throws {
        let busy = PacketSignalPeer(
            src: "02should-not-be-sent",
            dst: "02should-not-be-sent",
            sharedPublic: "ignored",
            signalType: .endCallBecauseBusy,
            roomId: "ignored-room"
        )
        let disconnected = PacketSignalPeer(
            src: "02should-not-be-sent",
            dst: "02should-not-be-sent",
            sharedPublic: "ignored",
            signalType: .endCallBecausePeerDisconnected,
            roomId: "ignored-room"
        )

        let decodedBusy = try decodeSignal(busy)
        XCTAssertEqual(decodedBusy.signalType, .endCallBecauseBusy)
        XCTAssertEqual(decodedBusy.src, "")
        XCTAssertEqual(decodedBusy.dst, "")
        XCTAssertEqual(decodedBusy.sharedPublic, "")
        XCTAssertEqual(decodedBusy.roomId, "")

        let decodedDisconnected = try decodeSignal(disconnected)
        XCTAssertEqual(decodedDisconnected.signalType, .endCallBecausePeerDisconnected)
        XCTAssertEqual(decodedDisconnected.src, "")
        XCTAssertEqual(decodedDisconnected.dst, "")
        XCTAssertEqual(decodedDisconnected.sharedPublic, "")
        XCTAssertEqual(decodedDisconnected.roomId, "")
    }

    func testWebRtcRoundTripForOfferAnswerAndIceCandidate() throws {
        let offer = PacketWebRTC(signalType: .offer, sdpOrCandidate: #"{"type":"offer","sdp":"v=0"}"#)
        let answer = PacketWebRTC(signalType: .answer, sdpOrCandidate: #"{"type":"answer","sdp":"v=0"}"#)
        let candidate = PacketWebRTC(
            signalType: .iceCandidate,
            sdpOrCandidate: #"{"candidate":"candidate:1 1 udp 2130706431 10.0.0.1 7777 typ host"}"#
        )

        let decodedOffer = try decodeWebRtc(offer)
        XCTAssertEqual(decodedOffer.signalType, .offer)
        XCTAssertEqual(decodedOffer.sdpOrCandidate, offer.sdpOrCandidate)

        let decodedAnswer = try decodeWebRtc(answer)
        XCTAssertEqual(decodedAnswer.signalType, .answer)
        XCTAssertEqual(decodedAnswer.sdpOrCandidate, answer.sdpOrCandidate)

        let decodedCandidate = try decodeWebRtc(candidate)
        XCTAssertEqual(decodedCandidate.signalType, .iceCandidate)
        XCTAssertEqual(decodedCandidate.sdpOrCandidate, candidate.sdpOrCandidate)
    }

    func testIceServersRoundTrip() throws {
        let packet = PacketIceServers(
            iceServers: [
                CallIceServer(
                    url: "turn:turn.rosetta.im?transport=udp",
                    username: "u1",
                    credential: "p1",
                    transport: "udp"
                ),
                CallIceServer(
                    url: "stun:stun.l.google.com:19302",
                    username: "",
                    credential: "",
                    transport: ""
                ),
            ]
        )

        let encoded = PacketRegistry.encode(packet)
        guard let decoded = PacketRegistry.decode(from: encoded),
              let decodedPacket = decoded.packet as? PacketIceServers
        else {
            XCTFail("Failed to decode PacketIceServers")
            return
        }

        XCTAssertEqual(decoded.packetId, 0x1C)
        XCTAssertEqual(decodedPacket.iceServers, packet.iceServers)
    }

    private func decodeSignal(_ packet: PacketSignalPeer) throws -> PacketSignalPeer {
        let encoded = PacketRegistry.encode(packet)
        guard let decoded = PacketRegistry.decode(from: encoded),
              let decodedPacket = decoded.packet as? PacketSignalPeer
        else {
            throw NSError(domain: "CallPacketParityTests", code: 1)
        }
        XCTAssertEqual(decoded.packetId, 0x1A)
        return decodedPacket
    }

    private func decodeWebRtc(_ packet: PacketWebRTC) throws -> PacketWebRTC {
        let encoded = PacketRegistry.encode(packet)
        guard let decoded = PacketRegistry.decode(from: encoded),
              let decodedPacket = decoded.packet as? PacketWebRTC
        else {
            throw NSError(domain: "CallPacketParityTests", code: 2)
        }
        XCTAssertEqual(decoded.packetId, 0x1B)
        return decodedPacket
    }
}
