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


open class NotificationModel: ContainerModel, 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
    }
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    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 closeButton: NotificationXButtonModel?
    public var style: NotificationModel.Style = .success
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(with headline: LabelModel) {
        self.headline = headline
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Default
    //--------------------------------------------------
    
    open override func setDefaults() {
        useHorizontalMargins = true
        useVerticalMargins = true
        topPadding = PaddingTwo
        bottomPadding = PaddingTwo

        if backgroundColor == nil {
            switch style {
            case .error:
                backgroundColor = Color(uiColor: .mvmOrange)
            case .warning:
                backgroundColor = Color(uiColor: .mvmYellow)
            case .information:
                backgroundColor = Color(uiColor: .mvmBlue)
            default:
                backgroundColor = Color(uiColor: .mvmGreen)
            }
        }
        if headline.textColor == nil {
            switch style {
            case .error, .warning:
                headline.textColor = Color(uiColor: .mvmBlack)
            default:
                headline.textColor = Color(uiColor: .mvmWhite)
            }
        }
        if body?.textColor == nil {
            switch style {
            case .error, .warning:
                body?.textColor = Color(uiColor: .mvmBlack)
            default:
                body?.textColor = Color(uiColor: .mvmWhite)
            }
        }
        
        button?.size = .tiny
        if button?.enabledTextColor == nil {
            switch style {
            case .error, .warning:
                button?.enabledTextColor = Color(uiColor: .mvmBlack)
            default:
                button?.enabledTextColor = Color(uiColor: .mvmWhite)
            }
        }
        if button?.enabledBorderColor == nil {
            switch style {
            case .error, .warning:
                button?.enabledBorderColor = Color(uiColor: .mvmBlack)
            default:
                button?.enabledBorderColor = Color(uiColor: .mvmWhite)
            }
        }
        if button?.style == nil {
            button?.style = .secondary
        }
        
        if closeButton?.color == nil {
            switch style {
            case .error, .warning:
                closeButton?.color = Color(uiColor: .mvmBlack)
            default:
                closeButton?.color = Color(uiColor: .mvmWhite)
            }
        }
    }
    
    //--------------------------------------------------
    // MARK: - Coding Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case headline
        case body
        case button
        case closeButton
        case style
    }
    
    //--------------------------------------------------
    // 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)
        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)
        closeButton = try typeContainer.decodeIfPresent(NotificationXButtonModel.self, forKey: .closeButton)
        if let style = try typeContainer.decodeIfPresent(NotificationModel.Style.self, forKey: .style) {
            self.style = style
        }
        super.init()
    }

    open override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        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(closeButton, forKey: .closeButton)
        try container.encode(style, forKey: .style)
    }
}
