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

import BFCommonKit
import BFMaterialKit
import UIKit
import AppTrackingTransparency
import AdSupport


@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 {
        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
            }
        }
        // 配置启动项
        DispatchQueue.global().async {
            // 初始化配置
            INAppConfigUtil.pageConfig()
        #if DEBUG
        #else
            INAppConfigUtil.applicationConfig(launchOptions:launchOptions)
        #endif

        }
//        BFMaterialConfig.shared.choseType = .single
        return true
    }

    // MARK: 前后台 Lifecycle

    func applicationWillResignActive(_: UIApplication) {}

    func applicationDidEnterBackground(_: UIApplication) {
        // 热启动重新生成SubSesstionId
        isEnterBack = true
        DispatchQueue.global().async {
            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
        }
        UIApplication.shared.keyWindow?.isUserInteractionEnabled = true
        
        requestPromission()
    }
    
    func requestPromission(){
        if #available(iOS 14.0, *) {
            let status = ATTrackingManager.trackingAuthorizationStatus
            switch status {
                case .denied:
                    print("用户拒绝IDFA")
                case .authorized:
                    print("用户允许IDFA")
                case .notDetermined:
                    print("用户未做选择或未弹窗IDFA")
                    ATTrackingManager.requestTrackingAuthorization { _ in
                    }
                default:
                    break
            }
        } else {
            if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
                print("用户开启了广告追踪IDFA")
            } else {
                print("用户关闭了广告追踪IDFA")
            }
        }
    }
}
