//
//  RDSUserProtocolViewController.m
//  SmartLightForBigFish
//
//  Created by coderYK on 2017/11/22.
//  Copyright © 2017年 RD. All rights reserved.
//

#import "RDSUserProtocolViewController.h"
#import <WebKit/WebKit.h>

@interface RDSUserProtocolViewController () <WKUIDelegate, WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;

@end

@implementation RDSUserProtocolViewController

- (void)dealloc {
//    WKUserContentController *controller = self.webView.configuration.userContentController;
//    [controller removeScriptMessageHandlerForName:@"closePage"];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = _vcTitle;
    [self p_setupUI];

    [self p_loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)p_loadData {
    /* 加载服务器url的方法*/
    NSString *url = self.url;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [RDSHudShower showWithStatus:nil];
    [_webView loadRequest:request];
}

- (void)p_setupUI {
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
//    [configuration.userContentController addScriptMessageHandler:self name:@"closePage"];
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];

    // 放在AppDelegate中全局修改
    //    [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
    //        NSString *newUserAgent = [result stringByAppendingFormat:@"/%@", @"yongxulvjian"];
    //        [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
    //    }];

    [self.view addSubview:_webView];
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view);
//        make.right.equalTo(self.view);
//        make.top.equalTo(self.view);
//        if (@available(iOS 11.0, *)) {
//            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
//        } else {
//            make.bottom.equalTo(self.view.mas_bottom);
//        }
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.allowsBackForwardNavigationGestures = YES;
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [RDSHudShower dismissHUD];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [RDSHudShower dismissHUD];
}

//- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
//    if ([message.name isEqualToString:@"closePage"]) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}

@end
