//
//  RDSControlWebVC.m
//  Temperature
//  
//  Created by Kevin on 2023/12/6.


#import "RDSControlWebVC.h"
#import <WebViewJavascriptBridge/WebViewJavascriptBridge.h>
#import <WebKit/WebKit.h>

@interface RDSControlWebVC () <WKUIDelegate, WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) WebViewJavascriptBridge *bridge;

@property (nonatomic, copy) NSString *requestURL;

@end

@implementation RDSControlWebVC

- (instancetype)initWithRecordId:(NSString *)recordId code:(NSString *)code {
    self = [super init];
    if (self) {
        self.requestURL = [NSString stringWithFormat:@"https://app.yongxulvjian.com/#/deviceManage/%@/%@", recordId, code];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configSubviews];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestURL]]];
    [RDSHudShower showWithStatus:nil];
}

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

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

- (void)configSubviews {
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            } else {
                make.top.equalTo(self.mas_topLayoutGuide);
            }
        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);
    }];
}

- (void)jsBridge {
    RDS_WEAKSELF(weakSelf)
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
    [self.bridge setWebViewDelegate:self];
    
    // js 回调
    [self.bridge registerHandler:@"closeWebView" handler:^(id data, WVJBResponseCallback responseCallback) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (WKWebView *)webView {
    if (!_webView) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        config.userContentController = [[WKUserContentController alloc] init];
        config.allowsInlineMediaPlayback = YES;
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
        _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        [self jsBridge];
    }
    return _webView;
}

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

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

@end
