//
//  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()
    var csmPresenter = CSMPresenter()
    var adSection: Sections!
    
    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:
            if adSection == .Mediation {
                return presenter.mopubModels.count
            }
            return csmPresenter.mopubCSMModels.count
        case .AdMob:
            if adSection == .Mediation {
                return presenter.admobModels.count
            }
            return csmPresenter.admobCSMModels.count
        }
    }

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

        switch networkType! {
        case .MoPub:
            if adSection == .Mediation {
                cell.textLabel?.text = presenter.mopubModels[indexPath.row].name
            } else {
                cell.textLabel?.text = csmPresenter.mopubCSMModels[indexPath.row].name
            }
        case .AdMob:
            if adSection == .Mediation {
                cell.textLabel?.text = presenter.admobModels[indexPath.row].name
            } else {
                cell.textLabel?.text = csmPresenter.admobCSMModels[indexPath.row].name
            }
        }

        return cell
    }

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

            tableView.reloadData()
        }
    }

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

    func showViewControllerForPredefinedAds(model: MediationAdModel) {
        switch model.adType {
        case .Banner:
            showViewControllerForBanner(forModel: model)
        case .Interstitial:
            showViewControllerForInterstitial(forModel: model)
        case .Rewarded:
            showViewControllerForRewarded(forModel: model)
        case .Native:
            showViewControllerForNative(forModel: model)
        }
    }
    
    func showViewControllerForPredefinedAds(model: AdModel) {
        switch model {
        case is BannerAdModel:
            showBannerVCForCSM(model: model as! BannerAdModel)
            break
        case is InterstitialAdModel:
            showInterstitialVCForCSM(model: model as! InterstitialAdModel)
            break
        case is RewardedAdModel:
            showRewardedVCForCSM(model: model as! RewardedAdModel)
            break
        default:
            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 showBannerVCForCSM(model: BannerAdModel) {
        let storyboard = UIStoryboard(name: "Banner", bundle: nil)
        #if OBJCAPP
        guard let bannerViewController = storyboard.instantiateInitialViewController() as? SMABannerViewController else {
          return
        }
        #else
        guard let bannerViewController = storyboard.instantiateInitialViewController() as? BannerViewController else {
          return
        }
        #endif

        bannerViewController.adSpaceId = model.adSpaceId
        bannerViewController.adSize = model.size!.adSizeType
        bannerViewController.expectedSize = model.size!.size
        
        splitViewController?.showDetailViewController(UINavigationController(rootViewController: bannerViewController), sender: nil)
    }
    
    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 showInterstitialVCForCSM(model: InterstitialAdModel) {
        let storyboard = UIStoryboard(name: "Interstitial", bundle: nil)
        #if OBJCAPP
        guard let interstitialViewController = storyboard.instantiateInitialViewController() as? SMAInterstitialBannerViewController else {
           return
        }
        #else
        guard let interstitialViewController = storyboard.instantiateInitialViewController() as? InterstitialViewController else {
           return
        }
        #endif

        interstitialViewController.adSpaceId = model.adSpaceId

        splitViewController?.showDetailViewController(UINavigationController(rootViewController: interstitialViewController), sender: nil)
    }

    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
        }
    }
    
    func showRewardedVCForCSM(model: RewardedAdModel) {
        let storyboard = UIStoryboard(name: "RewardedAd", bundle: nil)

        #if OBJCAPP
        guard let rewardedAdViewController = storyboard.instantiateInitialViewController() as? SMARewardedAdViewController else {
            return
        }
        #else
        guard let rewardedAdViewController = storyboard.instantiateInitialViewController() as? RewardedAdViewController else {
            return
        }
        #endif

        rewardedAdViewController.adSpaceId = model.adSpaceId

        splitViewController?.showDetailViewController(UINavigationController(rootViewController: rewardedAdViewController), sender: nil)
    }
    
    func showViewControllerForNative(forModel model: MediationAdModel? = nil) {
        switch self.networkType! {
        case .MoPub:
            let storyboard = UIStoryboard(name: "MoPubNative", bundle: nil)
            #if OBJCAPP
            guard let viewController = storyboard.instantiateInitialViewController() as? SMAMoPubNativeAdapterViewController else {
                return
            }
            #else
            guard let viewController = storyboard.instantiateInitialViewController() as? MoPubNativeViewController else {
                return
            }
            #endif

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

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

