//
//  TwoButtonViewModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/13/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit
import VDS

public class TwoButtonViewModel: ParentMoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "twoButtonView"
    public var id: String = UUID().uuidString
    public var backgroundColor: Color?
    public var primaryButton: ButtonModel?
    public var secondaryButton: ButtonModel?
    public var fillContainer: Bool = false
    
    public var children: [MoleculeModelProtocol] { [primaryButton, secondaryButton].compactMap { $0 } }
    
    public func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool  {
        return try replaceChildMolecule(at: &primaryButton, with: molecule)
        || replaceChildMolecule(at: &secondaryButton, with: molecule)
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case backgroundColor
        case primaryButton
        case secondaryButton
        case fillContainer
    }
    
    //--------------------------------------------------
    // MARK: - Initialzer
    //--------------------------------------------------
    
    public init(_ primaryButton: ButtonModel?, _ secondaryButton: ButtonModel?) {
        self.primaryButton = primaryButton
        self.secondaryButton = secondaryButton
    }
    
    //--------------------------------------------------
    // 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
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        
        //set context value for 'primary' style to be set for the primaryButton in case the
        //property is not returned in the JSON and once decoded, this value is removed from the context
        try decoder.setContext(value: Use.primary, for: "style") {
            self.primaryButton = try typeContainer.decodeMoleculeIfPresent(codingKey: .primaryButton)
        }
        
        //set context value for 'secondary' style to be set for the primaryButton in case the
        //property is not returned in the JSON and once decoded, this value is removed from the context
        try decoder.setContext(value: Use.secondary, for: "style") {
            self.secondaryButton = try typeContainer.decodeMoleculeIfPresent(codingKey: .secondaryButton)
        }
        fillContainer = try typeContainer.decodeIfPresent(Bool.self, forKey: .fillContainer) ?? false
    }
    
    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.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(primaryButton, forKey: .primaryButton)
        try container.encodeIfPresent(secondaryButton, forKey: .secondaryButton)
        try container.encodeIfPresent(fillContainer, forKey: .fillContainer)
    }
}
