//
//  SLMikeInputView.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/3.
//

#import "SLMikeInputView.h"

@interface SLMikeInputView()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHeightConst;

@end

@implementation SLMikeInputView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addContentView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addContentView];
    }
    return self;
}

- (void)addContentView {
    [[NSBundle mainBundle] loadNibNamed:@"SLMikeInputView" owner:self options:nil];
    self.contentView.frame = self.bounds;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.55];
    UILongPressGestureRecognizer *gesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
    gesture.minimumPressDuration= 0.3f;
    [self addGestureRecognizer:gesture];
//    gesture.numberOfTouchesRequired = 1;
    UIImage *img = self.mikeInputImageView.image;
    [img resizableImageWithCapInsets:UIEdgeInsetsMake(60, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
    self.mikeInputImageView.image = img;
    CGFloat h = 143 / 750.0 * kSCREEN_WIDTH + kSafeAreaBottom;
    self.imgHeightConst.constant = h;
    self.clipsToBounds = YES;
    self.contentView.hidden = YES;
    [self nuiSpeechRecognizer];
}

- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender {
    CGPoint point = [paramSender locationInView:paramSender.view];
    UIBezierPath *inputViewPath = [UIBezierPath bezierPathWithRect:self.mikeInputImageView.frame];
    if (CGPathContainsPoint(inputViewPath.CGPath, NULL, point, NO)) {
        
    }else{
    }
    UIBezierPath *cancelViewPath = [UIBezierPath bezierPathWithRect:self.mikeCancelImageView.frame];
    BOOL isContainCancelPath = CGPathContainsPoint(cancelViewPath.CGPath, NULL, point, NO);
    if (isContainCancelPath) {
        self.mikeCancelImageView.image = ImageName(@"icon_mike_cancel_sel");
        if (paramSender.state == UIGestureRecognizerStateEnded){
            self.contentView.hidden = YES;
            self.frame = self.oldRect;
        }
    }else{
        self.mikeCancelImageView.image = ImageName(@"icon_mike_cancel_nor");
    }
    if (paramSender.state == UIGestureRecognizerStateBegan) {
        self.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT);
        self.contentView.hidden = NO;
        NSLog(@"SLMikeInputView long pressTap state :begin");
        [self.nuiSpeechRecognizer startSpeechRecognizer];
    }else if (paramSender.state == UIGestureRecognizerStateChanged){

    }else if (paramSender.state == UIGestureRecognizerStateEnded){
        NSLog(@"SLMikeInputView long pressTap state :ended");
        self.contentView.hidden = YES;
        self.frame = self.oldRect;
        [self.nuiSpeechRecognizer stopSpeechRecognizer:!isContainCancelPath];
    }else {
        NSLog(@"SLMikeInputView long pressTap state :end");
        [self.nuiSpeechRecognizer stopSpeechRecognizer:NO];
    }
}

- (SLNuiSpeechRecognizer *)nuiSpeechRecognizer {
    if (!_nuiSpeechRecognizer) {
        _nuiSpeechRecognizer = [[SLNuiSpeechRecognizer alloc] init];
    }
    return _nuiSpeechRecognizer;
}

- (void)alertShow {
    UIWindow* window = nil;
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
        {
            if (windowScene.activationState == UISceneActivationStateForegroundActive)
            {
                window = windowScene.windows.firstObject;
                break;
            }
        }
    }else {
        window = [[UIApplication sharedApplication].delegate window];
    }
    [window makeKeyAndVisible];
    [window makeKeyWindow];
    [window addSubview:self];
}

@end
