//
//  XVOrderDetailsViewController.m
//  fastservice
//
//  Created by xuning on 2/8/24.
//  Copyright © 2024 FastService. All rights reserved.
//

#import "XVOrderDetailsViewController.h"
#import "XVOrderInfoViewController.h"



@interface XVOrderDetailsViewController ()<UIGestureRecognizerDelegate>



@property (nonatomic, strong) XVOrderInfoViewController *infoVc;
@property (nonatomic, assign) CGPoint containerOrigin;
@property (nonatomic, assign) NSInteger lastPos;
@end

@implementation XVOrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];

    
    [self setupUI];
    
}

- (void)setupUI {
    
    
    self.infoVc.view.frame = CGRectMake(0, 361, SCREEN_WIDTH, SCREEN_HEIGHT-KTOP_HEIGHT);
    [self.view addSubview:self.infoVc.view];

    [self addChildViewController:self.infoVc];
    self.infoVc.scrollView.scrollEnabled = NO;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognized:)];
    pan.delegate = self;
    [self.infoVc.view addGestureRecognizer:pan];
    
    
}


- (void)panGestureRecognized:(UIPanGestureRecognizer *)pan {
    
    
    UIView *view = pan.view;
    
    CGPoint offset = [pan translationInView:self.view];
    if(offset.y > 0 && self.infoVc.view.top == 361) {
        return;
    }
    if(view.top <= 0) {
        view.top = 0;
    }else{
        view.top += offset.y;
    }
    DebugLog(@"%f===%ld",view.top,(long)pan.state);
    
    
    if(pan.state == UIGestureRecognizerStateEnded) {
        if ([pan velocityInView:self.view].y < 0) {
            
            if(view.top <= 90) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    view.top = 0;
                } completion:^(BOOL finished) {
                    self.infoVc.scrollView.contentOffset = CGPointMake(0, 0);
                    self.infoVc.scrollView.scrollEnabled = YES;
                }];
                
            }else if(view.top <= 270 && view.top > 90) {
                [UIView animateWithDuration:0.3 animations:^{
                    view.top = 180;
                } completion:^(BOOL finished) {
                    self.infoVc.scrollView.contentOffset = CGPointMake(0, 0);
                    self.infoVc.scrollView.scrollEnabled = NO;
                }];
            }else if(view.top > 270) {
                [UIView animateWithDuration:0.3 animations:^{
                    view.top = 361;
                } completion:^(BOOL finished) {
                    self.infoVc.scrollView.contentOffset = CGPointMake(0, 0);
                    self.infoVc.scrollView.scrollEnabled = NO;
                }];
            }
            
        }else{
            if(view.top == 0) {
                [UIView animateWithDuration:0.3 animations:^{
                    view.top = 180;
                } completion:^(BOOL finished) {
                    self.infoVc.scrollView.contentOffset = CGPointMake(0, 0);
                    self.infoVc.scrollView.scrollEnabled = NO;
                }];
            }else if(view.top <= 270 && view.top > 90) {
                [UIView animateWithDuration:0.3 animations:^{
                    view.top = 180;
                } completion:^(BOOL finished) {
                    self.infoVc.scrollView.contentOffset = CGPointMake(0, 0);
                    self.infoVc.scrollView.scrollEnabled = NO;
                }];
            }else if(view.top > 270) {
                [UIView animateWithDuration:0.3 animations:^{
                    view.top = 361;
                } completion:^(BOOL finished) {
                    self.infoVc.scrollView.contentOffset = CGPointMake(0, 0);
                    self.infoVc.scrollView.scrollEnabled = NO;
                }];
            }
            
            
        }
        
    }
    [pan setTranslation:CGPointZero inView:view];}


// 实现UIGestureRecognizerDelegate代理方法  ,  返回YES表示支持多个手势同时触发，否则不允许多个手势同时触发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    if ([otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *tab = (UIScrollView *)[otherGestureRecognizer view];
        CGPoint vel = [tab.panGestureRecognizer velocityInView:tab];
        if (vel.y > 0) {
            // 下拉 , 只有当contentOffset.y为0时才允许多手势同时触发
            CGPoint Point= tab.contentOffset;
            return Point.y == 0;
            
        }else{
            // 上拉
            return NO;
        }
    }
    return NO;
}

- (XVOrderInfoViewController *)infoVc {
    if(_infoVc == nil) {
        _infoVc = [[XVOrderInfoViewController alloc]init];
    }
    return _infoVc;
}


@end
