//
//  AppDelegate.swift
//  Introduce
//
//  Created by 胡志强 on 2021/11/29.
//

import BFCommonKit
import BFMaterialKit
import UIKit
import AppTrackingTransparency

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var isEnterBack: Bool = false // 进入后台
    var notificationUserInfo: [String: Any]?
    
    func application(_: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 初始化配置
        INAppConfigUtil.pageConfig()
        if launchOptions?.keys.contains(UIApplication.LaunchOptionsKey.remoteNotification) ?? false {
            notificationUserInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any]
            if notificationUserInfo != nil && notificationUserInfo?.keys.contains("type") ?? false {
                PQSingletoMemoryUtil.shared.coldLaunchType = .coldLaunchType_pushRecall
            }
        }
        // 配置启动项
        INAppConfigUtil.applicationConfig(launchOptions:launchOptions)
//        BFMaterialConfig.shared.choseType = .single
        return true
    }

    // MARK: 前后台 Lifecycle

    func applicationWillResignActive(_: UIApplication) {}

    func applicationDidEnterBackground(_: UIApplication) {
        // 热启动重新生成SubSesstionId
        isEnterBack = true
        PQSingletoMemoryUtil.shared.createSubSesstionId()
        BFConfig.shared.subSessionId = PQSingletoMemoryUtil.shared.subSessionid
    }

    func application(_: UIApplication, supportedInterfaceOrientationsFor _: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    

    func applicationDidBecomeActive(_: UIApplication) {
        BFLog(message: "applicationDidBecomeActive")
        if isEnterBack {
            // 热启动
            PQSingletoMemoryUtil.shared.isColdLaunch = false
            // 热启动上报
            INAppConfigUtil.appLaunchReportUpload(isHotLaunch: true)
            isEnterBack = false
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            if #available(iOS 14, *) {
                ATTrackingManager.requestTrackingAuthorization { status in
                    
                }
            }
        }
    }
}
