//
//  Category.swift
//  OrdermentumSDK
//
//  Created by Brandon Stillitano on 12/3/19.
//  Copyright © 2019 Ordermentum Pty Ltd. All rights reserved.
//

import Foundation

public struct CategoryResponse {
    public var meta: Meta = Meta()
    public var links: Links = Links()
    public var data: [Category] = []
}

public struct Category: Encodable {
    public init() {}
    public var id: String = ""
    public var name: String = ""
    public var images: Image = Image()
}

extension CategoryResponse: Decodable {
    public init(from decoder: Decoder) throws {
        //Create Container
        guard let container = try? decoder.container(keyedBy: CodingKeys.self) else {
            return
        }
        
        //Decode Data
        meta = container.tryDecode(type: Meta.self, forKey: .meta) ?? Meta()
        links = container.tryDecode(type: Links.self, forKey: .links) ?? Links()
        data = container.tryDecode(type: [Category].self, forKey: .data) ?? []
    }
}

extension Category: Decodable {
    public init(from decoder: Decoder) throws {
        //Create Container
        guard let container = try? decoder.container(keyedBy: CodingKeys.self) else {
            return
        }
        
        //Decode Data
        id = container.tryDecode(type: String.self, forKey: .id) ?? ""
        name = container.tryDecode(type: String.self, forKey: .name) ?? ""
        images = container.tryDecode(type: Image.self, forKey: .images) ?? Image()
    }
}

extension Category: Equatable {
    public static func == (lhs: Category, rhs: Category) -> Bool {
        return lhs.id == rhs.id
    }
}

extension Category: AnalyticsTrackable {
    public var trackableProperties: [String: String]? {
        var properties: [String: String] = [:]
        properties["categoryId"] = id
        properties["categoryName"] = name
        return properties
    }
}
