//
//  ActionOpenPanelHandler.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 7/18/22.
//  Copyright © 2022 Verizon Wireless. All rights reserved.
//

import Foundation
import MVMCore

/// Shows the panel.
open class ActionOpenPanelHandler: MVMCoreJSONActionHandlerProtocol {
    required public init() {}
    
    open func performAction(with JSON: [AnyHashable : Any], model: ActionModelProtocol, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) async throws {
        guard let model = model as? ActionOpenPanelModel else { return }
        switch model.panel {
        case .left, .menu:
            await MVMCoreUISplitViewController.main()?.leftPanel?.willOpen?(withActionInformation: JSON, additionalData: additionalData)
            await MVMCoreUISplitViewController.main()?.showLeftPanel(animated: true)
        case .right, .support:
            await MVMCoreUISplitViewController.main()?.rightPanel?.willOpen?(withActionInformation: JSON, additionalData: additionalData)
            await MVMCoreUISplitViewController.main()?.showRightPanel(animated: true)
        }
    }
    
    open func execute(with model: ActionModelProtocol, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) async throws {
        guard let model = model as? ActionOpenPanelModel else { return }
        let json = try MVMCoreActionHandler.convertActionToJSON(model)
        try await performAction(with: json, model: model, delegateObject: delegateObject, additionalData: additionalData)
    }
}
