//
//  Header.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 10/3/19.
//  Copyright © 2019 Suresh, Kamlesh. All rights reserved.
//


@objcMembers public class HeaderModel: ContainerModel {
    public var backgroundColor: Color?
    public var line: LineModel?
    
    private enum CodingKeys: String, CodingKey {
        case line
        case backgroundColor
    }
    
    /// Defaults to set
    public override func setDefaults() {
        if useHorizontalMargins == nil {
            useHorizontalMargins = true
        }
        if useVerticalMargins == nil {
            useVerticalMargins = true
        }
        if topPadding == nil {
            topPadding = PaddingDefaultVerticalSpacing
        }
        if bottomPadding == nil {
            bottomPadding = PaddingDefaultVerticalSpacing
        }
        if line == nil {
            line = LineModel(type: .heavy)
        }
    }
    
    public override init() {
        super.init()
    }

    required public init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(line, forKey: .line)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
    }
}
