//
//  BaseListItemModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 2/12/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//
//  A base class that has common list item boilerplate model stuffs.


@objcMembers open class ListItemModel: ContainerModel, ListItemModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var backgroundColor: Color?
    public var action: ActionModelProtocol?
    public var hideArrow: Bool?
    public var line: LineModel?
    public var style: ListItemStyle?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case backgroundColor
        case action
        case hideArrow
        case line
        case style
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// Defaults to set
    open override func setDefaults() {
        setByStyle()
    }
    
    /// Convenience function to set common values.
    open func set(useHorizontalMargins: Bool? = true, useVerticalMargins: Bool? = true, topPadding: CGFloat? = nil, bottomPadding: CGFloat? = nil) {
        if self.useHorizontalMargins == nil {
            self.useHorizontalMargins = useHorizontalMargins
        }
        if self.useVerticalMargins == nil {
            self.useVerticalMargins = useVerticalMargins
        }
        if self.topPadding == nil {
            self.topPadding = topPadding
        }
        if self.bottomPadding == nil {
            self.bottomPadding = bottomPadding
        }
    }
    
    /// Convenience function to set common values based on style. We default to standard if not set.
    open func setByStyle() {
        let style = self.style ?? .standard
        switch style {
        case .standard:
            set(topPadding: Padding.Component.VerticalMarginSpacing, bottomPadding: Padding.Component.VerticalMarginSpacing)
        case .shortDivider:
            set(topPadding: Padding.Component.LargeVerticalMarginSpacing, bottomPadding: Padding.Four)
        case .tallDivider:
            set(topPadding: Padding.Twelve, bottomPadding: Padding.Four)
        case .sectionFooter:
            set(topPadding: Padding.Component.VerticalMarginSpacing, bottomPadding: 0)
        case ListItemStyle.none:
            set(topPadding: 0, bottomPadding: 0)
        }
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(style: ListItemStyle? = .standard, action: ActionModelProtocol?) {
        self.style = style
        self.action = action
        super.init()
    }
    
    public override 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) {
        super.init(horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, useHorizontalMargins: useHorizontalMargins, leftPadding: leftPadding, rightPadding: rightPadding, useVerticalMargins: useVerticalMargins, topPadding: topPadding, bottomPadding: bottomPadding)
    }
    
    public override init() {
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        action = try typeContainer.decodeModelIfPresent(codingKey: .action)
        hideArrow = try typeContainer.decodeIfPresent(Bool.self, forKey: .hideArrow)
        line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line)
        style = try typeContainer.decodeIfPresent(ListItemStyle.self, forKey: .style)
        try super.init(from: decoder)
    }
    
    open override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeModelIfPresent(action, forKey: .action)
        try container.encodeIfPresent(hideArrow, forKey: .hideArrow)
        try container.encodeIfPresent(line, forKey: .line)
        try container.encodeIfPresent(style, forKey: .style)
    }
}
