//
//  TourOptionViewController.m
//  TakeTours
//
//  Created by Vae on 2019/3/1.
//

#import "TourOptionViewController.h"
#import "SDK.h"
#import <dsBridge/dsbridge.h>
#import <YYModel/YYModel.h>
#import <libextobjc/EXTScope.h>
#import <BlocksKit/BlocksKit.h>
#import "Vars.h"
#import "JSTourSiteBridge.h"

@interface TourOptionViewController ()
@property(nonatomic, strong) DWKWebView *webView;
@property(nonatomic, assign) NSInteger index;
@end

@implementation TourOptionViewController


- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationController.navigationBar.translucent = YES;
	self.title = self.optionTitle;
	self.webView = [[DWKWebView alloc] initWithFrame:self.view.bounds];
	[self.view addSubview:self.webView];
#ifdef DEBUG
	[self.webView setDebugMode:YES];
#endif
	[self setUpJSBridge];
}

-(void)setUpJSBridge{
	JSTourSiteBridge *bridge = [[JSTourSiteBridge alloc] init];
	@weakify(self)
	bridge.optionHandle = ^(NSNumber *index) {
		@strongify(self)
		[self.navigationController popViewControllerAnimated:YES];
		if (self.indexBlock) {
			self.indexBlock(index.integerValue);
		}
	};
	NSString *buldlePath = [[NSBundle mainBundle] bundlePath];
	NSString *basePath = [NSString stringWithFormat:@"%@/TourOption",buldlePath];
	NSURL *baseUrl = [NSURL fileURLWithPath:basePath isDirectory:YES];
	NSString *indexPath = [NSString stringWithFormat:@"%@/index.html",basePath];
	NSString *htmlContent = [NSString stringWithContentsOfFile:indexPath encoding:NSUTF8StringEncoding error:nil];
	[self.webView loadHTMLString:htmlContent baseURL:baseUrl];
	[self.webView addJavascriptObject:bridge namespace:@"tourSite"];
	BOOL isCnSite = [Vars is_cn_website];
	[self.webView callHandler:@"app.changeRouter" arguments:@[@"TourOption"]];
	[self.webView callHandler:@"tourSite.initOption" arguments:@[self.labelString,@(isCnSite)]];

}

@end
