//
//  ListNotificationAuthModel.swift
//  MVMCoreUI
//
//  Created by Edayattu Salam, Nowfal on 13/04/23.
//  Copyright © 2023 Verizon Wireless. All rights reserved.
//

import Foundation

public class ListNotificationAuthModel: ListOneColumnFullWidthTextAllTextAndLinksModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public class override var identifier: String { "listNotificationAuth" }
    public var enableStatus: String
    public var disableStatus: String
    public var enableAction: ActionModelProtocol?
    public var disableAction: ActionModelProtocol?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    private enum CodingKeys: String, CodingKey {
        case enableStatus
        case disableStatus
        case enableAction
        case disableAction
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        enableStatus = try typeContainer.decode(String.self, forKey: .enableStatus)
        disableStatus = try typeContainer.decode(String.self, forKey: .disableStatus)
        enableAction = try typeContainer.decodeModelIfPresent(codingKey: .enableAction)
        disableAction = try typeContainer.decodeModelIfPresent(codingKey: .disableAction)
        try super.init(from: decoder)
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(enableStatus, forKey: .enableStatus)
        try container.encode(disableStatus, forKey: .disableStatus)
        try container.encodeModelIfPresent(enableAction, forKey: .enableAction)
        try container.encodeModelIfPresent(disableAction, forKey: .disableAction)
    }
}

extension ListNotificationAuthModel: PageBehaviorProtocolRequirer {
    public func getRequiredBehaviors() -> [PageBehaviorModelProtocol] {
        [GetNotificationAuthStatusBehaviorModel()]
    }
}

extension ListNotificationAuthModel: GetNotificationAuthStatusBehaviorConsumerProtocol {
    public func consume(notificationStatus: UNAuthorizationStatus) {
        if notificationStatus == .authorized {
            body?.text = enableStatus
            action = enableAction
        } else {
            body?.text = disableStatus
            action = disableAction
        }
    }
}
