import UIKit
import CoreLocation
import AppTrackingTransparency

import GoogleMobileAds
import AppLovinSDK

import AdViewSDK
import AdMobAdViewAdapter
import AppLovinMediationAdViewAdapter

@main
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    
    private let locationManager = CLLocationManager()
    
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
       
        // Set account id
        AdView.shared.adviewAccountId = "54649565"
        // Third-party id (deh) module — disabled by default.
//        AdView.shared.enableDEH = true
        try! AdView.initializeSDK(
        ) { status, error in
            if let error = error {
                print("Initialization Error: \(error.localizedDescription)")
            } else {
                print("Initialization Success")
                print("AdView SDK Version: \(AdView.shared.version)")
            }
        }
        
//        AdView.shared.deh.setIdentity(email: "user@example.com", phone: nil)
        
        // Initialize GoogleMobileAds SDK
        MobileAds.shared.start()

        AdViewAdMobUtils.initializeGAD()
        
        // Initialize AppLovin MAX SDK
        let config = ALSdkInitializationConfiguration(
            sdkKey: "86VQKXR4cOA8cTFPI7i1NI33iRltJt09BLceIIF1SKF7UHBKlcPomiv3dgJ46_lwJtzoJhTEtQyNEPe7xGVkZG"
        ) { builder in
            builder.mediationProvider = ALMediationProviderMAX
        }
        
        ALSdk.shared().initialize(with: config) { _ in

        }
        
        setupLocationManager()
        
        return true
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
 
        print("applicationDidBecomeActive")
        
    }

    
    // 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>) {}
    
    // MARK: - Location
  
    
    private func setupLocationManager() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        locationManager.distanceFilter = 100
        
        switch locationManager.authorizationStatus {
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .authorizedWhenInUse, .authorizedAlways:
            locationManager.startUpdatingLocation()
        default:
            break
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse || status == .authorizedAlways {
            locationManager.startUpdatingLocation()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last, CLLocationCoordinate2DIsValid(location.coordinate) else { return }
        let coord = location.coordinate

//        Targeting.shared.setLatitude(coord.latitude, longitude: coord.longitude)
//        Targeting.shared.location = location
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location error: \(error.localizedDescription)")
    }

}



