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

open class NotificationMoleculeModel: MoleculeModelProtocol {

    /**
     The style of the notification:
     - success, green background, white content
     - error, orange background, black content
     - \warning, yellow background, black content
     - information, blue background, white content
     */
    public enum Style: String, Codable {
        case success
        case error
        case warning
        case information
        
        var toVDSStyle: VDS.Notification.Style {
            switch self {
            case .success:
                    .success
            case .error:
                    .error
            case .warning:
                    .warning
            case .information:
                    .info
            }
        }
    }
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public var id: String = UUID().uuidString
    public class var identifier: String { "notification" }
    public var accessibilityIdentifier: String?
    public var backgroundColor: Color?
    public var headline: LabelModel
    public var body: LabelModel?
    public var button: ButtonModel?
    public var secondaryButton: ButtonModel?
    public var closeButton: NotificationXButtonModel?
    public var style: Style = .success
    public var inverted: Bool = false
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(with headline: LabelModel, style: NotificationMoleculeModel.Style = .success, backgroundColor: Color? = nil, body: LabelModel? = nil, button: ButtonModel? = nil, secondaryButton: ButtonModel? = nil, closeButton: NotificationXButtonModel? = nil) {
        self.headline = headline
        self.style = style
        self.backgroundColor = backgroundColor
        self.body = body
        self.button = button
        self.secondaryButton = secondaryButton
        self.closeButton = closeButton
    }
    
    //--------------------------------------------------
    // MARK: - Coding Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case headline
        case body
        case button
        case secondaryButton
        case closeButton
        case inverted
        case style
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        headline = try typeContainer.decode(LabelModel.self, forKey: .headline)
        body = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .body)
        button = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .button)
        secondaryButton = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .secondaryButton)
        closeButton = try typeContainer.decodeIfPresent(NotificationXButtonModel.self, forKey: .closeButton)
        inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) ?? false
        if let style = try typeContainer.decodeIfPresent(NotificationMoleculeModel.Style.self, forKey: .style) {
            self.style = style
        }
    }

    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(headline, forKey: .headline)
        try container.encodeIfPresent(body, forKey: .body)
        try container.encodeIfPresent(button, forKey: .button)
        try container.encodeIfPresent(secondaryButton, forKey: .secondaryButton)
        try container.encodeIfPresent(closeButton, forKey: .closeButton)
        try container.encodeIfPresent(inverted, forKey: .inverted)
        try container.encode(style, forKey: .style)
    }
}

extension NotificationMoleculeModel {
    public var surface: Surface {
        inverted ? .dark : .light
    }
}
