import UIKit

fileprivate let bannerBaseViewControllerNibName = "BannerBaseViewController"

/// Leaderboard (728×90) on iPad, mobile banner (320×50) on iPhone / other idioms.
private func defaultBannerAdSize() -> CGSize {
    if UIDevice.current.userInterfaceIdiom == .pad {
        return CGSize(width: 728, height: 90)
    }
    return CGSize(width: 320, height: 50)
}

/// Base controller, which provides banner controls, i.e. banner view
class BannerBaseViewController: UIViewController {
    
    @IBOutlet weak var bannerView: UIView!
    
    // Integration case ad size; default follows device (728×90 iPad, 320×50 phone).
    // Override via `init(adSize:)` or assign before `viewDidLoad` when a case needs a fixed size.
    var adSize = defaultBannerAdSize()
    
    convenience init(adSize: CGSize) {
        self.init(nibName: bannerBaseViewControllerNibName, bundle: nil)
        self.adSize = adSize
    }
    
    convenience init() {
        self.init(nibName: bannerBaseViewControllerNibName, bundle: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
         
        bannerView.constraints.first { $0.firstAttribute == .width }?.constant = adSize.width
        bannerView.constraints.first { $0.firstAttribute == .height }?.constant = adSize.height
    }
}
