//
//  HeaderBiddingViewController.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 HeaderBiddingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var mediationNetworkVersionLabel: UILabel!
    
    var presenter = HeaderBiddingPresenter()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "Header 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.showNewHbMediationAdSegue {
            guard let navController = segue.destination as? UINavigationController,
                let detailViewController = navController.visibleViewController as? NewHeaderBiddingAdViewController 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: "HbMoPubBanner", bundle: nil)
        #if OBJCAPP
        guard let viewController = storyboard.instantiateInitialViewController() as? SMAHbMoPubBannerViewController else {
            return
        }
        #else
        guard let viewController = storyboard.instantiateInitialViewController() as? HbMoPubBannerViewController 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: "HbMoPubInterstitial", bundle: nil)
        
        #if OBJCAPP
        guard let viewController = storyboard.instantiateInitialViewController() as? SMAHbMoPubInterstitialViewController else {
            return
        }
        #else
        guard let viewController = storyboard.instantiateInitialViewController() as? HbMoPubInterstitialViewController 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: "HbMoPubRewarded", bundle: nil)
        
        #if OBJCAPP
        guard let viewController = storyboard.instantiateInitialViewController() as? SMAHbMoPubRewardedViewController else {
            return
        }
        #else
        guard let viewController = storyboard.instantiateInitialViewController() as? HbMoPubRewardedViewController else {
            return
        }
        #endif
        
        viewController.mopubModel = model as? MopubModel
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: viewController), sender: nil)
    }
}
