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

#import "BusInterception.h"
#import "ProductSearchListVC.h"
#import "BusSearchBoxViewController.h"
#import "UIViewController+Privilege.h"
#import "CocoaUtils.h"
#import "Vars.h"
#import "Lang.h"
#import "InterceptionProtocol.h"
#import "SalesEntryDetailViewController.h"

@implementation BusInterception

- (BOOL)handleUrl:(NSURL *)url{
	if ([url.path isEqualToString:@"/bus-and-hotel"]) {
		[self routerBusAndHotelViewController];
		return YES;
	}
	
	if ([url.query containsString:@"entry_id="]) {
		NSArray *array = [url.absoluteString componentsSeparatedByString:@"="];
		if (array.count > 2) {
			[self routersalesEntryDetailViewController:array[2]];
			return YES;
		}
	}
	
	NSString *pattern = @"^/tickets/.*?-to-.*?-bus$";
	NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
	NSArray *result = [regular matchesInString:url.path options:0 range:NSMakeRange(0, url.path.length)];
	if (result.count > 0) {
		[self routerViewControlerWithString:url.absoluteString];
		return YES;
	}
	return NO;
}

-(void)routerViewControlerWithString:(NSString *)url{
	NSURL *tempUrl = [NSURL URLWithString:url];
	[Vars appDelegate].shopCarts.adultNum = 1;
	[Vars appDelegate].shopCarts.childNum = 0;
	ProductSearchListVC *productSearchListViewController = [[ProductSearchListVC alloc] init];
	productSearchListViewController.hidesBottomBarWhenPushed = YES;
	NSMutableString *pageUrl = [NSMutableString stringWithFormat:@"%@",tempUrl.path];
	if (tempUrl.hasDirectoryPath) {
		[pageUrl appendString:@"/"];
	}
	productSearchListViewController.pageUrl = pageUrl;
	productSearchListViewController.currentDepartString = [[DateFormatUtils ISO_DATE_FORMAT] format:[NSDate date]];
	UIViewController *this = [CocoaUtils topMostViewController];
	[this.navigationController pushViewController:productSearchListViewController animated:YES];
}

-(void)routerBusAndHotelViewController{
	UIViewController *viewController = [[NSClassFromString(@"BusHotelSearchBoxViewController") alloc] init];
	viewController.hidesBottomBarWhenPushed = YES;
	UIViewController *this = [CocoaUtils topMostViewController];
	[this.navigationController pushViewController:viewController animated:YES];
}

-(void)routersalesEntryDetailViewController:(NSString *)entryId{
	if ([StringUtils isEmpty:entryId]) {
		return;
	}
	UIViewController *this = [CocoaUtils topMostViewController];
	SalesEntryDetailViewController *viewController = [[SalesEntryDetailViewController alloc] init];
	viewController.itineraryId = entryId;
	viewController.allowsShowMap = YES;
	viewController.allowsShowBarcode = YES;
	viewController.allowsSelfCheckIn = YES;
	viewController.allowsSelfReschedule = NO;
	[this.navigationController pushViewController:viewController animated:YES];
}

@end
