//
//  UIViewController+HTTP.m
//  iCemarose
//
//  Created by Sanchew on 2017/7/13.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "UIViewController+HTTP.h"
#import <JLRoutes/JLRoutes.h> 
#import <RegexKitLite/RegexKitLite.h>
//#import <TOWebViewController/TOWebViewController.h>
#import "DPWebViewController.h"
#import "AppDelegate.h"
#import "KWMWebViewVC.h"

@implementation UIViewController (HTTP)

- (void)openURLWithString:(NSString *)URLString {
    NSString *parten=@"[^%\\da-zA-Z:/.?&=]+";
    NSRegularExpression *reg=[NSRegularExpression regularExpressionWithPattern:parten options:0 error:nil];
    NSArray *matchs=[reg matchesInString:URLString options:0 range:NSMakeRange(0, [URLString length])];
    NSString *encodeURL=[NSString stringWithString:URLString];
    for (long i=matchs.count-1; i>=0; --i) {
        NSTextCheckingResult *tcr=matchs[i];
        encodeURL=[encodeURL stringByReplacingCharactersInRange:tcr.range withString:[[encodeURL substringWithRange:tcr.range] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
//    NSURL *url = [[NSURL alloc] initWithString:[encodeURL stringByReplacingOccurrencesOfRegex:@"^https?" withString:@"cemarose"]];
    NSURL *url = [[NSURL alloc] initWithString:encodeURL];
    if ([JLRoutes canRouteURL:url]) {
        [JLRoutes routeURL:url];
//    }else if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
//        [application openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil];
    } else if(url){
//        [application openURL:[NSURL URLWithString:URLString]];
        TOWebViewController *wv = [[DPWebViewController alloc] initWithURLString:encodeURL];
        wv.hidesBottomBarWhenPushed = YES;
        [[AppDelegate mainViewController].selectedViewController pushViewController:wv animated:YES];
    }
}

@end