//
//  FireworkSdk+CTA.swift
//  react-native-firework-sdk
//
//  Created by Jeff Zheng on 2021/12/21.
//

import FireworkVideo
import Foundation

extension FireworkSDKModule: FireworkVideoCTADelegate {
    func handleCustomCTAClick(_ viewController: PlayerViewController, url: URL, for video: VideoDetails) -> Bool {
        let callbackId = UUID().uuidString
        playerMap.setObject(viewController, forKey: callbackId as NSString)

        let body: [String: Any?] = [
            "url": url.absoluteString,
            "callbackId": callbackId,
            "video": video.jsObject
        ]

        self.sendEvent(withName: FWEventName.customCTAClick.rawValue,
                  body: body)

        return customCTAClickEnabled
    }

    @objc(pausePlayer:)
    func pausePlayer(_ callbackId: String) {
        if let playerViewController = playerMap.object(forKey: callbackId as NSString),
           let playerViewController = playerViewController as? PlayerViewController {
            playerViewController.pause()
        }
    }

    @objc(resumePlayer:)
    func resumePlayer(_ callbackId: String) {
        if let playerViewController = playerMap.object(forKey: callbackId as NSString),
           let playerViewController = playerViewController as? PlayerViewController {
            playerViewController.resume()
        }
    }
}
