import SwiftUI
import GoogleMobileAds
import UIKit

enum GADSlot: String, CaseIterable {
    case articles = "/98851964/RA-Right-Column-Middle"
    case articlesAlt1 = "/98851964/RA-Right-Column-Top"
    case articlesAlt2 = "/98851964/RA-Right-Column-Bottom"
    case articlesAlt3 = "/98851964/RA-Footer-Responsive"
    case articlesAlt4 = "/98851964/RA_sidebar_top_300x250"
    case articlesAlt5 = "/98851964/RA_sidebar_middle_300x250"
    case articlesAlt6 = "/98851964/RA_sidebar_middle_2_300x250"
    case articlesAlt7 = "/98851964/RA_sidebar_bottom_300x250"
    case articleDetail = "/98851964/Inside_article_1"
    case events = "/98851964/Inside_article_2"
    
    var name: String {
        self.rawValue
    }
    
    static var articleListPool: [GADSlot] {
        [.articles,.articlesAlt1,.articlesAlt2,.articlesAlt3,.articlesAlt4,.articlesAlt5,.articlesAlt6,.articlesAlt7]
    }
    
    var size: GADSize {
        switch self {
        default:
            return .mediumRectangle
        }
    }
}

enum GADSize {
    case banner
    case largeBanner
    case fullBanner
    case mediumRectangle
    case leaderboard
    
    var legacy: GADAdSize {
        switch self {
        case .banner:
            return GADAdSizeBanner
        case .largeBanner:
            return GADAdSizeLargeBanner
        case .fullBanner:
            return GADAdSizeFullBanner
        case .mediumRectangle:
            return GADAdSizeMediumRectangle
        case .leaderboard:
            return GADAdSizeLeaderboard
        }
    }
}

struct GADViewWrapper: UIViewControllerRepresentable {
    
    var adSlot: GADSlot
    
    @State private var banner: GADBannerView = GADBannerView(adSize: GADAdSizeBanner)
    
    func makeUIViewController(context: Context) -> UIViewController {
        let viewController = UIViewController()
        banner.adSize = adSlot.size.legacy
        banner.adUnitID = adSlot.name
        banner.rootViewController = viewController
        viewController.view.addSubview(banner)
        viewController.view.frame = CGRect(origin: .zero, size: adSlot.size.legacy.size)
        banner.load(GADRequest())
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context){
        banner.frame = CGRect(origin: .zero, size: adSlot.size.legacy.size)
        banner.load(GADRequest())
    }
    
}
