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

import Foundation
import MoPub
import GoogleMobileAds

@objc class MediationPresenter: NSObject {
    
    var mopuData:[MopubModel] = [
        MopubModel(moPubsize: .bannerSize, adType: .Banner, adSpaceId: "d6cbf05660b44fbaa89909c3756f6657", name: "Banner", isCustom: false),
                                    MopubModel(moPubsize: nil, adType: .Interstitial, adSpaceId: "1c01d618cbb74f7692109ca1176d9598", name: "Interstitial", isCustom: false),
                                    MopubModel(moPubsize: nil, adType: .Rewarded, adSpaceId: "9e855bb8ab324c10852e5a8fc025c5fd", name: "Rewarded", isCustom: false)
                                    ]
    var admobData:[AdMobModel] = [
        AdMobModel(adMobsize: AdMobBannerSizes.bannerSize, adType: .Banner, adSpaceId: "ca-app-pub-3774013871324317/2949807561", name: "Banner", isCustom: false),
                                    AdMobModel(adMobsize: nil, adType: .Interstitial, adSpaceId: "ca-app-pub-3774013871324317/8893890078", name: "Interstitial", isCustom: false),
                                    AdMobModel(adMobsize: nil, adType: .Rewarded, adSpaceId: "ca-app-pub-3774013871324317/9440684981", name: "Rewarded", isCustom: false)]

    var mopubModels: [MopubModel] {
        get {
            if (UserDefaults.standard.object(forKey: "MoPubAds") != nil) {
                if let modelsArray = UserDefaults.standard.array(forKey: "MoPubAds") as? [[String:Any]] {
                    mopuData = modelsArray.compactMap { MopubModel(dictionary: $0)! }
                }
            }
            
            return mopuData
        }
        
        set(newValue){
            let models = newValue.map{ $0.propertyListRepresentation }
            UserDefaults.standard.set(models, forKey: "MoPubAds")
            mopuData = newValue
        }
    }
    
    var admobModels: [AdMobModel] {
        get {
            if (UserDefaults.standard.object(forKey: "AdMobAds") != nil) {
                if let modelsArray = UserDefaults.standard.array(forKey: "AdMobAds") as? [[String:Any]] {
                    admobData = modelsArray.compactMap { AdMobModel(dictionary: $0)! }
                }
            }
            
            return admobData
        }
        
        set(newValue){
            let models = newValue.map{ $0.propertyListRepresentation }
            UserDefaults.standard.set(models, forKey: "AdMobAds")
            admobData = newValue
        }
    }
    
    var onNewAdModel: (() -> ())?

    func add(mopubAdModel adSpaceId: String, name: String, adType: AdType, size: MoPubBannerSizes? = nil) {
        let model = MopubModel(moPubsize: size, adType: adType, adSpaceId: adSpaceId, name: name, isCustom: true)
        mopubModels.append(model)
        
        guard let newModelNotification = onNewAdModel else {
            return
        }
        newModelNotification()
    }
    
    func add(adMobAdModel adSpaceId: String, name: String, adType: AdType, size: AdMobBannerSizes? = nil) {
        let model = AdMobModel(adMobsize: size, adType: adType, adSpaceId: adSpaceId, name: name, isCustom: true)
        admobModels.append(model)
        
        guard let newModelNotification = onNewAdModel else {
            return
        }
        newModelNotification()
    }
}

