//
//  AppDelegate.swift
//  QuickSearchLocation
//
//  Created by mac on 2024/4/10.
//

import UIKit
import AMapFoundationKit
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        Thread.sleep(forTimeInterval: 1)
        IQKeyboardManager.shared.enable = true
        AMapServices.shared().apiKey = QSLConfig.MapKey
        //启动埋点
        managerAppOnlyLaunch()
        return true
    }

    
    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }

    //管理app启动埋点
    func managerAppOnlyLaunch() {
        
        guard UserDefaults.standard.string(forKey: "MANAGER_APP_ONLY_LAUNCH") != nil else {
            UserDefaults.standard.set("YES", forKey: "MANAGER_APP_ONLY_LAUNCH")
            UserDefaults.standard.synchronize()
            //只有app第一次启动才会埋点
            gravityInstance?.track(QSLGravityConst.app_launch, properties: ["id": 02001])
            return
        }
    }
    
    // 当 App 即将终止（退出）时调用
    func applicationWillTerminate(_ application: UIApplication) {
        print("App 即将退出")
        // 仅在 App 被用户主动退出或系统正常终止时触发（强制杀死进程时可能不调用）
        // 可在此做最后的数据保存（操作需快速完成，系统可能限制时间）
        ///埋点app退出
        gravityInstance?.track(QSLGravityConst.app_exit, properties: ["id": 02002])
    }
}

