/*---------------------------------------------------------------------------*/
/* WebViewNavigationController.m                                             */
/* copyright (c) innovaphone 2015                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

#import "platform/platform.h"
#import "platform/IosNamespace.h"
#import "../ios_forms/FormsConst.h"
#import "PhoneIosDelegate.h"
#import "WebViewNavigationController.h"

#define SWIPE_BACK_LEFT_EDGE_LIMIT  40

@interface WebViewNavigationController()

@end
@implementation WebViewNavigationController

- (void) doPushViewController_:(UIViewController *)viewController animated:(BOOL)animated
{
    [self pushViewController:viewController animated:animated];
}

- (void) doPerformSegueWithIdentifier_:(NSString *)identifier sender:(UIViewController *)sender
{
    [sender performSegueWithIdentifier:identifier sender:sender];
}

- (void) doPopViewController_:(BOOL)animated
{
    if (self.viewControllers.count == 1) {
        [self.backButtonDelegate backButtonClick];
    }
    else {
        [self popViewControllerAnimated:animated];
    }
}

- (void) updateContentSize
{
}

- (void) viewDidLoad
{
    WebViewNavigationController *__weak weakSelf = self;
    _doPushViewController = ^ void (UIViewController *viewController, bool animated) { [weakSelf doPushViewController_:viewController animated:animated]; };
    _doPerformSegueWithIdentifier = ^ void (NSString *identifier, UIViewController *sender) { [weakSelf doPerformSegueWithIdentifier_:identifier sender:sender]; };
    _doPopViewController = ^ void (bool animated) { [weakSelf doPopViewController_:animated]; };

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.translucent = false;
    if ([self.navigationBar respondsToSelector:@selector(standardAppearance)]) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor = self.statusBarColor;
        self.navigationBar.standardAppearance = appearance;
        self.navigationBar.scrollEdgeAppearance = appearance;
    }
    self.delegate = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        [self.view removeGestureRecognizer:self.interactivePopGestureRecognizer];
    }
    UISwipeGestureRecognizer *swipeBack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeBackHandler:)];
    swipeBack.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeBack];
    [self updateContentSize];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (UIStatusBarStyle) preferredStatusBarStyle
{ 
    return self.statusBarStyle;
}

- (BOOL) prefersStatusBarHidden
{
    return self.statusBarHidden;
}

- (void) swipeBackHandler:(UISwipeGestureRecognizer*)gesture
{
    if ((gesture.state == UIGestureRecognizerStateEnded)
     && ([gesture locationOfTouch:0 inView:self.view].x < SWIPE_BACK_LEFT_EDGE_LIMIT)
     && (((UIViewController *)([self.viewControllers objectAtIndex:self.viewControllers.count - 1])).navigationItem.rightBarButtonItem == nil)) {
        if (self.viewControllers.count == 1) {
            [self.backButtonDelegate backButtonClick];
        }
        else {
            [self popViewControllerAnimated:true];
        }
    }
}

- (void) cancelClicked
{
    if (self.viewControllers.count == 1) {
        [self.backButtonDelegate backButtonClick];
    }
    else {
        [self popViewControllerAnimated:true];
    }
}

- (void) dealloc
{
}

- (BOOL) navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item
{
    return YES;
}

- (BOOL) navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    if (self.viewControllers.count == 1) {
        [self.backButtonDelegate backButtonClick];
        return NO;
    }
    else {
        [self popViewControllerAnimated:true];
        return YES;
    }
}

- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
}

- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController.navigationItem.leftBarButtonItem == nil) {
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
            target:self action:@selector(cancelClicked)];
        [self.navigationItem.leftBarButtonItem setTitleTextAttributes:
            [NSDictionary dictionaryWithObjectsAndKeys:
                self.statusBarTextColor,
                NSForegroundColorAttributeName,
                nil]
            forState:UIControlStateNormal];
    }
}

- (void) navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}

@end
