import Foundation
import XMPPSwift

// External "middleware" for interaction with XMPP lib
extension AppStore {
    func processXmppEvent(_ event: XMPPService.Event) {
        switch event.eventType {
        case .clientInitialised:
            xmpp.doAction(.init(event.jid, .connectToServer))

        case .serverConnectionEstablished:
            dispatch(.registrationAction(.serverStateChanged(.connected)))

        case .serviceError(let err):
            switch err {
            case .serverConnectionFailed:
                dispatch(.registrationAction(.serverStateChanged(.error)))

            default:
                break
            }

        case .clientAuthResult(let done):
            if done {
                dispatch(.registrationAction(.clientStateChanged(.loggedIn)))
            } else {
                dispatch(.registrationAction(.clientStateChanged(.loginError)))
            }
        }
    }
}
