// Copyright © 2022 Olo Inc. All rights reserved.
// This software is made available under the Olo Pay SDK License (See LICENSE.md file)
//
//  OloPayViewProtocol.swift
//  OlopaysdkReactNative
//
//  Created by Justin Anderson on 8/23/23.
//  Copyright © 2023 Facebook. All rights reserved.
//

import Foundation

protocol OloPayViewProtocol : NSObjectProtocol {
    
    // Must be implemented by concrete subclasses
    func updateAllProperties()
    func createViewController() -> UIViewController
    
    // Default implementations provided in OloPayView
    var viewIsDirty: Bool { get set }
    var viewController: UIViewController? { get set }
    
    // Default implementations provided in protocol extensions
    func embed(view: UIView, parentViewController: UIViewController?)
    
}

extension OloPayViewProtocol {
    func embed(view: UIView, parentViewController: UIViewController?) {
        guard let parent = parentViewController else {
            return
        }
        
        //Find the VC that is used by React Native and add our view and view controller to it
        let newViewController = createViewController()
        parent.addChild(newViewController)
        view.addSubview(newViewController.view)
        newViewController.view.frame = view.bounds
        newViewController.didMove(toParent: parent)
        viewController = newViewController
    }
}
