//
//  AppLaunchInitial.swift

//
//  Created by Leon on 2025/7/19.
//
import Foundation
//import IQKeyboardManagerSwift
import IQKeyboardToolbarManager
import IQKeyboardManagerSwift
class AppLaunchInitial{
    
    public func setupCommon(){
        // User
        UserCore.shared.autoLoginTry()
        
        setupUIAbout()
        
        LTNetworkManage.ltManage.netWorkReachability(reachabilityStatus: nil)
        
        IAPCore.shared.check()
        
        IMManager.shared.setupNIM()
        
        H5BaseViewController.clearCache()
        
        loadApis(excludeNoNeedLogin: false)
        
        setupEvent()
    }
    
    /// excludeNoNeedLogin, 排除掉不需要登陆的。 一般使用false
    private func loadApis(excludeNoNeedLogin : Bool){
        // Req Dict Data
        if excludeNoNeedLogin == false{
            AppDictManager.shared.loadRequiredDict()
        }
        
        WalletCore.shared.refreshWallet(block: nil)
        IMManager.shared.regetNoticeUnread()
    }
    
    private func setupEvent(){
        NotificationCenter.default.addObserver(self, selector: #selector(notifyNetworkRestore), name: AppNotificationName.networkRestored.notificationName, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(loginIn), name: AppNotificationName.userLoginSuccess.notificationName, object: nil)
    }
    
    
    // MARK: - Functions
    private func setupUIAbout(){
        setupIQKeyboard()
        
        CLPopoverListView.setupCLStyle()
        
        UICommonSetup.setupCLStyle()
    }
    
    // MARK: Helper
    private func setupIQKeyboard(){
        DispatchQueue.main.async {
            IQKeyboardToolbarManager.shared.isEnabled = true
            IQKeyboardToolbarManager.shared.toolbarConfiguration.tintColor = .c.cpn
            IQKeyboardManager.shared.isEnabled = true
            
            IQKeyboardManager.shared.disabledDistanceHandlingClasses = [SessionController.self]
        }
    }
    
    // MARK: - Notification
    
    @objc private func notifyNetworkRestore(){
        dlog("🛜网络恢复")
        loadApis(excludeNoNeedLogin: false)
    }
    
    @objc private func loginIn(){
        loadApis(excludeNoNeedLogin: true)
    }
    
    
    
    
}
