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

import UIKit

class AdTypesViewController: UITableViewController {

    var adtype: AdType!
    var presenter: AdPresenting!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = adtype.identifier
    }

    private func adModel(for indexPath: IndexPath) -> AdModel? {
        let model = presenter.models[indexPath.row]
        return model
    }

    //MARK: UITableViewControllerDelegate and UITableViewControllerDataSourcere

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

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

        let model = adModel(for: indexPath)
        cell.textLabel?.text = model?.name
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        guard let model = adModel(for: indexPath) else {
            return
        }
        showViewController(with: model)
    }

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        guard let header = view as? UITableViewHeaderFooterView else {
            return
        }

        header.textLabel?.font = UIFont(appFont: .SFDisplay_Semibold, size: 24)
        header.textLabel?.textColor = .appColor
    }

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

    //MARK: show detail view controller

    private func showViewController(with adModel: AdModel) {

        switch self.adtype! {
        case .Interstitial:
            self.showInterstitialVC(with: adModel)
            break
        case .Banner:
            self.showBannerVC(with: adModel)
            break
        case .Rewarded:
            self.showRewardedVC(with: adModel)
            break
        }
    }

    func showInterstitialVC(with adModel: AdModel) {
        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 = adModel.adSpaceId

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

    func showBannerVC(with adModel: AdModel) {
        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

        let bannerModel = adModel as! BannerAdModel
        bannerViewController.adSpaceId = bannerModel.adSpaceId
        bannerViewController.adSize = bannerModel.size!.adSizeType
        bannerViewController.expectedSize = bannerModel.size!.size

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

    func showRewardedVC(with adModel: AdModel) {
        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 = adModel.adSpaceId

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