//
//  BasePopView.m
//  GotoBus
//
//  Created by 包光清 on 2018/6/13.
//

#import "BasePopView.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "UIToolkit.h"
#import <PureLayout/PureLayout.h>

@interface BasePopView ()
@end

@implementation BasePopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
        [self addGestureRecognizer:pan];
    }
    return self;
}

- (void)panGesture:(UIPanGestureRecognizer *)sender {
	
	if (sender.state == UIGestureRecognizerStateChanged) {
		self.changePopViewFrame(CGRectGetHeight(self.superview.frame) - [sender locationInView:self.superview].y );
	}
	
    if (sender.state == UIGestureRecognizerStateEnded) {
		[self popViewChangeSizeTypeWithEndedlocation:CGRectGetHeight(self.superview.frame) - [sender locationInView:self.superview].y];
        if (self.didChangePopViewFrame) {
			switch (self.popViewState) {
				case PopViewSizeTypeNone:
					self.didChangePopViewFrame(self.minimumHeight);
					break;
				case PopViewSizeTypeHalf:
					self.didChangePopViewFrame(self.baselineHeight);
					break;
				case PopViewSizeTypeAll:
					self.didChangePopViewFrame(self.maximumHeight);
					break;
				default:
					break;
			}
			
        }
    }
}

- (void)popViewChangeSizeTypeWithEndedlocation:(CGFloat)endLocationY {
	if (endLocationY >= self.maximumHeight) {
		self.popViewState = PopViewSizeTypeAll;
	} else if (endLocationY >= self.baselineHeight) {
		if (self.popViewState == PopViewSizeTypeAll) {
			self.popViewState = PopViewSizeTypeHalf;
		} else {
			self.popViewState = PopViewSizeTypeAll;
		}
	} else if (endLocationY >= self.minimumHeight) {
		if (self.popViewState == PopViewSizeTypeNone) {
			self.popViewState = PopViewSizeTypeHalf;
		} else {
			self.popViewState = PopViewSizeTypeNone;
		}
	} else {
		self.popViewState = PopViewSizeTypeNone;
	}
}


@end
