//
//  TourInterception.m
//  Project
//
//  Created by Vae on 2018/10/8.
//

#import "TourInterception.h"
#import "TourSearchReviewListViewController.h"
#import "TourSearchDetailViewController.h"
#import "TourSearchListViewController.h"
#import "CocoaUtils.h"
#import "StringUtils.h"
#import "Utility.h"
#import "SDK.h"
#import "Vars.h"

@implementation TourInterception

-(BOOL)handleUrl:(NSURL *)url{
	if ([url.path hasSuffix:@"review.html"]) {
		NSString *templeUrl = [self travelCode:url.path];
		if([StringUtils isEmpty:templeUrl]){
			return NO;
		} else {
			[self routerReviewController:templeUrl];
			return YES;
		}
	}
	
	NSUInteger numMatchCount = [self regularExpressionNumberfor:url.pathComponents.lastObject];
	if ([StringUtils isEmpty:url.pathComponents.lastObject]){
		return NO;
	}
	if (numMatchCount > 3 && ![url.path hasSuffix:@"tour.do"]) {
		[self routerDetailViewController: [self travelCode:url.pathComponents.lastObject]];
		return YES;
	}

	
	if ([url.absoluteString hasSuffix:@"review-app.html"]) {
		[self routerReviewController:url.path];
		return YES;
	}else if ([url.path hasSuffix:@"tour.do"]) {
		[self routerListViewController:url.query];
		return YES;
	}else {
		NSString *temUrlString;
		if([url.absoluteString containsString:@"-"]){
			temUrlString = [url.absoluteString stringByReplacingOccurrencesOfString:@"-" withString:@""];
		} else {
			temUrlString = url.absoluteString;
		}
		if ([self regularExpressionNumberfor:temUrlString] != 0 && [self regularExpressionNumberfor:temUrlString] == temUrlString.length){
			[self routerReviewController:url.absoluteString];
			return YES;
		}
	}
	return NO;
}

-(void)routerReviewController:(NSString *)str{
	TourSearchReviewListViewController *viewcontroller = [[TourSearchReviewListViewController alloc] init];
	if (![StringUtils isEmpty:str]){
		viewcontroller.tourCode = str;
		UIViewController *vc = [CocoaUtils topMostViewController];
		[vc.navigationController pushViewController:viewcontroller animated:YES];
	}
}

-(void)routerDetailViewController:(NSString *)str {
	TourSearchDetailViewController *viewController = [[TourSearchDetailViewController alloc] init];
	if (![StringUtils isEmpty:str]){
		viewController.tourCode = str;
		UIViewController *vc = [CocoaUtils topMostViewController];
		[vc.navigationController pushViewController:viewController animated:YES];
	}
}

-(void)routerListViewController:(NSString *)str{
	TourSearchListViewController *viewController = [[TourSearchListViewController alloc] init];
	NSDictionary *params = [self paramWithQueryString:str];
	TourSearchPaginationParam *param = [[TourSearchPaginationParam alloc] init];
	if ([[params allKeys] containsObject:@"dep_ids"]) {
		param.departures =@[params[@"dep_ids"]];
	}
	if ([[params allKeys] containsObject:@"destinations"]){
		param.destinations = @[params[@"destinations"]];
	}
	if ([[params allKeys] containsObject:@"duration"]){
		param.duration = @[params[@"duration"]];
	}
	param.pageNumber = 0;
	viewController.tourSearchParam = param;
	UIViewController *vc = [CocoaUtils topMostViewController];
	[vc.navigationController pushViewController:viewController animated:YES];
}


-(NSDictionary *)paramWithQueryString:(NSString *)queryString{
	NSArray *params = [queryString componentsSeparatedByString:@"&"];
	NSMutableDictionary *dic = [NSMutableDictionary dictionary];
	for (NSString *param in params) {
		NSArray *dicArray = [param componentsSeparatedByString:@"="];
		if (dicArray.count > 1) {
			[dic setObject:dicArray[1] forKey:dicArray[0]];
		}
	}
	return dic;
}

- (NSString *)travelCode:(NSString *)str{
	str = [str stringByReplacingOccurrencesOfString:@".html" withString:@""];
	NSMutableArray *templearray = [str componentsSeparatedByString:@"-"];
	if (templearray.count > 1){
		[templearray removeObjectAtIndex:0];
	}
	NSMutableArray *array = [NSMutableArray array];
	if(templearray.count >= 3){
		for (int i = 0; i < templearray.count; i++) {
			if(i >= templearray.count - 3)
				[array addObject:templearray[i]];
		}
	}
	

	
	NSMutableString *numString = [NSMutableString string];
	for (NSString *str in array) {
		NSScanner* scan = [NSScanner scannerWithString:str];
		int val;
		if ( [scan scanInt:&val] && [scan isAtEnd]) {
			if (numString.length) {
				[numString appendString:str];
			}else{
				[numString appendString:[NSString stringWithFormat:@"%@-",str]];
			}
		}
	}
	if ([StringUtils isEmpty:numString]){
		return @"";
	}
	else {
		return numString;
	}
	
}

- (NSUInteger)regularExpressionNumberfor:(NSString *)strContent {
	NSRegularExpression *numRegularExpression = [NSRegularExpression
												 regularExpressionWithPattern:@"[0-9]"
												 options:NSRegularExpressionCaseInsensitive
												 error:nil];
	NSUInteger numMatchCount = 0;
	numMatchCount = [numRegularExpression
					 numberOfMatchesInString:strContent
					 options:NSMatchingReportProgress
					 range:NSMakeRange(0, strContent.length)];
	return numMatchCount;
}
@end
