//
//  MediationAdTypesViewController.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 MediationAdTypesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

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

    var networkType: NetworkType!
    var presenter = MediationPresenter()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = networkType.identifier
        tableView.delegate = self
        tableView.dataSource = self
        setMediationNetworkVersion()
    }

    func setMediationNetworkVersion() {
        switch networkType! {
        case .MoPub:
            mediationNetworkVersionLabel.text = "MoPub SDK version \(MP_SDK_VERSION)"
            break
        case .AdMob:
            mediationNetworkVersionLabel.text = "AdMob SDK version \(GADRequest.sdkVersion())"
            break
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch networkType! {
        case .MoPub:
            return presenter.mopubModels.count
        case .AdMob:
            return presenter.admobModels.count
        }
    }

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

        switch networkType! {
        case .MoPub:
            cell.textLabel?.text = presenter.mopubModels[indexPath.row].name
        case .AdMob:
            cell.textLabel?.text = presenter.admobModels[indexPath.row].name
        }

        return cell
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == .delete) {
            switch networkType! {
            case .MoPub:
                presenter.mopubModels.remove(at: indexPath.row)
            case .AdMob:
                presenter.admobModels.remove(at: indexPath.row)
            }

            tableView.reloadData()
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch networkType! {
        case .MoPub:
            showViewControllerForPredefinedAds(model: presenter.mopubModels[indexPath.row])
        case .AdMob:
            showViewControllerForPredefinedAds(model: presenter.admobModels[indexPath.row])
        }
    }

    func showViewControllerForPredefinedAds(model: MediationAdModel) {
        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: MediationAdModel? = nil) {
        switch self.networkType! {
        case .MoPub:
            let storyboard = UIStoryboard(name: "MoPubBanner", bundle: nil)
            #if OBJCAPP
            guard let viewController = storyboard.instantiateInitialViewController() as? SMAMoPubBannerAdapterViewController else {
                return
            }
            #else
            guard let viewController = storyboard.instantiateInitialViewController() as? MoPubBannerViewController else {
                return
            }
            #endif

            viewController.mopubModel = model as? MopubModel
            splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
            break
        case .AdMob:
            let storyboard = UIStoryboard(name: "AdMobBanner", bundle: nil)

            #if OBJCAPP
            guard let viewController = storyboard.instantiateInitialViewController() as? SMAAdMobBannerAdapterViewController else {
                return
            }
            #else
            guard let viewController = storyboard.instantiateInitialViewController() as? AdMobBannerAdapterViewController else {
                return
            }
            #endif
            viewController.adMobModel = model as? AdMobModel
            splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
            break
        }
    }

    func showViewControllerForInterstitial(forModel model: MediationAdModel? = nil) {
        switch self.networkType! {
        case .MoPub:
            let storyboard = UIStoryboard(name: "MoPubInterstitial", bundle: nil)

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

            viewController.mopubModel = model as? MopubModel
            splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
            break
        case .AdMob:
            let storyboard = UIStoryboard(name: "AdMobInterstitial", bundle: nil)

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

            viewController.adMobModel = model as? AdMobModel
            splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
            break
        }
    }

    func showViewControllerForRewarded(forModel model: MediationAdModel? = nil) {
        switch self.networkType! {
        case .MoPub:
            let storyboard = UIStoryboard(name: "MoPubRewarded", bundle: nil)

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

            viewController.mopubModel = model as? MopubModel
            splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
            break
        case .AdMob:
            let storyboard = UIStoryboard(name: "AdMobRewarded", bundle: nil)
            #if OBJCAPP
            guard let viewController = storyboard.instantiateInitialViewController() as? SMAAdMobRewardedAdapterViewController else {
                return
            }
            #else
            guard let viewController = storyboard.instantiateInitialViewController() as? AdMobRewardedInterstitialAdapterViewController else {
                return
            }
            #endif

            viewController.adMobModel = model as? AdMobModel
            splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
            break
        }
    }
}

