import XCTest
@testable import CarePulseConnect

/// Same vectors as the Android GattParsersTest.kt so both apps decode devices identically.
final class GattParsersTests: XCTestCase {
    private func ts(_ y: Int, _ mo: Int, _ d: Int, _ h: Int, _ mi: Int) -> Date {
        var c = DateComponents(); c.year = y; c.month = mo; c.day = d; c.hour = h; c.minute = mi; c.second = 0
        var cal = Calendar(identifier: .gregorian); cal.timeZone = .current; return cal.date(from: c)!
    }

    func testSfloat() {
        XCTAssertEqual(GattParsers.sfloat([0x78, 0x00], 0)!, 120, accuracy: 0.0001)
        XCTAssertEqual(GattParsers.sfloat([0xFF, 0x0F], 0)!, -1, accuracy: 0.0001)
        XCTAssertEqual(GattParsers.sfloat([0x6E, 0xB0], 0)!, 0.0011, accuracy: 1e-9)
        XCTAssertNil(GattParsers.sfloat([0xFF, 0x07], 0))
        XCTAssertNil(GattParsers.sfloat([0x00, 0x08], 0))
    }

    func testBloodPressureWithTimeAndPulse() {
        let now = ts(2026, 11, 17, 10, 35)
        let r = Dictionary(uniqueKeysWithValues: GattParsers.bloodPressure([0x06, 0x78, 0x00, 0x50, 0x00, 0x5D, 0x00, 0xEA, 0x07, 0x0B, 0x11, 0x0A, 0x1E, 0x00, 0x48, 0x00], now: now).map { ($0.param, $0) })
        XCTAssertEqual(r["bp_sys"]?.value, 120); XCTAssertEqual(r["bp_dia"]?.value, 80); XCTAssertEqual(r["bp_map"]?.value, 93); XCTAssertEqual(r["pulse"]?.value, 72)
        XCTAssertEqual(r["bp_sys"]?.unit, "mmHg"); XCTAssertEqual(r["bp_sys"]?.measuredAt, ts(2026, 11, 17, 10, 30))
    }

    func testWrongDeviceClockUsesPhoneTime() {
        let now = ts(2026, 11, 17, 10, 35)
        XCTAssertEqual(GattParsers.bloodPressure([0x02, 0x78, 0x00, 0x50, 0x00, 0x5D, 0x00, 0xD0, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00], now: now)[0].measuredAt, now)
    }

    func testTemperature() {
        let c = GattParsers.temperature([0x00, 0x6F, 0x01, 0x00, 0xFF])[0]
        XCTAssertEqual(c.value, 36.7, accuracy: 0.0001); XCTAssertEqual(c.unit, "Cel")
        let f = GattParsers.temperature([0x01, 0xDA, 0x03, 0x00, 0xFF])[0]
        XCTAssertEqual(f.value, 98.6, accuracy: 0.0001); XCTAssertEqual(f.unit, "[degF]")
    }

    func testGlucose() {
        let r = GattParsers.glucose([0x02, 0x05, 0x00, 0xEA, 0x07, 0x0B, 0x11, 0x08, 0x00, 0x00, 0x6E, 0xB0, 0x11], now: ts(2026, 11, 17, 9, 0))[0]
        XCTAssertEqual(r.value, 110, accuracy: 0.01); XCTAssertEqual(r.unit, "mg/dL"); XCTAssertEqual(r.sequence, 5); XCTAssertEqual(r.measuredAt, ts(2026, 11, 17, 8, 0))
    }

    func testRacp() {
        XCTAssertEqual(GattParsers.racpReport(nil), [0x01, 0x01])
        XCTAssertEqual(GattParsers.racpReport(300), [0x01, 0x03, 0x01, 0x2C, 0x01])
    }

    func testOximeterWeightHeartRate() {
        let o = Dictionary(uniqueKeysWithValues: GattParsers.plxSpot([0x00, 0x61, 0x00, 0x48, 0x00]).map { ($0.param, $0.value) })
        XCTAssertEqual(o["spo2"], 97); XCTAssertEqual(o["pulse"], 72)
        XCTAssertEqual(GattParsers.weight([0x00, 0x14, 0x37])[0].value, 70.5, accuracy: 0.001)
        XCTAssertTrue(GattParsers.weight([0x00, 0xFF, 0xFF]).isEmpty)
        XCTAssertEqual(GattParsers.heartRate([0x00, 0x48])[0].value, 72)
        XCTAssertEqual(GattParsers.heartRate([0x01, 0x04, 0x01])[0].value, 260)
    }

    func testMalformedPacketIgnoredNotCrashing() {
        XCTAssertTrue(GattParsers.parse(GattParsers.chBP, [0x06, 0x78]).isEmpty)
        XCTAssertTrue(GattParsers.parse(GattParsers.chGlucose, [0x03, 0x01]).isEmpty)
        XCTAssertTrue(GattParsers.parse(GattParsers.chPlxSpot, [0x01, 0x61, 0x00, 0x48, 0x00]).count == 2)
    }
}
