//
//  LNRoomSystemMessageItem.swift
//  Gami
//
//  Created by OneeChan on 2026/3/20.
//

import Foundation
import AtomicXCore
import AutoCodable


enum LNRoomSystemMessageType: String {
    case MicClear = "mic.clear"
}


@AutoCodable
class LNRoomMicClearMessage: Decodable {
    var type: LNApplyingSeatType = .none
    var time: Int64 = 0
}


class LNRoomSystemMessageItem {
    private let message: Barrage
    private(set) var cmd: LNRoomSystemMessageType = .MicClear
    private var cmdMessage: (any Decodable)?
    
    init?(info: Barrage) {
        guard info.messageType == .custom,
              info.businessID == LNRoomSystemMessageType.MicClear.rawValue
        else { return nil }
        message = info
    }
    
    func decodeCmdMessage<T: Decodable>() -> T? {
        if let cached = cmdMessage as? T {
            return cached
        }
        
        guard let data = message.data.data(using: .utf8) else { return nil }
        let decoder = JSONDecoder()
        decoder.keyDecodingStrategy = .useDefaultKeys
        let result = try? decoder.decode(T.self, from: data)
        if let result {
            cmdMessage = result
        }
        return result
    }
}
