//
//  ERTCVideoRemoteShareView.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/5/18.
//

#import "ERTCVideoRemoteShareView.h"
#import "ERTCVideoMeetingManager.h"
#import "ERTCPlayerViewManager.h"

@interface ERTCVideoRemoteShareView ()<UIGestureRecognizerDelegate>
@property (nonatomic, assign) CGPoint lastCenterPoint;

@end
@implementation ERTCVideoRemoteShareView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureAction:)];
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        panGesture.delegate = self;
        [self addGestureRecognizer:pinchGesture];
        [self addGestureRecognizer:panGesture];
        
    }
    return self;
}

/**
 拖动手势
 */
- (void)panGestureAction:(UIPanGestureRecognizer *)recognizer {
    CGVector vector = CGVectorMake(0, 0);
    CGPoint centerPoint = [recognizer locationInView:recognizer.view];
        if (self.lastCenterPoint.x != 0 && self.lastCenterPoint.y != 0) {
            vector = CGVectorMake(centerPoint.x - _lastCenterPoint.x, centerPoint.y - _lastCenterPoint.y);
        }
    self.lastCenterPoint = centerPoint;
    [[ERTCPlayerViewManager sharedInstance].engine zoomMoveView:self.bounds.size WithUserId:@"" withRegionID:0 movement:vector];
    switch (recognizer.state) {
            case UIGestureRecognizerStateBegan:

                break;
            case  UIGestureRecognizerStateChanged: {

            }
                break;
            case UIGestureRecognizerStateEnded:{
                self.lastCenterPoint = CGPointMake(0, 0);
            }
            default:
                break;
        }

}
/**
 捏合放大手势响应方法
 */
-(void)pinchGestureAction:(UIPinchGestureRecognizer*)gesture {
    static CGFloat scaleCurrent = 1.0;
    static CGPoint s_center;
    static int ratio = 1;
    if (gesture.state == UIGestureRecognizerStateBegan) {
        scaleCurrent = [[ERTCPlayerViewManager sharedInstance].engine viewScaleOfUserId:@"" withRegionID:0];
        ratio = scaleCurrent / 1;
        s_center = [gesture locationInView:gesture.view];
    }else {
        CGFloat newScale = scaleCurrent + ratio * (gesture.scale - 1.0);
        newScale = MAX(1.0, newScale);
        CGPoint center = ([gesture locationInView:gesture.view]);
        [[ERTCPlayerViewManager sharedInstance].engine zoomScaleView:self.bounds.size WithUserId:@"" withRegionID:0 scale:newScale center:center];
    }
}


@end
