import Foundation
import TigaseSwift

// swiftlint: disable force_unwrapping
// swiftlint: disable force_cast
open class DBRoomStore: RoomStore {
    public let dispatcher: QueueDispatcher
    private let sessionObject: SessionObject

    public var rooms: [Room] {
        return store.getChats(for: sessionObject.userBareJid!).filter({ $0 is Room }).map({ $0 as! Room })
    }

    private let store = DBChatStore.instance

    public init(sessionObject: SessionObject) {
        self.sessionObject = sessionObject
        self.dispatcher = store.dispatcher
    }

    deinit {
        self.store.unloadChats(for: self.sessionObject.userBareJid!)
    }

    public func room(for jid: BareJID) -> Room? {
        return store.getChat(for: sessionObject.userBareJid!, with: jid) as? Room
    }

    public func createRoom(roomJid: BareJID, nickname: String, password: String?) -> Result<Room, ErrorCondition> {
        switch store.createRoom(for: sessionObject.userBareJid!, context: sessionObject.context, roomJid: roomJid, nickname: nickname, password: password) {
        case .success(let room):
            return .success(room as Room)

        case .failure(let error):
            return .failure(error)
        }
    }

    public func close(room: Room) -> Bool {
        return store.close(for: sessionObject.userBareJid!, chat: room)
    }

    public func initialize() {
        store.loadChats(for: sessionObject.userBareJid!, context: sessionObject.context)
    }

    public func deinitialize() {
        store.unloadChats(for: sessionObject.userBareJid!)
    }
}
