//
//  CollapsableNotificationModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/15/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

open class CollapsableNotificationModel: NotificationModel {
    public class override var identifier: String {
        return "collapsableNotification"
    }
    public var topLabel: LabelModel
    public var topAction: ActionModelProtocol?
    public var alwaysShowTopLabel = false
    public var collapseTime: Int = 5
    public var initiallyCollapsed = false
    public var pages: [String]?
    
    public init(with topLabel: LabelModel, headline: LabelModel) {
        self.topLabel = topLabel
        super.init(with: headline)
    }
    
    open override func setDefaults() {
        super.setDefaults()
        if topLabel.numberOfLines == nil {
            topLabel.numberOfLines = 1
        }
        if topLabel.textColor == nil {
            topLabel.textColor = Color(uiColor: .white)
        }
        if topLabel.textAlignment == nil {
            topLabel.textAlignment = .center
        }
    }
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case topLabel
        case topAction
        case alwaysShowTopLabel
        case collapseTime
        case initiallyCollapsed
        case pages
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        topLabel = try typeContainer.decode(LabelModel.self, forKey: .topLabel)
        topAction = try typeContainer.decodeModelIfPresent(codingKey: .topAction)
        if let alwaysShowTopLabel = try typeContainer.decodeIfPresent(Bool.self, forKey: .alwaysShowTopLabel) {
            self.alwaysShowTopLabel = alwaysShowTopLabel
        }
        if let collapseTime = try typeContainer.decodeIfPresent(Int.self, forKey: .collapseTime) {
            self.collapseTime = collapseTime
        }
        if let initiallyCollapsed = try typeContainer.decodeIfPresent(Bool.self, forKey: .initiallyCollapsed) {
            self.initiallyCollapsed = initiallyCollapsed
        }
        pages = try typeContainer.decodeIfPresent([String].self, forKey: .pages)
        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.encode(moleculeName, forKey: .moleculeName)
        try container.encode(topLabel, forKey: .topLabel)
        try container.encodeModelIfPresent(topAction, forKey: .topAction)
        try container.encode(alwaysShowTopLabel, forKey: .alwaysShowTopLabel)
        try container.encode(collapseTime, forKey: .collapseTime)
        try container.encode(initiallyCollapsed, forKey: .initiallyCollapsed)
        try container.encodeIfPresent(pages, forKey: .pages)
    }
}
