//
//  SuiteWVWebViewController.m
//  SuiteApp
//
//  Created by junmo on 2018/12/18.
//  Copyright © 2018年 微博@iOS程序犭袁. All rights reserved.
//
#if __has_include(<WindVane/WindVane.h>)
#import <WindVane/WindVane.h>
#else
#endif

#import "SuiteWVWebViewController.h"
#import "StaticJSBridgeManager.h"

static NSString *observeKeyPathTitle = @"title";
static NSString *observeKeyPathEstimatedProgress = @"estimatedProgress";

@interface SuiteWVWebViewController ()

#if __has_include(<WindVane/WindVane.h>)
@property (nonatomic, strong) UIView<WVWebViewProtocol> *webView;
#else
@property (nonatomic, strong) UIView *webView;
#endif
@end

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"WindVane演示";
    [self configWebView];
    [self.view addSubview:_webView];
    //[self loadURL:@"http://cdn.emas-poc.com/app/yanpeicpf-aaa/index.html"];
    //[self loadURL:@"https://www.aliyun.com"];
    [self loadURL:[[self localFileURL] absoluteString]];
}

- (void)configWebView {
    // 创建时声明大小
    _webView = [[WVWebView alloc] initWithFrame:self.view.bounds];
    _webView.sourceViewController = self;
    _webView.openLocalService = YES;
    [self addWebViewObserver];
    [StaticJSBridgeManager registerJSBridgeToWebView:_webView];
}

- (NSURL *)localFileURL {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *localURL = [NSURL fileURLWithPath:path];
    return localURL;
}

// 监听页面信息，添加KVO监听，务必在WebView销毁前移除监听
- (void)addWebViewObserver {
    [_webView addObserver:self forKeyPath:observeKeyPathTitle options:NSKeyValueObservingOptionNew context:nil];
    [_webView addObserver:self forKeyPath:observeKeyPathEstimatedProgress options:NSKeyValueObservingOptionNew context:nil];
}

// 处理KVO监听
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:observeKeyPathTitle]) {
        NSLog(@"title change to: %@", change[NSKeyValueChangeNewKey]);
    } else if ([keyPath isEqualToString:observeKeyPathEstimatedProgress]) {
        NSLog(@"progress change to: %@", change[NSKeyValueChangeNewKey]);
    }
}

- (void)loadURL:(NSString *)url {
    [_webView loadURL:url];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_webView bindingWebViewService];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_webView releaseWebViewService];
}

- (void)dealloc {
    [_webView removeObserver:self forKeyPath:observeKeyPathTitle];
    [_webView removeObserver:self forKeyPath:observeKeyPathEstimatedProgress];
    _webView = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#else
#endif
@end


