//
//  ACBridgeWKWebView.m
//  iOSJSBridgeDemo
//
//  Created by 罗坚(EX-LUOJIAN004) on 2018/4/28.
//  Copyright © 2018年 罗坚(EX-LUOJIAN004). All rights reserved.
//

#import "ACBridgeWKWebView.h"
#import "ACJSImplement.h"

@interface ACBridgeWKWebView () <WKNavigationDelegate>

@property (nonatomic, weak) id<WKNavigationDelegate> outsideDelegate;
@property (nonatomic, strong) ACJSImplement *jsImplement;

@end

@implementation ACBridgeWKWebView

- (id)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
{
    self = [super initWithFrame:frame configuration:configuration];
    if (self) {
        [self initDelegate];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initDelegate];
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self initDelegate];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    
    if (self){
        [self initDelegate];
    }
    
    return self;
}

- (void)initDelegate
{
    [super setNavigationDelegate:self];
}

#pragma mark - WebView Delegate

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if ([self.jsImplement handleAppCoreURL:[navigationAction.request URL]] == NO) {
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
        return;
    }
    else {
        decisionHandler(WKNavigationActionPolicyAllow);
        return;
    }
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView decidePolicyForNavigationResponse:navigationResponse decisionHandler:decisionHandler];
        return;
    }
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didStartProvisionalNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didReceiveServerRedirectForProvisionalNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didFailProvisionalNavigation:navigation withError:error];
    }
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didCommitNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
    [self.jsImplement injectPluginJS];
    [self.jsImplement injectWebPlusJS];

    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didFinishNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didFailNavigation:navigation withError:error];
    }
}

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webView:webView didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler];
    }
}

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
{
    if (self.outsideDelegate && self.outsideDelegate != self && [self.outsideDelegate respondsToSelector:_cmd]) {
        [self.outsideDelegate webViewWebContentProcessDidTerminate:webView];
    }
}

#pragma mark - getter and setter

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

- (void)setNavigationDelegate:(id<WKNavigationDelegate>)navigationDelegate
{
    self.outsideDelegate = navigationDelegate;
}

- (id<WKNavigationDelegate>)navigationDelegate
{
    return self.outsideDelegate;
}

- (ACJSImplement *)jsImplement
{
    if (_jsImplement == nil) {
        _jsImplement = [[ACJSImplement alloc] init];
        _jsImplement.webView = self;
        _jsImplement.vc = self.vc;
    }
    return _jsImplement;
}

@end
