//
//  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 adSpaceId: String! { get set }
    var name: String! { get set }
    var adType: AdType! { get set }
    var moPubsize: MoPubBannerSizes { get set }
    var adMobsize: AdMobBannerSizes { get set }
    var isCustom: Bool { 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 ["adSpaceId": adSpaceId, "adType": adType.rawValue, "name": name, "moPubsize": moPubsize.rawValue, "adMobsize": adMobsize.rawValue, "isCustom": isCustom]
        }
    }
}

@objcMembers class MopubModel: NSObject, MediationAdModel {
    var moPubsize: MoPubBannerSizes
    var isCustom: Bool
    var adType: AdType!
    var adSpaceId: String!
    var name: String!

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

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

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

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

@objcMembers class AdMobModel: NSObject, MediationAdModel {
    var isCustom: Bool
    var adMobsize: AdMobBannerSizes
    var adType: AdType!
    var adSpaceId: String!
    var name: String!

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

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

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

    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 .skyscrapperSize:
            return "Skyscraper size"
        }
    }
}
