//
//  VDS-Tilelet+Codable.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 1/25/23.
//  Copyright © 2023 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

extension Tilelet.BadgeModel: Codable {
    private enum CodingKeys: String, CodingKey {
        case text, fillColor, surface, numberOfLines, maxWidth
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let text = try container.decode(String.self, forKey: .text)
        let fillColor = try container.decodeIfPresent(Badge.FillColor.self, forKey: .fillColor) ?? .red
        let surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .light
        let numberOfLines = try container.decodeIfPresent(Int.self, forKey: .numberOfLines) ?? 0
        let maxWidth = try container.decodeIfPresent(CGFloat.self, forKey: .maxWidth)
        self.init(text: text, fillColor: fillColor, surface: surface, numberOfLines: numberOfLines, maxWidth: maxWidth)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(fillColor, forKey: .fillColor)
        try container.encode(surface, forKey: .surface)
        try container.encode(numberOfLines, forKey: .numberOfLines)
        try container.encodeIfPresent(maxWidth, forKey: .maxWidth)
    }
}

extension Tilelet.DescriptiveIcon: Codable {
    private enum CodingKeys: String, CodingKey {
        case name, size, surface
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let name = try container.decode(Icon.Name.self, forKey: .name)
        let size = try container.decodeIfPresent(Icon.Size.self, forKey: .size) ?? .medium
        let surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .dark
        self.init(name: name, size: size, surface: surface)
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
        try container.encode(size, forKey: .size)
        try container.encode(surface, forKey: .surface)
    }
}

extension Tilelet.DirectionalIcon: Codable {
    private enum CodingKeys: String, CodingKey {
        case size, surface
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let size = try container.decodeIfPresent(Icon.Size.self, forKey: .size) ?? .medium
        let surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .dark
        self.init(size: size, surface: surface)
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(size, forKey: .size)
        try container.encode(surface, forKey: .surface)
    }
}

