//
//  AppDelegate.swift
//  galaxy
//
//  Created by davidhuangA on 2023/12/19.
//

import UIKit
import ESTabBarController_swift
import IQKeyboardManagerSwift
import SmartCodable
import GKNavigationBarSwift
import DeviceKit

import NIMSDK
import NECoreKit
import NECoreIMKit
import NEChatKit
import NEChatUIKit
import NEConversationUIKit
import NETeamUIKit
import NEChatUIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        //全局导航栏、返回事件/手势
        setupNavBar()
        
        //1.设置主窗口
        setupRootVC()
        
        
        //2.通用UI设置
        setupWholeUI()
        
        //3.其他第三方sdk设置
        thirdSetting()
        
        //        YHAnalytics.analyticsStart(launchOptions: launchOptions)
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if YHAnalytics.handleSchemeUrl(url) ?? false {
            return true
        }
        return true
    }
    
    
    func setupNavBar() {
        GKConfigure.setupDefault()
        
    }
}


extension AppDelegate {
    
    //
    private func thirdSetting() -> Void {
        DispatchQueue.global().async {
            
            //1.数据解析
#if DEBUG
//            SmartConfig.debugMode = .verbose
            SmartConfig.debugMode = .none //for test hjl
#else
            SmartConfig.debugMode = .none
#endif
            
            
            //2.bugly参数设置
#if DEBUG
            Bugly.start(withAppId: "3b10faa43e")
#else
            Bugly.start(withAppId: "5dc2acc53e")
#endif
            
            
            if !Device.current.isSimulator {
                //3.一键登录
                let handler = TXCommonHandler.sharedInstance()
                handler.setAuthSDKInfo(YhConstant.Alipay.kAliOnePressPhoneLoginKey) { [weak self] dic in
                    
                    guard self != nil else { return }
                    print("Ali Cloud login completed : /(dic） " )
                    
                }
                
            }
            
        }
    }
    
    //2.通用全局UI设置相关
    private func setupWholeUI() -> Void {
        
        DispatchQueue.global().async {
            //异步延迟设置
            DispatchQueue.main.asyncAfter(wallDeadline: .now() + 0.25) { [] in
                //1、系统控件属性设置
                UITextField.appearance().tintColor = .brandMainColor
                UITextView.appearance().tintColor = .brandMainColor
                
                IQKeyboardManager.shared.enable = true
                IQKeyboardManager.shared.enableAutoToolbar = true
                IQKeyboardManager.shared.resignOnTouchOutside = true  //控制点击背景是否收起键盘
            }
        }
        
        YHNetworkStatusManager.shared.listenNetWorkStatus()
    }
    
    //1.设置主窗口
    private func setupRootVC() {
        let tabBarController = YHTabBarViewController()
        let v0 = YHNavigationController(rootVC:YHHomeViewController())
        
//        let v0 = YHNavigationController(rootVC:YHPersonInformMainViewController())//for test hjl
        
        let v1 = YHNavigationController(rootVC:YHServiceCenterMainViewController())
        let v2 = YHNavigationController(rootVC:YHMyViewController())
        
        v0.tabBarItem = ESTabBarItem.init(YHBasicContentView(),title: "首页", image: UIImage(named: "photo"), selectedImage: UIImage(named: "photo_1"))
        v1.tabBarItem = ESTabBarItem.init(YHBasicContentView(),title: "服务中心", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(YHBasicContentView(),title: "我的", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
        tabBarController.viewControllers = [v0,v1,v2]
        tabBarController.tabBar.backgroundColor = .white
        
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = tabBarController
        window?.makeKeyAndVisible()
    }
    
    // 网银云信IM相关配置
    private func configIM() {
        
        let option = NIMSDKOption()
        option.appKey = IMAppKey.appKey
        option.apnsCername = "云信控制台配置的 APNS 推送证书名称"
        option.pkCername = "云信控制台配置的 PushKit 推送证书名称"
        IMKitClient.instance.setupCoreKitIM(option)
        let _ = NEAtMessageManager.instance
    }
}


