//
//  ScanVC.m
//  app
//
//  Created by xuyazhong on 2022/4/2.
//

#import "ScanVC.h"
#import <SGQRCode/SGQRCode.h>
#import "AppManager.h"
#import <TZImagePickerController/TZImagePickerController.h>

@interface ScanVC (){
    SGScanCode *scanCode;
}
@property (nonatomic, strong) SGScanView *scanView;
@property (nonatomic, strong) UILabel *promptLabel;
@property (nonatomic, assign) BOOL stop;

@end

@implementation ScanVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (_stop) {
        [scanCode startRunningWithBefore:nil completion:nil];
    }
}

- (void)dealloc {
    NSLog(@"WBQRCodeVC - dealloc");
    [self removeScanningView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor blackColor];
    
    scanCode = [SGScanCode scanCode];
    
    [self setupQRCodeScan];
    [self setupNavigationBar];
    [self.view addSubview:self.scanView];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[ UIImage imageNamed:@"react-native-helper.bundle/back@2x.png"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(15, [TZCommonTools tz_statusBarHeight]+10, 30, 30);
    [btn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [self.scanView addSubview:btn];
    [self.scanView startScanning];
    [self.view addSubview:self.promptLabel];
}

- (void)setupQRCodeScan {
    __weak typeof(self) weakSelf = self;
    
    [scanCode scanWithController:self resultBlock:^(SGScanCode *scanCode, NSString *result) {
        if (result) {
            [scanCode stopRunning];
            weakSelf.stop = YES;
            if (weakSelf.block) {
                weakSelf.block(result);
            }
            [weakSelf dismissViewControllerAnimated:NO completion:nil];
        }
    }];
    [scanCode startRunningWithBefore:^{
        NSLog(@"正在加载...");
    } completion:^{
        NSLog(@"加载完成");
    }];
}

- (void)setupNavigationBar {
    self.navigationItem.title = @"扫一扫";
}

- (void)close {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (SGScanView *)scanView {
    if (!_scanView) {
        _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _scanView.scanLineName = @"react-native-helper.bundle/scan_icon_scanline@2x.png";
        _scanView.scanStyle = ScanStyleDefault;
        _scanView.cornerLocation = CornerLoactionOutside;
        _scanView.cornerColor = [UIColor colorWithRed:0 green:113/255.0 blue:255/255.0 alpha:1];
    }
    return _scanView;
}

- (void)removeScanningView {
    [self.scanView stopScanning];
    [self.scanView removeFromSuperview];
    self.scanView = nil;
}

- (UILabel *)promptLabel {
    if (!_promptLabel) {
        _promptLabel = [[UILabel alloc] init];
        _promptLabel.backgroundColor = [UIColor clearColor];
        CGFloat promptLabelX = 0;
        CGFloat promptLabelY = 0.73 * self.view.frame.size.height;
        CGFloat promptLabelW = self.view.frame.size.width;
        CGFloat promptLabelH = 25;
        _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
        _promptLabel.textAlignment = NSTextAlignmentCenter;
        _promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
        _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
        _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
    }
    return _promptLabel;
}
@end
