//
//  BadgeIndicatorModel.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 4/12/24.
//  Copyright © 2024 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS


open class BadgeIndicatorModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public static var identifier: String { "badgeIndicator" }
    public var id: String = UUID().uuidString
    public var backgroundColor: Color?
    
    //--------------------------------------------------
    // MARK: - VDS Properties
    //--------------------------------------------------
    public var surface: Surface { inverted ? .dark : .light }
    public var inverted: Bool = false
    public var number: Int?
    public var accessibilityText: String?
    public var fillColor = BadgeIndicator.FillColor.red
    public var borderColorLight: Color?
    public var borderColorDark: Color?
    public var kind =  BadgeIndicator.Kind.simple
    public var maximumDigits =  BadgeIndicator.MaximumDigits.two
    public var size =  BadgeIndicator.Size.xxlarge
    public var leadingCharacter: String?
    public var trailingText: String?
    public var dotSize: CGFloat?
    public var verticalPadding: CGFloat?
    public var horizontalPadding: CGFloat?
    public var hideDot: Bool = false
    public var hideBorder: Bool = false
    public var width: CGFloat?
    public var height: CGFloat?
    
    private enum CodingKeys: String, CodingKey {
        case id
        case inverted
        case accessibilityText
        case number
        case fillColor
        case borderColorLight
        case borderColorDark
        case kind
        case maximumDigits
        case size
        case leadingCharacter
        case trailingText
        case dotSize
        case verticalPadding
        case horizontalPadding
        case hideDot
        case hideBorder
        case width
        case height
    }
        
    required public convenience init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.init()
        id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        inverted = try container.decodeIfPresent(Bool.self, forKey: .inverted) ?? false
        accessibilityText = try container.decodeIfPresent(String.self, forKey: .accessibilityText)
        number = try container.decodeIfPresent(Int.self, forKey: .number)
        fillColor = try container.decodeIfPresent(BadgeIndicator.FillColor.self, forKey: .fillColor) ?? .red
        borderColorLight = try container.decodeIfPresent(Color.self, forKey: .borderColorLight)
        borderColorDark = try container.decodeIfPresent(Color.self, forKey: .borderColorDark)
        kind = try container.decodeIfPresent(BadgeIndicator.Kind.self, forKey: .kind) ?? .simple
        maximumDigits = try container.decodeIfPresent(BadgeIndicator.MaximumDigits.self, forKey: .maximumDigits) ?? .two
        size = try container.decodeIfPresent(BadgeIndicator.Size.self, forKey: .size) ?? .xxlarge
        leadingCharacter = try container.decodeIfPresent(String.self, forKey: .leadingCharacter)
        trailingText = try container.decodeIfPresent(String.self, forKey: .trailingText)
        dotSize = try container.decodeIfPresent(CGFloat.self, forKey: .dotSize)
        verticalPadding = try container.decodeIfPresent(CGFloat.self, forKey: .verticalPadding)
        horizontalPadding = try container.decodeIfPresent(CGFloat.self, forKey: .horizontalPadding)
        hideDot = try container.decodeIfPresent(Bool.self, forKey: .hideDot) ?? false
        hideBorder = try container.decodeIfPresent(Bool.self, forKey: .hideBorder) ?? false
        width = try container.decodeIfPresent(CGFloat.self, forKey: .width)
        height = try container.decodeIfPresent(CGFloat.self, forKey: .height)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(inverted, forKey: .inverted)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encodeIfPresent(number, forKey: .number)
        try container.encodeIfPresent(fillColor, forKey: .fillColor)
        try container.encodeIfPresent(borderColorLight, forKey: .borderColorLight)
        try container.encodeIfPresent(borderColorDark, forKey: .borderColorDark)
        try container.encodeIfPresent(kind, forKey: .kind)
        try container.encodeIfPresent(maximumDigits, forKey: .maximumDigits)
        try container.encodeIfPresent(size, forKey: .size)
        try container.encodeIfPresent(leadingCharacter, forKey: .leadingCharacter)
        try container.encodeIfPresent(trailingText, forKey: .trailingText)
        try container.encodeIfPresent(dotSize, forKey: .dotSize)
        try container.encodeIfPresent(verticalPadding, forKey: .verticalPadding)
        try container.encodeIfPresent(hideDot, forKey: .hideDot)
        try container.encodeIfPresent(hideBorder, forKey: .hideBorder)
        try container.encodeIfPresent(width, forKey: .width)
        try container.encodeIfPresent(height, forKey: .height)
    }    
}
