//
//  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
        
        #if compiler(>=5.1)
        if #available(iOS 13.0, *) {
            cell.textLabel?.textColor = UIColor.label
        }
        #endif
        
        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:
            showInterstitialVC(with: adModel)
        case .Banner:
            showBannerVC(with: adModel)
        case .Rewarded:
            showRewardedVC(with: adModel)
        case .Native:
            showNativeVC(with: adModel)
        }
    }

    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)
    }
    
    func showNativeVC(with adModel: AdModel) {
       let storyboard = UIStoryboard(name: "Native", bundle: nil)
       let nativeModel = adModel as! NativeAdModel
        
       #if OBJCAPP
       guard let nativeViewController = storyboard.instantiateInitialViewController() as? SMANativeViewController else {
           return
       }
       switch nativeModel.mode {
         case .auto:
            nativeViewController.renderingMode = SmaatoNativeRenderingMode.autoBySDK
         case .manual:
            nativeViewController.renderingMode = SmaatoNativeRenderingMode.manualByPublisher
       }
       #else
       guard let nativeViewController = storyboard.instantiateInitialViewController() as? NativeViewController else {
           return
       }
       switch nativeModel.mode {
         case .auto:
            nativeViewController.renderingMode = .autoBySDK
         case .manual:
            nativeViewController.renderingMode = .manualByPublisher
       }
       #endif

       nativeViewController.adSpaceId = nativeModel.adSpaceId
       splitViewController?.showDetailViewController(UINavigationController(rootViewController: nativeViewController), sender: nil)
    }

}
