//
//  GoogleInterstistialADSetup.swift
//  WOKA
//
//  Created by MacBook Pro on 23/09/24.
//

import GoogleMobileAds
import Alamofire

class GoogleInterstistialADSetup{
    
    static let shareInstance = GoogleInterstistialADSetup()
    let reachability = NetworkReachabilityManager()

    private init(){}
    var interstitial: GADInterstitialAd?

    func setupGoogleIntersitialAD(){
        GADInterstitialAd.load(withAdUnitID: K.GoogleAdIDs.gamesDetailsInterStial, request: GADRequest()) { [weak self] ad, error in
            if let error = error {
                print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                
                // If internet is reachable, try to load the ad again
                if self?.reachability?.isReachable == true {
                    self?.setupGoogleIntersitialAD()
                }
                return
            }

            // Successfully loaded the interstitial ad
            self?.interstitial = ad
            print("Interstitial ad loaded successfully.")
        }
    }
}
