//
//  ModalMoleculeListTemplate.swift
//  MVMCoreUI
//
//  Created by Ryan on 3/6/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


open class ModalMoleculeListTemplate: MoleculeListTemplate {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var closeButton: Button?
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    // For subclassing the model.
    open override func decodeTemplate(using decoder: JSONDecoder, from data: Data) throws -> ModalListPageTemplateModel {
        try decoder.decode(ModalListPageTemplateModel.self, from: data)
    }
    
    override open func handleNewData() {
        super.handleNewData()
        
        closeButton = MVMCoreUICommonViewsUtility.addCloseButton(to: view, action: { [weak self] _ in
            guard let self = self else { return }
            let closeAction = (self.templateModel as? ModalListPageTemplateModel)?.closeAction ??
                ActionBackModel()
            MVMCoreActionHandler.shared()?.asyncHandleAction(with: closeAction, additionalData: nil, delegateObject: self.delegateObjectIVar)
        })
    }
    
    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        accessibilityElements = [closeButton as Any, tableView as Any]
    }
}
