import Foundation
import TigaseSwift

class SiskinPushNotificationsModuleProvider: SiskinPushNotificationsModuleProviderProtocol {
    func mutedChats(for account: BareJID) -> [BareJID] {
        return DBChatStore.instance.getChats(for: account).filter({ (chat) -> Bool in
                if let c = chat as? DBChat {
                    return c.options.notifications == .none
                }
                return false
            })
            .map({ (chat) -> BareJID in
                return chat.jid.bareJid
            })
            .sorted { (j1, j2) -> Bool in
                return j1.stringValue.compare(j2.stringValue) == .orderedAscending
            }
    }

    func groupchatFilterRules(for account: BareJID) -> [TigasePushNotificationsModule.GroupchatFilter.Rule] {
        return DBChatStore.instance.getChats(for: account).filter({ (c) -> Bool in
                switch c {
                case let channel as DBChannel:
                    switch channel.options.notifications {
                    case .none:
                        return false

                    case .always, .mention:
                        return true
                    }

                case let room as DBRoom:
                    switch room.options.notifications {
                    case .none:
                        return false

                    case .always, .mention:
                        return true
                    }

                default:
                    break
                }
                return false
            })
            .sorted(by: { (r1, r2) -> Bool in
                return r1.jid.bareJid.stringValue.compare(r2.jid.bareJid.stringValue) == .orderedAscending
            })
            .map({ (c) -> TigasePushNotificationsModule.GroupchatFilter.Rule in
                switch c {
                case let channel as DBChannel:
                    switch channel.options.notifications {
                    case .none:
                        return .never(room: channel.channelJid)

                    case .always:
                        return .always(room: channel.channelJid)

                    case .mention:
                        return .mentioned(room: channel.channelJid, nickname: channel.nickname ?? "")
                    }

                case let room as DBRoom:
                    switch room.options.notifications {
                    case .none:
                        return .never(room: room.roomJid)

                    case .always:
                        return .always(room: room.roomJid)

                    case .mention:
                        return .mentioned(room: room.roomJid, nickname: room.nickname)
                    }

                default:
                    // should not happen
                    return .never(room: c.account)
                }
            })
    }
}
