//
//  AppDelegate.swift
//  DemoApp
//
//  Created by Smaato Inc on 18.04.18.
//  Copyright © 2018 Smaato Inc. All rights reserved.
//

import UIKit
import MoPub
import GoogleMobileAds
import SmaatoSDKCore
import SmaatoSDKUnifiedBidding

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        setupAppearance()
        setupSmaatoSDK()
        setupMoPubSDK()
        setupAdMobSDK()

        return true
    }
}

private extension AppDelegate {

    private func setupSmaatoSDK() {
        let config = SMAConfiguration()

        // You can change 'httpsOnly' flag in SettingsViewController view controller. By default httpsOnly = false in this application
        config.httpsOnly = UserDefaults.standard.bool(forKey: "MySavedHTTPSOnlySettings")
        config.loggingDisabled = false
        config.logLevel = .info

        SmaatoSDK.initSDK(config)

        // You can change 'requireCoppaCompliantAds' flag in SettingsViewController view controller. By default requireCoppaCompliantAds = false in this application
        SmaatoSDK.requireCoppaCompliantAds = UserDefaults.standard.bool(forKey: "MySavedCoppaSettings")
    }

    private func setupAppearance() {
        let navigationBar = UINavigationBar.appearance()
        navigationBar.isTranslucent = true
        navigationBar.barTintColor = .appColor
        navigationBar.tintColor = UIColor.white

        navigationBar.shadowImage = UIImage()

        navigationBar.titleTextAttributes =
                [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.white]
        if #available(iOS 11.0, *) {
            navigationBar.largeTitleTextAttributes =
                    [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.white]
        }

    }

    private func setupMoPubSDK() {
        let sdkConfig = MPMoPubConfiguration(adUnitIdForAppInitialization: "17c9566ad386405db78c5447dc93c75d")
        sdkConfig.globalMediationSettings = []
        sdkConfig.loggingLevel = .info
        MoPub.sharedInstance().initializeSdk(with: sdkConfig, completion: {
            print("MoPub initialization complete")
        })
        setupUnifiedBidding()
    }

    private func setupAdMobSDK() {
        GADMobileAds.sharedInstance().start()
    }

    private func setupUnifiedBidding() {
        SmaatoSDK.enableUnifiedBidding(withPublisherId: "1100042525")
    }
}
