import UIKit
import AppTrackingTransparency

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
    }
    
    func sceneDidDisconnect(_ scene: UIScene) {}
    
    func sceneDidBecomeActive(_ scene: UIScene) {
        _ = scene
        // ATT 
        self.authorizationIDFA()
    }
    
    func sceneWillResignActive(_ scene: UIScene) {}
    
    func sceneWillEnterForeground(_ scene: UIScene) {}
    
    func sceneDidEnterBackground(_ scene: UIScene) {}
    
    @objc func authorizationIDFA() {
        let application = UIApplication.shared
        if application.applicationState == .active {
            if #available(iOS 14, *) {
                let status = ATTrackingManager.trackingAuthorizationStatus
                if status == .notDetermined {
                    ATTrackingManager.requestTrackingAuthorization { status in
                        // Tracking authorization completed.
                        _ = status
                    }
                }
            }
        } else {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
                self?.authorizationIDFA()
            }
        }
    }
}
