//
//  LNConfigManager.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/25.
//

import Foundation


class LNConfigManager {
    static let shared = LNConfigManager()
    private(set) var commonConfig = LNConfigResponse()
    
    var isBeanAvailable: Bool {
        guard let version = commonConfig.appReviewSpecialVersionConfig?.ios else { return false }
        if version.isEmpty { return true }
        
        guard let iVersion = Int(version) else { return false }
        if iVersion == 0 { return true }
        
        guard let curVersion = Int(curBuildVersion) else { return false }
        return curVersion < iVersion
    }
    
    private init() {
        LNEventDeliver.addObserver(self)
    }
    
    func reloadCommonConfig() {
        LNHttpManager.shared.getCommonConfig { [weak self] res, err in
            guard let self else { return }
            guard let res else { return }
            
            commonConfig = res
        }
    }
    
    func getCountryCodeList(queue: DispatchQueue = .main, handler: @escaping ([LNCountryCodeVO]?) -> Void) {
        LNHttpManager.shared.getCountryCodeList { res, err in
            queue.asyncIfNotGlobal {
                handler(res?.list)
            }
            if let err {
                showToast(err.errorDesc)
            }
        }
    }
    
    func getBannerList(adSlot: Int, queue: DispatchQueue = .main, handler: @escaping ([LNBannerInfoVO]?) -> Void) {
        LNHttpManager.shared.getBannerList(adSlot: adSlot) { res, err in
            queue.asyncIfNotGlobal {
                handler(res?.list)
            }
        }
    }
}

extension LNConfigManager: LNAccountManagerNotify {
    func onUserLogin() {
        reloadCommonConfig()
    }
}
