//
//  StaticJSBridgeManager.m
//  suite-ios-h5
//
//  Created by junmo on 2018/12/18.
//  Copyright © 2018年 junmo. All rights reserved.
//
#if __has_include(<WindVane/WindVane.h>)
#import <WindVane/WindVane.h>
#else
#endif
#import "StaticJSBridgeManager.h"

#if __has_include(<WindVane/WindVane.h>)
static WVBridgeHandler handler1;
static WVBridgeHandler handler2;
static WVBridgeHandler handler3;
#else
#endif

static NSString *staticHandlerClassName = @"StaticJSBridge";
static NSString *handlerName1;
static NSString *handlerName2;
static NSString *handlerName3;
static NSString *handlerName4;

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

+ (void)load {
    handler1 = ^(NSDictionary * params, id<WVBridgeCallbackContext> context) {
        NSLog(@"global handler.");
        [context callbackSuccess:nil];
    };
    
    handler2 = ^(NSDictionary * params, id<WVBridgeCallbackContext> context) {
        NSLog(@"webView handler.");
        [context callbackSuccess:nil];
    };
    
    handler3 = ^(NSDictionary * params, id<WVBridgeCallbackContext> context) {
        NSLog(@"webView controller handler.");
        [context callbackSuccess:nil];
    };
    
    handlerName1 = [NSString stringWithFormat:@"%@.handler1", staticHandlerClassName];
    handlerName2 = [NSString stringWithFormat:@"%@.handler2", staticHandlerClassName];
    handlerName3 = [NSString stringWithFormat:@"%@.handler3", staticHandlerClassName];
    handlerName4 = [NSString stringWithFormat:@"%@.handler4", staticHandlerClassName];
    
    [self registerJSBridgeToGlobal];
    [self registerJSBridgeAlias];
}

+ (void)registerJSBridgeToGlobal {
    // 注册JSBRidge到全局
    WVBridgeRegisterHandler(handlerName1, handler1);
}

+ (void)registerJSBridgeToWebView:(UIView<WVWebViewProtocol> *)webview {
    [webview registerHandler:handlerName2 withBlock:handler2];
}

+ (void)registerJSBridgeToWVWebViewController:(WVWebViewController *)wvVC {
    [wvVC registerHandler:handlerName3 withBlock:handler3];
}

// 注册JSBRidge全局别名，必须配合全局JSBRidge使用
+ (void)registerJSBridgeAlias {
    NSDictionary *alias = @{
                            handlerName4: handlerName1
                            };
    WVBridgeRegisterAlias(alias);
}
#else
#endif
@end

