//
//  BannerAdModel.swift
//  sdk-demo-ios
//
//  Created by Smaato Inc on 10.07.18.
//  Copyright © 2018 Smaato Inc. All rights reserved.
//

import Foundation
import SmaatoSDKBanner

protocol AdModel {
    var adSpaceId: String { get set }
    var name: String { get set }
    var size: BannerSize? { get set }
    var propertyListRepresentation: [String: Any] { get }
}

extension AdModel {
    var size: BannerSize? {
        get {
            return nil
        }
        set {
        }
    }

    var propertyListRepresentation: [String: Any] {
        get {
            return ["adSpaceId": adSpaceId, "name": name, "size": size?.rawValue ?? ""]
        }
    }
}

struct BannerAdModel: AdModel {
    var size: BannerSize? = nil
    var adSpaceId: String
    var name: String

    init(size: BannerSize, adSpaceId: String, name: String) {
        self.size = size
        self.adSpaceId = adSpaceId
        self.name = name
    }

    init?(dictionary: [String: Any]) {
        guard let size = dictionary["size"] as? Int,
              let adSpaceId = dictionary["adSpaceId"] as? String,
              let name = dictionary["name"]  as? String else {
            return nil
        }
        self.init(size: BannerSize(rawValue: size)!, adSpaceId: adSpaceId, name: name)
    }
}

enum BannerSize: Int, CaseIterable {
    case any
    case xxlarge
    case medium
    case leaderboard
    case skyscraper

    var adSizeType: SMABannerAdSize {
        switch self {
        case .any:
            return SMABannerAdSize.any
        case .xxlarge:
            return SMABannerAdSize.xxLarge_320x50
        case .medium:
            return SMABannerAdSize.mediumRectangle_300x250
        case .leaderboard:
            return SMABannerAdSize.leaderboard_728x90
        case .skyscraper:
            return SMABannerAdSize.skyscraper_120x600
        }
    }

    var description: String {
        switch self {
        case .any:
            return "Any"
        case .xxlarge:
            return "320 x 50"
        case .medium:
            return "300 x 250"
        case .leaderboard:
            return "728 x 90"
        case .skyscraper:
            return "120 x 600"
        }
    }

    var size: CGSize {
        switch self {
        case .any:
            return CGSize(width: 0, height: 0)
        case .xxlarge:
            return CGSize(width: 320, height: 50)
        case .medium:
            return CGSize(width: 300, height: 250)
        case .leaderboard:
            return CGSize(width: 728, height: 90)
        case .skyscraper:
            return CGSize(width: 120, height: 600)
        }
    }
}
