//
//  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 MoPub"
        tableView.delegate = self
        tableView.dataSource = self
        setMediationNetworkVersion()
        presenter.onNewAdModel = { [weak self] in
            self?.tableView.reloadData()
        }
    }

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

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let identifier = segue.identifier else {
            return
        }

        if identifier == Segues.showNewUbMediationAdSegue {
            guard let navController = segue.destination as? UINavigationController,
                  let detailViewController = navController.visibleViewController as? NewUnifiedBiddingAdViewController else {
                return
            }
            detailViewController.presenter = presenter
            navController.modalPresentationStyle = .formSheet
        }
    }

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

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

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

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if presenter.mopubModels[indexPath.row].isCustom {
            return true
        }
        return false
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == .delete) {
            presenter.mopubModels.remove(at: indexPath.row)
            tableView.reloadData()
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        showViewControllerForPredefinedAds(model: presenter.mopubModels[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) {
        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 as? MopubModel
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }

    func showViewControllerForInterstitial(forModel model: MediationAdModel? = nil) {
        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 as? MopubModel
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }

    func showViewControllerForRewarded(forModel model: MediationAdModel? = nil) {
        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 as? MopubModel
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }
}
