//
//  MyJSBridge.m
//  suite-ios-h5
//
//  Created by junmo on 2018/12/13.
//  Copyright © 2018年 junmo. All rights reserved.
//

#import "MyJSBridge.h"

@implementation MyJSBridge
#if __has_include(<WindVane/WindVane.h>)

/**
 自定义声明周期
 针对实例方法，默认每次JSBridge调用都会创建一个新的实例，该实例被强引用保证不会被错误释放，直到WebView被销毁再释放。
 支持4种自定义生命周期：
 - WVBridgeScopeInvocation，每次调用一个实例；
 - WVBridgeScopePage，每个页面一个实例；
 - WVBridgeScopeView，每个View一个实例；
 - WVBridgeScopeStatic，全局唯一实例。
 
 ScopeInvocation 和 ScopePage，实例的引用会持续到页面被切换（例如 WebView 跳转新页面、回退历史记录等）。
 ScopeView，实例的引用会持续到 View 被销毁。
 确保当前实例可被销毁时，建议主动调用 [context releaseHandler:self] 立刻释放当前实例，以节约内存。
 ScopeStatic 的实例，会一直存在，但也支持通过 [context releaseHandler:self] 主动释放。
 */
+ (WVBridgeScope)instanceScope {
    return WVBridgeScopeInvocation;
}

/**
 * 重置处理器 - 该方法将会在加载新页面时调用，可以做一些清理工作。
 *
 * @param context WVBridge 被重置时的上下文。
 * @param request 要加载的新页面。
 */
- (void)resetWithContext:(id<WVBridgeContext>)context withNextRequest:(NSURLRequest *)request {
    
}
/**
 * 暂停处理器 - 该方法将会在 UIViewController viewWillDisappear 时调用，用于降低 WVBridge API 的性能消耗，例如暂停播放音乐和持续性监听器等。
 * 不会对 WVBridgeScopeStatic 作用域的 WVBridge 调用。
 *
 * @param context WVBridge 被暂停时的上下文。
 */
- (void)pauseWithContext:(id<WVBridgeContext>)context {
}
/**
 * 恢复处理器 - 该方法将会在 UIViewController viewWillAppear 时调用，用于恢复 WVBridge API 的性能，例如恢复播放音乐和持续性监听器等。
 * 不会对 WVBridgeScopeStatic 作用域的 WVBridge 调用。
 *
 * @param context WVBridge 被恢复时的上下文。
 */
- (void)resumeWithContext:(id<WVBridgeContext>)context {
}

// 这是一个静态方法，直接调用。
+ (void)firstAPI:(NSDictionary *)params withWVBridgeContext:(id<WVBridgeCallbackContext>)context {
    // Do something
    NSLog(@"call firstAPI");
    [context callbackSuccess:nil];
}
// 这时一个实例方法，会创建一个新实例，然后再调用。
- (void)secondAPI:(NSDictionary *)params withWVBridgeContext:(id<WVBridgeCallbackContext>)context {
    // Do something
    NSLog(@"call secondAPI");
    [context callbackSuccess:nil];
}

+ (void)nativeCallJSBridge:(NSDictionary *)params withWVBridgeContext:(id<WVBridgeCallbackContext>)context {
    NSLog(@"call another api from JSBridge.");
    [WVBridge callHandler:@"MyJSBridge.firstAPI" withParams:nil withCallback:^(NSString * _Nonnull ret, NSDictionary * _Nullable result) {
        NSLog(@"native call JSBridge success.");
    }];
    [context callbackSuccess:nil];
}
#else
#endif
@end

