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

import Foundation

protocol MediationAdModel {
    var adUnitId: String { get set }
    var name: String { get set }
    var adType: AdType { get set }
    var moPubsize: MoPubBannerSizes { get set }
    var adMobsize: AdMobBannerSizes { get set }

    var propertyListRepresentation: [String: Any] { get }
}

extension MediationAdModel {
    var moPubsize: MoPubBannerSizes {
        get {
            return .bannerSize
        }
        set {
        }
    }

    var adMobsize: AdMobBannerSizes {
        get {
            return .bannerSize
        }
        set {
        }
    }

    var propertyListRepresentation: [String: Any] {
        get {
            return ["adUnitId": adUnitId, "adType": adType.rawValue, "name": name, "moPubsize": moPubsize.rawValue, "adMobsize": adMobsize.rawValue]
        }
    }
}

@objcMembers class MopubModel: NSObject, MediationAdModel {
    var moPubsize: MoPubBannerSizes
    var adType: AdType
    var adUnitId: String
    var name: String

    init(moPubsize: MoPubBannerSizes? = nil, adType: AdType, adUnitId: String, name: String) {
        self.moPubsize = moPubsize ?? .bannerSize
        self.adType = adType
        self.adUnitId = adUnitId
        self.name = name
    }

    convenience init?(dictionary: [String: Any]) {
        guard let size = dictionary["moPubsize"] as? Int,
              let adUnitId = dictionary["adUnitId"] as? String,
              let adType = dictionary["adType"] as? Int,
              let name = dictionary["name"] as? String else {
            return nil
        }
        self.init(moPubsize: MoPubBannerSizes(rawValue: size), adType: AdType(rawValue: adType)!, adUnitId: adUnitId, name: name)
    }
}

@objcMembers class MopubUBModel: MopubModel {
    var smaatoAdspaceId: String!
    
    init(moPubsize: MoPubBannerSizes? = nil, adType: AdType, adUnitId: String, name: String, smaatoAdspaceId: String) {
        super.init(moPubsize: moPubsize, adType: adType, adUnitId: adUnitId, name: name)
        self.smaatoAdspaceId = smaatoAdspaceId
    }
}

@objc enum MoPubBannerSizes: Int, CaseIterable {
    case bannerSize
    case medRectSize
    case leaderBoardSize

    var description: String {
        switch self {
        case .bannerSize:
            return "Banner size"
        case .medRectSize:
            return "Medium rectangle size"
        case .leaderBoardSize:
            return "Leader board"
        }
    }
}

@objcMembers class AdMobModel: NSObject, MediationAdModel {
    var adMobsize: AdMobBannerSizes
    var adType: AdType
    var adUnitId: String
    var name: String

    init(adMobsize: AdMobBannerSizes? = nil, adType: AdType, adUnitId: String, name: String) {
        self.adMobsize = adMobsize ?? AdMobBannerSizes.bannerSize
        self.adType = adType
        self.adUnitId = adUnitId
        self.name = name
    }

    convenience init?(dictionary: [String: Any]) {
        guard let size = dictionary["adMobsize"] as? Int,
              let adUnitId = dictionary["adUnitId"] as? String,
              let adType = dictionary["adType"] as? Int,
              let name = dictionary["name"] as? String else {
            return nil
        }
        self.init(adMobsize: AdMobBannerSizes(rawValue: size), adType: AdType(rawValue: adType)!, adUnitId: adUnitId, name: name)
    }
}

@objc enum AdMobBannerSizes: Int, CaseIterable {
    case bannerSize
    case largeBannerSize
    case medRectSize
    case leaderBoardSize
    case skyscraperSize

    var description: String {
        switch self {
        case .bannerSize:
            return "Banner size"
        case .largeBannerSize:
            return "Large banner size"
        case .medRectSize:
            return "Medium rectangle size"
        case .leaderBoardSize:
            return "Leader board"
        case .skyscraperSize:
            return "Skyscraper size"
        }
    }
}
