//
//  DDBaseNavigationController.m
//  DDBasicControlsKit_Private
//  Created by DDIsFriend on 2022/9/20.

#import "DDBaseNavigationController.h"
#import "DDNavigationControllerDelegateReceiver.h"
#import <DDLogKit_Private/DDOCLog.h>

@interface DDBaseNavigationController ()
@property (nullable , nonatomic, strong)DDNavigationControllerDelegateReceiver *delegateReceiver;
@property (nullable , nonatomic, strong)UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer;
@end

@implementation DDBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self config];
    
    [self addNavigationControllerDelegateReceiver];
    
    [self addScreenEdgePanGestureRecognizer];
}

// MARK: <Custom Function>
- (void)config{
    self.dd_hidesBottomBarWhenPushed = YES;
}

- (void)addNavigationControllerDelegateReceiver{
    self.delegateReceiver = [[DDNavigationControllerDelegateReceiver alloc] init];
    self.delegate = self.delegateReceiver;
}

// MARK: <ScreenEdgePanGestureRecognizer>
- (void)addScreenEdgePanGestureRecognizer{
    UIScreenEdgePanGestureRecognizer *interactivePopGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerToPop:)];
    interactivePopGestureRecognizer.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:interactivePopGestureRecognizer];
    self.screenEdgePanGestureRecognizer = interactivePopGestureRecognizer;
    
//    self.interactivePopGestureRecognizer.delegate = self;
    self.interactivePopGestureRecognizer.enabled = NO;
}

- (void)panGestureRecognizerToPop:(UIPanGestureRecognizer *)ges{
    if (ges.state == UIGestureRecognizerStateBegan){
        if (self.viewControllers.count == 1){
            return;
        }

        self.delegateReceiver.percentDrivenInteractiveTransition = [UIPercentDrivenInteractiveTransition new];
        [self popViewControllerAnimated:YES];
    }
    
    CGFloat progress = [ges translationInView:self.view].x / [UIScreen mainScreen].bounds.size.width;
    
    if (ges.state == UIGestureRecognizerStateChanged){
        [self.delegateReceiver.percentDrivenInteractiveTransition updateInteractiveTransition:progress];
    }else if (ges.state == UIGestureRecognizerStateEnded || ges.state == UIGestureRecognizerStateCancelled){
        if (progress >= 0.5){
            [self.delegateReceiver.percentDrivenInteractiveTransition finishInteractiveTransition];
        }else{
            [self.delegateReceiver.percentDrivenInteractiveTransition cancelInteractiveTransition];
        }
        self.delegateReceiver.percentDrivenInteractiveTransition = nil;
    }else if (ges.state == UIGestureRecognizerStateFailed){
        return;
    }
}

- (void)screenEdgePanGestureRecognizerEnable:(BOOL)enable{
    self.screenEdgePanGestureRecognizer.enabled = enable;
}

// MARK: <UIStatusBarStyle>
//- (UIStatusBarStyle)preferredStatusBarStyle{
//    return UIStatusBarStyleDefault;
//}

// override this method will instead - (UIStatusBarStyle)preferredStatusBarStyle.
- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}

- (UIViewController *)childViewControllerForStatusBarHidden{
    return self.topViewController;
}

// MARK: <Orientations>
- (BOOL)shouldAutorotate{
    return self.topViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}
// MARK: <Dealloc>
- (void)dealloc{
    DDLog(@"Attention:控制器 ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----%@ dealloc",[self class]);
}
@end
