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


@objcMembers public class StackModel: ContainerModel, StackModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public class var identifier: String { "stack" }
    
    public var backgroundColor: Color?
    public var molecules: [StackItemModelProtocol & MoleculeModelProtocol]
    public var axis: NSLayoutConstraint.Axis = .vertical
    public var spacing: CGFloat = Padding.Four
    public var useStackSpacingBeforeFirstItem = false
    
    public var children: [MoleculeModelProtocol] {
        return molecules
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(molecules: [StackItemModelProtocol & MoleculeModelProtocol], axis: NSLayoutConstraint.Axis? = nil, spacing: CGFloat? = nil) {
        self.molecules = molecules
        if let axis = axis {
            self.axis = axis
        }
        if let spacing = spacing {
            self.spacing = spacing
        } else if axis == .horizontal {
            self.spacing = Padding.Component.gutterForApplicationWidth
        }
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case molecules
        case axis
        case spacing
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        molecules = try typeContainer.decodeModels(codingKey: .molecules)
        if let axisString = try typeContainer.decodeIfPresent(String.self, forKey: .axis), let optionalAxis = NSLayoutConstraint.Axis(rawValue: axisString) {
            axis = optionalAxis
        }
        if let spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing) {
            self.spacing = spacing
        } else if axis == .horizontal {
            self.spacing = Padding.Component.gutterForApplicationWidth
        }
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        try super.init(from: decoder)
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeModels(molecules, forKey: .molecules)
        try container.encodeIfPresent(axis.rawValueString, forKey: .axis)
        try container.encodeIfPresent(spacing, forKey: .spacing)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
    }
    
}
