//
//  UnifiedBiddingViewController.swift
//  sdk-demo-ios
//
//  Created by Smaato Inc on 03/03/2019.
//  Copyright © 2019 Smaato Inc. All rights reserved.
//

import UIKit
import MoPub
import GoogleMobileAds

class UnifiedBiddingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet private weak var tableView: UITableView!
    @IBOutlet private weak var mediationNetworkVersionLabel: UILabel!

    var presenter = UnifiedBiddingPresenter()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Unified Bidding via MoPub"
        tableView.delegate = self
        tableView.dataSource = self
        setMediationNetworkVersion()
    }

    func setMediationNetworkVersion() {
        mediationNetworkVersionLabel.text = "MoPub SDK version \(MP_SDK_VERSION)"
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return presenter.mopubUBModels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") else {
            return UITableViewCell()
        }

        cell.textLabel?.text = presenter.mopubUBModels[indexPath.row].name
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        showViewControllerForPredefinedAds(model: presenter.mopubUBModels[indexPath.row])
    }

    func showViewControllerForPredefinedAds(model: MopubUBModel) {
        switch model.adType! {
        case .Banner:
            showViewControllerForBanner(forModel: model)
            break
        case .Interstitial:
            showViewControllerForInterstitial(forModel: model)
            break
        case .Rewarded:
            showViewControllerForRewarded(forModel: model)
            break
        }
    }

    func showViewControllerForBanner(forModel model: MopubUBModel) {
        let storyboard = UIStoryboard(name: "UbMoPubBanner", bundle: nil)
        #if OBJCAPP
        guard let viewController = storyboard.instantiateInitialViewController() as? SMAUbMoPubBannerViewController else {
            return
        }
        #else
        guard let viewController = storyboard.instantiateInitialViewController() as? UbMoPubBannerViewController else {
            return
        }
        #endif

        viewController.mopubModel = model
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }

    func showViewControllerForInterstitial(forModel model: MopubUBModel) {
        let storyboard = UIStoryboard(name: "UbMoPubInterstitial", bundle: nil)

        #if OBJCAPP
        guard let viewController = storyboard.instantiateInitialViewController() as? SMAUbMoPubInterstitialViewController else {
            return
        }
        #else
        guard let viewController = storyboard.instantiateInitialViewController() as? UbMoPubInterstitialViewController else {
            return
        }
        #endif

        viewController.mopubModel = model
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }

    func showViewControllerForRewarded(forModel model: MopubUBModel) {
        let storyboard = UIStoryboard(name: "UbMoPubRewarded", bundle: nil)

        #if OBJCAPP
        guard let viewController = storyboard.instantiateInitialViewController() as? SMAUbMoPubRewardedViewController else {
            return
        }
        #else
        guard let viewController = storyboard.instantiateInitialViewController() as? UbMoPubRewardedViewController else {
            return
        }
        #endif

        viewController.mopubModel = model
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }
}
