import Toast_Swift

@objc(XcodeTemplateProject)

class XcodeTemplateProject: RCTEventEmitter {
    
    @objc(nativeNormalMethod)
    func nativeNormalMethod() {
        DispatchQueue.main.async {
            UIApplication.shared.delegate?.window??.makeToast("调用了Native方法")
        }
    }
    
    @objc(nativeMethodWithParams:withNumB:)
    func nativeMethodWithParams(numA: Int, numB: Int) {
        let result = numA + numB
        let str = "JS传递参数到Native方法结果为\(result)"
        DispatchQueue.main.async {
            UIApplication.shared.delegate?.window??.makeToast("\(str)")
        }
    }
    
    @objc(nativeMethodWithCallback:withNumB:withSuccessCallback:withErrorCallback:)
    func nativeMethodWithCallback(numA: Int, numB: Int, successCallback: RCTResponseSenderBlock, errorCallback: RCTResponseSenderBlock) {
        let result = numA + numB
        successCallback([result]);
    }
    
    @objc(nativeMethodWithPromise:withNumB:withResolver:withRejecter:)
    func nativeMethodWithPromise(numA: Int, numB: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
        let result = numA + numB
        let dic = ["numA": numA, "numB": numB, "result": result]
        resolve(dic)
    }
    
    @objc(nativeSendCustomEvent)
    func nativeSendCustomEvent() {
        sendEvent(withName: "MessageEvent", body: ["key":"this is a writeable message"])
    }
    
    override init() {
        super.init()
    }
    
    override func supportedEvents() -> [String]! {
        return ["MessageEvent"]
    }
}
