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

import UIKit
import ESTabBarController_swift
import IQKeyboardManagerSwift
import SmartCodable

// 阿里云一键登录密钥
let AliCloudLoginSecret = "MuKKbWPm4DRHmZpgASqjI3L1HtiOlxyKWmocGnJhbiibdfaKlwYUj8ISgOexPHLAUAhdo6NNIGOi8oCAeqA1PO9b4ea3YwHXKnIa2u7BCTkd5L4IswsGgljfBeGIyRhoFCnCibix7RwQEqhrkdZ2KMsE/NtPrXfi+EU3oAnO9tXIztzvpAe75Pna2mUKMvO1qt4SSmPe4VnEOX5LCdbn7sGAvOLN8/l/fcCdEU7FA+AGLTY+547jlwan3wyJYJn0Sc8qnEW9LBxRwTDpEZPqyg=="

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //1.设置主窗口
        setupRootVC()
        
        
        //2.通用UI设置
        setupWholeUI()
        
        //3.其他第三方sdk设置
        thirdSetting()
        return true
    }
}


extension AppDelegate {
    
    //
    private func thirdSetting() -> Void {
        DispatchQueue.global().async {
            
            //1.数据解析
#if DEBUG
            SmartConfig.debugMode = .verbose
#else
            SmartConfig.debugMode = .none
#endif
            
            
            //2.bugly参数设置
#if DEBUG
            Bugly.start(withAppId: "3b10faa43e")
#else
            Bugly.start(withAppId: "5dc2acc53e")
#endif
            
            //3.一键登录
            var handler = TXCommonHandler.sharedInstance()
            handler.setAuthSDKInfo(AliCloudLoginSecret) { [weak self] dic in
                
                guard let self = self else { return }
                print("Ali Cloud login completed : /(dic） " )
                
            }
        }
    }
    
    //2.通用全局UI设置相关
    private func setupWholeUI() -> Void {
        
        DispatchQueue.global().async {
            //异步延迟设置
            DispatchQueue.main.asyncAfter(wallDeadline: .now() + 0.25) { [self] in
                //1、系统控件属性设置
                //1.1
                UITextField.appearance().tintColor = .mainColor
                UITextView.appearance().tintColor = .mainColor
                
                //1.2设置UINavigationBar title的字体和颜色
                let titleTextAttributes = [NSAttributedString.Key.font : UIFont.PFSCM(ofSize: 16) , NSAttributedString.Key.foregroundColor : UIColor.commonColor100]
                UINavigationBar.appearance().titleTextAttributes = titleTextAttributes
                UINavigationBar.appearance().tintColor = .white
                
                //1.3UITabBar bar 的选中颜色
                UITabBar.appearance().tintColor = UIColor.commonColor100
                //1.4tabbar默认的背景色
                UITabBar.appearance().barTintColor = UIColor.white
                
                
                //2、一键登录初始化
                //                TXCommonHandler.sharedInstance().setAuthSDKInfo(BsConstant.kAliOnePressPhoneLoginKey)
                
                //3.微信登录初始化
                //                BsWxLoginManager.shared.doWxInit()
                
                //4.
                IQKeyboardManager.shared.enable = true
                IQKeyboardManager.shared.enableAutoToolbar = false
                IQKeyboardManager.shared.shouldResignOnTouchOutside = true // 控制点击背景是否收起键盘
                
                //webView 预加载
                //                BsWebViewPreloadManager.share.addPreloadingView()
                
                //
                customAppearance()
            }
        }
        
        YHNetworkStatusManager.shared.listenNetWorkStatus()
        
        
        
        
    }
    
    //1.设置主窗口
    private func setupRootVC() {
        let tabBarController = YHTabBarViewController()
        let v0 = YHNavigationController(rootViewController: YHHomeViewController())
        let v1 = YHNavigationController(rootViewController: YHIntelligentServiceViewController())
        let v2 = YHNavigationController(rootViewController: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()
        window?.backgroundColor = .white
    }
}


