//
//  ButtonGroupModel.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 1/11/24.
//  Copyright © 2024 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

public class ButtonGroupModel: ParentMoleculeModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "buttonGroup"
    public var id: String = UUID().uuidString
    public var backgroundColor: Color?
    public var children: [MoleculeModelProtocol] { buttons }

    //--------------------------------------------------
    // MARK: - VDS Properties
    //--------------------------------------------------
    public var buttons: [ButtonModelProtocol & MoleculeModelProtocol]
    public var alignment: VDS.ButtonGroup.Alignment = .center
    public var rowQuantityPhone: Int = 0
    public var rowQuantityTablet: Int = 0
    public var childWidthValue: CGFloat?
    public var childWidthPercentage: CGFloat?
    public var surface: VDS.Surface = .light
    public var enabled: Bool = true
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case backgroundColor
        case buttons
        case alignment
        case rowQuantityPhone
        case rowQuantityTablet
        case childWidthValue
        case childWidthPercentage
        case surface
        case enabled
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        surface = try typeContainer.decodeIfPresent(Surface.self, forKey: .surface) ?? .light
        enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
        buttons = try typeContainer.decodeModels(codingKey: .buttons)
        alignment = try typeContainer.decodeIfPresent(VDS.ButtonGroup.Alignment.self, forKey: .alignment) ?? .center
        rowQuantityPhone = try typeContainer.decodeIfPresent(Int.self, forKey: .rowQuantityPhone) ?? 0
        rowQuantityTablet = try typeContainer.decodeIfPresent(Int.self, forKey: .rowQuantityTablet) ?? 0
        childWidthValue = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .childWidthValue)
        childWidthPercentage = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .childWidthPercentage)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(surface, forKey: .surface)
        try container.encode(enabled, forKey: .enabled)
        try container.encodeModels(buttons, forKey: .buttons)
        try container.encode(alignment, forKey: .alignment)
        try container.encode(rowQuantityPhone, forKey: .rowQuantityPhone)
        try container.encode(rowQuantityTablet, forKey: .rowQuantityTablet)
        try container.encodeIfPresent(childWidthValue, forKey: .childWidthValue)
        try container.encodeIfPresent(childWidthPercentage, forKey: .childWidthPercentage)
    }
}
