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

import Foundation

class InterstitialsPresenter: AdPresenting {    
  
    private var interstitialModels: [InterstitialAdModel] = [InterstitialAdModel(publisherId: "0", adSpaceId: "130512792", name: "Multi ad format interstitial", isCustom: false)]
    
    var models: [AdModel] {
        get {
            if (UserDefaults.standard.object(forKey: AdType.Interstitial.identifier) != nil) {
                if let modelsArray = UserDefaults.standard.array(forKey: AdType.Interstitial.identifier) as? [[String:Any]] {
                    interstitialModels = modelsArray.map { InterstitialAdModel(dictionary: $0)! }
                }
            }
            
            return interstitialModels
        }
        
        set(newValue){
            let models = newValue.map{ $0.propertyListRepresentation }
            UserDefaults.standard.set(models, forKey: AdType.Interstitial.identifier)
            interstitialModels = newValue as! [InterstitialAdModel]
        }
    }
    
    var onNewAdModel: (() -> ())?
    
    func add(adModel publisherId: String, adSpaceId: String, name: String) {
        let interstitialAdModel = InterstitialAdModel(publisherId: publisherId, adSpaceId: adSpaceId, name: name, isCustom: true)
        models.append(interstitialAdModel)
        
        guard let newInterstitialAdModelNotification = onNewAdModel else {
            return
        }
        newInterstitialAdModelNotification()
    }
    
}
