//
//  ContainerModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 12/4/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


open class ContainerModel: ContainerModelProtocol, Codable {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var horizontalAlignment: UIStackView.Alignment?
    public var useHorizontalMargins: Bool?
    public var leftPadding: CGFloat?
    public var rightPadding: CGFloat?
    
    public var verticalAlignment: UIStackView.Alignment?
    public var useVerticalMargins: Bool?
    public var topPadding: CGFloat?
    public var bottomPadding: CGFloat?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case horizontalAlignment
        case useHorizontalMargins
        case leftPadding
        case rightPadding
        case verticalAlignment
        case useVerticalMargins
        case topPadding
        case bottomPadding
    }
    
    //--------------------------------------------------
    // MARK: - Subclassable
    //--------------------------------------------------
    
    /// Sets the default values. Should be called on init.
    open func setDefaults() { }
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public init() {
        setDefaults()
    }
    
    public init(horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil, useHorizontalMargins: Bool? = nil, leftPadding: CGFloat? = nil, rightPadding: CGFloat? = nil, useVerticalMargins: Bool? = nil, topPadding: CGFloat? = nil, bottomPadding: CGFloat? = nil) {
        self.horizontalAlignment = horizontalAlignment
        self.verticalAlignment = verticalAlignment
        self.useHorizontalMargins = useHorizontalMargins
        self.leftPadding = leftPadding
        self.rightPadding = rightPadding
        self.useVerticalMargins = useVerticalMargins
        self.topPadding = topPadding
        self.bottomPadding = bottomPadding
        setDefaults()
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------

    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        if let verticalAlignmentString = try typeContainer.decodeIfPresent(String.self, forKey: .verticalAlignment) {
            verticalAlignment = ContainerHelper.getAlignment(for: verticalAlignmentString)
        }
        if let horizontalAlignmentString = try typeContainer.decodeIfPresent(String.self, forKey: .horizontalAlignment) {
            horizontalAlignment = ContainerHelper.getAlignment(for: horizontalAlignmentString)
        }
        useHorizontalMargins = try typeContainer.decodeIfPresent(Bool.self, forKey: .useHorizontalMargins)
        leftPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .leftPadding)
        rightPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .rightPadding)
        useVerticalMargins = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalMargins)
        topPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .topPadding)
        bottomPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .bottomPadding)
        setDefaults()
    }
              
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(ContainerHelper.getAlignmentString(for: verticalAlignment), forKey: .verticalAlignment)
        try container.encodeIfPresent(ContainerHelper.getAlignmentString(for: horizontalAlignment), forKey: .horizontalAlignment)
        try container.encodeIfPresent(useHorizontalMargins, forKey: .useHorizontalMargins)
        try container.encodeIfPresent(leftPadding, forKey: .leftPadding)
        try container.encodeIfPresent(rightPadding, forKey: .rightPadding)
        try container.encodeIfPresent(useVerticalMargins, forKey: .useVerticalMargins)
        try container.encodeIfPresent(topPadding, forKey: .topPadding)
        try container.encodeIfPresent(bottomPadding, forKey: .bottomPadding)
    }
}
