//
//  ACWebViewJSBridge.m
//  iOSJSBridgeDemo
//
//  Created by 罗坚(EX-LUOJIAN004) on 2018/5/21.
//  Copyright © 2018年 罗坚(EX-LUOJIAN004). All rights reserved.
//

#import "ACJSImplement.h"
#import "ACBaseJSConstant.h"
#import "ACPluginJSConstant.h"
#import "ACWebPlusJSConstant.h"
#import "ACPluginJSBridge.h"
#import "ACBridgeUtil.h"
#import "ACWebPlusJSBridge.h"

@interface ACJSImplement ()

// 基础JS是否注入
@property (nonatomic, assign, getter=isBaseJSInject) BOOL baseJSInject;
// 插件Bridge处理
@property (nonatomic, strong) ACPluginJSBridge *pluginBridge;
// web增强Bridge处理
@property (nonatomic, strong) ACWebPlusJSBridge *webPlusBridge;

@end

@implementation ACJSImplement

- (void)injectBaseJS
{
    if (self.isBaseJSInject == YES) {
        return;
    }
    self.baseJSInject = YES;
    // 注入js和native交互的js代码.
    [ACBridgeUtil webView:self.webView callJsWithString:[kACJSInjectBasic copy]];
}

- (void)injectPluginJS
{
    [self injectBaseJS];
    [ACBridgeUtil webView:self.webView callJsWithString:[kACJSInjectPlugin copy]];
}

- (void)injectWebPlusJS
{
    [self injectBaseJS];
    //注入web增强JS识别
    //注入方法转url 方法列表 JS
    NSMutableString *injectionMethods = [NSMutableString new];
    NSArray *handlesAr = [self.webPlusBridge.handlesDic allKeys];
    for (int i = 0; i < [handlesAr count]; i++) {
        if (i != 0) {
            [injectionMethods appendString:@"\""];
        }
        [injectionMethods appendString:handlesAr[i]];

        if (i != [handlesAr count] - 1){
            [injectionMethods appendString:@"\""];
            [injectionMethods appendString:@", "];
        }
    }
    NSString *injectWebPlusFuncConvertStr = [NSString stringWithFormat:[kACJSInjectWebPlusFormat copy],injectionMethods];
    [ACBridgeUtil webView:self.webView callJsWithString:injectWebPlusFuncConvertStr];

    //点击图片全屏JS
    [ACBridgeUtil webView:self.webView callJsWithString:[kACJSInjectClickImage copy]];
    [ACBridgeUtil webView:self.webView callJsWithString:[kACJSCallImageConvert copy]];
    [ACBridgeUtil webView:self.webView callJsWithString:[kACJSGetImages copy] completionHandler:^(id obj) {
        if ([obj isKindOfClass:[NSString class]]) {
            NSString *imagesStr = (NSString *)obj;
            NSMutableArray<NSString *> *imageAr = [NSMutableArray arrayWithArray:[imagesStr componentsSeparatedByString:@"+"]];
            if (imageAr && imageAr.count >= 2) {
                [imageAr removeLastObject];
                self.webPlusBridge.imageAr = imageAr;
            }
        }
    }];
}

- (BOOL)handleAppCoreURL:(NSURL *)url
{
    NSString *urlStr = [url absoluteString];
    NSArray *components = [urlStr componentsSeparatedByString:@":"];
    NSString *interfaceStr = components[0];

    // 图片点击预览
    if ([interfaceStr isEqualToString:[kACJSBridgeClickImageScheme copy]]) {
        NSString* path = [url.absoluteString substringFromIndex:[kACJSBridgeClickImageScheme length]+1];
        path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [self.webPlusBridge previewPictureImage:path];
        return NO;
    }

    // 取得参数Interface Obj Function Message
    if ([components count] > 3){
        NSString *objStr = components[1];
        NSString *functionStr = components[2];
        NSString *argsString = [components[3] length] ? [components[3] stringByRemovingPercentEncoding] : nil;
        
        // 插件scheme
        if ([interfaceStr isEqualToString:[kACJSBridgePluginScheme copy]]) {
            [self.pluginBridge execPlugWithPlugName:objStr Function:functionStr Message:argsString];
            return NO;
        }
        // web增强 scheme
        else if ([interfaceStr isEqualToString:[kACJSBridgeWebPlusScheme copy]]) {
            ACMessage *retMsg = [self.webPlusBridge callHandler:functionStr withParam:[ACBridgeUtil convertStringToMessage:argsString]];
            
            if (retMsg != nil) {
                // native调用JS，回传返回值
                [ACBridgeUtil webView:self.webView callJsWithMessage:retMsg];
            }
            return NO;
        }
    }
    return YES;
}

#pragma mark - getter and setter

- (void)setVc:(UIViewController *)vc
{
    _vc = vc;
    self.pluginBridge.vc = vc;
    self.webPlusBridge.vc = vc;
}

- (ACPluginJSBridge *)pluginBridge
{
    if (_pluginBridge == nil) {
        _pluginBridge = [[ACPluginJSBridge alloc] init];
        _pluginBridge.webView = self.webView;
    }
    return _pluginBridge;
}

- (ACWebPlusJSBridge *)webPlusBridge
{
    if (_webPlusBridge == nil) {
        _webPlusBridge = [[ACWebPlusJSBridge alloc] init];
        _webPlusBridge.webView = self.webView;
    }
    return _webPlusBridge;
}

@end
