import UIKit
import Capacitor
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        excludeWebViewDataFromBackup()
        return true
    }

    /// iOS counterpart of android:allowBackup="false": the WKWebView keeps the
    /// Django session cookie (Library/Cookies) and the native CSRF token in
    /// localStorage (Library/WebKit), which iCloud/Finder backups would
    /// otherwise restore onto another device, transplanting an active session.
    /// Re-applied on every launch and on entering background: at first launch
    /// these directories do not exist yet (the WKWebView is created after
    /// didFinishLaunching), and WebKit can later recreate them without the
    /// exclusion attribute.
    private func excludeWebViewDataFromBackup() {
        let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
        for directory in ["Cookies", "WebKit"] {
            var url = library.appendingPathComponent(directory, isDirectory: true)
            guard FileManager.default.fileExists(atPath: url.path) else { continue }
            var values = URLResourceValues()
            values.isExcludedFromBackup = true
            try? url.setResourceValues(values)
        }
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        // Re-apply here so the WebKit/Cookies directories created during the
        // first session are excluded before any backup runs (backups typically
        // trigger while the app is backgrounded).
        excludeWebViewDataFromBackup()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // The push badge carries the unread count as of send time; once the
        // user is in the app it is stale, so drop it (web counterpart:
        // clearAppBadge in features/auth). Delivered banners are dismissed by
        // the JS side (clearDeliveredNativeNotifications).
        if #available(iOS 16.0, *) {
            UNUserNotificationCenter.current().setBadgeCount(0)
        } else {
            application.applicationIconBadgeNumber = 0
        }
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        // Called when the app was launched with a url. Feel free to add additional processing here,
        // but if you want the App API to support tracking app url opens, make sure to keep this call
        return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        // Called when the app was launched with an activity, including Universal Links.
        // Feel free to add additional processing here, but if you want the App API to support
        // tracking app url opens, make sure to keep this call
        return ApplicationDelegateProxy.shared.application(application, continue: userActivity, restorationHandler: restorationHandler)
    }

    // Hand the APNs registration outcome to @capacitor/push-notifications:
    // its native side observes these notifications and resolves the JS
    // "registration"/"registrationError" events the app awaits
    // (features/native/push.ts, obtainToken).
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
    }

}
