import Foundation
import TigaseSwift

open class DNSSrvDiskCache: DNSSrvResolverWithCache.DiskCache {
    public override init(cacheDirectoryName: String) {
        super.init(cacheDirectoryName: cacheDirectoryName)
        NotificationCenter.default.addObserver(self, selector: #selector(accountChanged(_:)), name: AccountManager.ACCOUNT_CHANGED, object: nil)
    }

    @objc fileprivate func accountChanged(_ notification: Notification) {
        guard let account = notification.object as? AccountManager.Account else {
            return
        }
        guard !(AccountManager.getAccount(for: account.name)?.active ?? false) else {
            return
        }

        self.store(for: account.name.domain, result: nil)
    }
}
