//
// LiveHelpViewController.m
// Project
//
// Created by haibara on 1/10/18.
//

#import "LiveHelpViewController.h"
#import "SDK.h"
#import "DrawUtils.h"
#import "Utility.h"
#import "Vars.h"
#import "DrawUtils.h"
#import "CocoaUtils.h"
#import "FAKIcon.h"
#import "WebViewErrorView.h"
#import "_NoInputAccessoryView.h"
#import <objc/runtime.h>


@interface LiveHelpViewController ()
@property(nonatomic, strong) WebViewErrorView *errorView;
@end

@implementation LiveHelpViewController

- (instancetype)init {
    if (self = [super init]) {
        // Custom initialization
        self.title = IMLocalizedString(@"Support");
        [[Vars appDelegate] applyDefaultConfigurationForViewController:self];
    }
    return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	
	[self removeInputAccessoryViewFromWKWebView:self.webView];
	// Do any additional setup after loading the view.
	if ([Vars phone] || [Vars rangedPhones] || [Vars tollFreePhone]) {
		UINavigationItem *navigationItem = [[UIBarButtonItem alloc] im_initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome phoneIconWithSize:24] fillColor:[UIColor blackColor]] style:UIBarButtonItemStylePlain handler:^(UIBarButtonItem *barButtonItem) {
			[[Vars appDelegate] callPhone];
		}];
		 self.navigationItem.rightBarButtonItem = navigationItem;
	}
	
	self.URL = [NSURL URLWithString:[Vars liveZillaUrl]];
	self.navigationActionDecisionHandler = ^WKNavigationActionPolicy (WKWebView *webView, WKNavigationAction *navigationAction) {
		NSURL *url = navigationAction.request.URL;
		if ([url.host isEqualToString:@"live.taketours.com"] && [url.path isEqualToString:@"/leave_message.html"]) {
			dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
				[webView reload];
			});
			[Utility sendFeedback];
			return WKNavigationActionPolicyCancel;
		}
		return WKNavigationActionPolicyAllow;
	};

}

- (void)removeInputAccessoryViewFromWKWebView:(WKWebView *)webView {
	UIView *targetView;
	
	for (UIView *view in webView.scrollView.subviews) {
		if([[view.class description] hasPrefix:@"WKContent"]) {
			targetView = view;
		}
	}
	
	if (!targetView) {
		return;
	}
	
	NSString *noInputAccessoryViewClassName = [NSString stringWithFormat:@"%@_NoInputAccessoryView", targetView.class.superclass];
	Class newClass = NSClassFromString(noInputAccessoryViewClassName);
	
	if(newClass == nil) {
		newClass = objc_allocateClassPair(targetView.class, [noInputAccessoryViewClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
		if(!newClass) {
			return;
		}
		
		Method method = class_getInstanceMethod([_NoInputAccessoryView class], @selector(inputAccessoryView));
		
		class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));
		
		objc_registerClassPair(newClass);
	}
	
	object_setClass(targetView, newClass);
}

-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
	self.errorView.hidden = NO;
}


-(void)refreshWebView{
	self.errorView.hidden = YES;
	[self.webView loadRequest:[NSURLRequest requestWithURL:self.URL]];
}

-(WebViewErrorView *)errorView{
	if (!_errorView) {
		_errorView = [WebViewErrorView viewFromNib];
		_errorView.frame = self.view.bounds;
		[self.view addSubview:_errorView];
		_errorView.hidden = YES;
		[_errorView.refreshButton addTarget:self action:@selector(refreshWebView) forControlEvents:UIControlEventTouchUpInside];
	}
	return _errorView;
}
@end
