//
//  AlertModel.swift
//  MVMCore
//
//  Created by Suresh, Kamlesh on 7/14/20.
//  Copyright © 2020 myverizon. All rights reserved.
//

import UIKit
import MVMCore


public class AlertButtonModel: Codable {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var title: String
    public var action: ActionModelProtocol
    public var style: UIAlertAction.Style = .default
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(_ title: String,_ action: ActionModelProtocol,_ style: UIAlertAction.Style = .default) {
        self.title = title
        self.action = action
        self.style = style
    }

    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case title
        case action
        case style
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        title = try typeContainer.decode(String.self, forKey: .title)

        if let style = try typeContainer.decodeIfPresent(String.self, forKey: .style) {
            self.style = UIAlertAction.Style(rawValue: style)
        }
        action = try typeContainer.decodeModel(codingKey: .action)
    }

    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(title, forKey: .title)
        try container.encode(style.rawValueString, forKey: .style)
        try container.encodeModel(action, forKey: .action)
    }
}

public class AlertModel: Codable {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var title: String
    public var message: String
    public var style: UIAlertController.Style = .alert
    public var alertActions: [AlertButtonModel]
    public var analyticsData: JSONValueDictionary?
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public init(_ title: String,_ message: String,_ alertActions: [AlertButtonModel]) {
        self.title = title
        self.message = message
        self.alertActions = alertActions
    }

    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case title
        case message
        case alertActions
        case style
        case analyticsData
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        title = try typeContainer.decode(String.self, forKey: .title)
        message = try typeContainer.decode(String.self, forKey: .message)
        alertActions = try typeContainer.decode([AlertButtonModel].self, forKey: .alertActions)
        analyticsData = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .analyticsData)

        if let style = try typeContainer.decodeIfPresent(String.self, forKey: .style) {
            self.style = UIAlertController.Style(rawValue: style)
        }
    }

    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(title, forKey: .title)
        try container.encode(message, forKey: .message)
        try container.encode(alertActions, forKey: .alertActions)
        try container.encode(style.rawValueString, forKey: .style)
        try container.encodeIfPresent(analyticsData, forKey: .analyticsData)
    }
}
