//
//  ViewController.m
//  SDKDemo
//
//  Created by vispek on 2022/4/12.
//

#import "ViewController.h"
#import "WSBKBluetoothListView.h"
#import "WSBKAlertViewController.h"
#import "MBProgressHUD.h"



@interface ViewController () <WSBKBlueToothSDKDelegate>
@property (weak, nonatomic) IBOutlet UIButton *blueToothConnect;
@property (nonatomic, strong) WSBKBluetoothListView *bluetoothListView;
@property (weak, nonatomic) IBOutlet UILabel *deviceBatteryPowerLab;
@property (weak, nonatomic) IBOutlet UILabel *SpectralDataLab;
@property (nonatomic, assign) BOOL blueConnectStaus;
@property (weak, nonatomic) IBOutlet UIButton *temperatureBtn;
@property (weak, nonatomic) IBOutlet UILabel *temperatureLab;
@property (weak, nonatomic) IBOutlet UIButton *deviceCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *deviceCodeLab;
@property (weak, nonatomic) IBOutlet UIButton *deviceVersionBtn;
@property (weak, nonatomic) IBOutlet UILabel *deviceVersionLab;
@property (weak, nonatomic) IBOutlet UILabel *highVoltageLab;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [WSBKBlueToothSDK shared].delegate = self;
    self.blueConnectStaus = NO;
    

}



- (IBAction)blueConnectedBtnClicked:(UIButton *)sender {

    if (!self.blueConnectStaus) {
        WSBKAlertViewController *alertViewController = [[WSBKAlertViewController alloc] initWithTransitionStyle:WSBKAlertViewControllerTransitionStyleFade supportedOrientations:self.supportedInterfaceOrientations];
        alertViewController.view.backgroundColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:0.5];
        CGFloat height = [[UIDevice currentDevice].model hasPrefix:@"iPad"] ? 400 : 266;
        
        
        WSBKBluetoothListView *view = [WSBKBluetoothListView fromNib];
        self.bluetoothListView = view;
        __weak typeof(self) weakSelf = self;
        __weak typeof(alertViewController) weakAlertCtrl = alertViewController;
        self.bluetoothListView.negativeButtonClicked = ^(WSBKBluetoothListView *view) {
            __strong typeof(weakAlertCtrl) alertViewController = weakAlertCtrl;
            if (!alertViewController) return;
            [[WSBKBlueToothSDK shared] stopScanPeripheral];
            [alertViewController dismissViewControllerAnimated:YES completion:nil];
        };

        //连接蓝牙设备
        self.bluetoothListView.selectedCellClicked = ^(WSBKBluetoothListView *view) {
            __strong typeof(weakAlertCtrl) alertViewController = weakAlertCtrl;
            __strong typeof(weakSelf) self = weakSelf;

            if (!alertViewController) return;
            [alertViewController dismissViewControllerAnimated:YES completion:^{
            }];
            
            //只显示文字
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = @"正在连接蓝牙设备，请稍后";
            hud.margin = 10.f;
        };
        
        alertViewController.alertViewContentView = view;
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(alertViewController.view).offset(10);
            make.right.mas_equalTo(alertViewController.view).offset(-10);
            make.height.mas_equalTo(height);
            
            if (@available(iOS 11.0, *)) {
                make.bottom.mas_equalTo(alertViewController.view.mas_safeAreaLayoutGuideBottom).offset(-10);
            } else {
                make.bottom.mas_equalTo(alertViewController.view).offset(-10);
            }

        }];
        [[WSBKBlueToothSDK shared] startScanPeripheral];

        view.backgroundColor = [UIColor whiteColor];
        [self presentViewController:alertViewController animated:YES completion:nil];
        
    } else {
        self.blueConnectStaus = NO;
    }
}

- (IBAction)sendBatteryCommendWithGetDeviceBatteryPower:(UIButton *)sender {
    
    [[WSBKBlueToothSDK shared] sendCommandType:WSBKBlueToothSDKBatteryCommandType getDeviceData:^(NSString * _Nonnull response, NSString * _Nonnull response2) {
        self.deviceBatteryPowerLab.text = response;
    } failed:^(NSString * _Nonnull failed) {
        NSLog(@"failed: %@",failed);
    }];
}

- (IBAction)sendSpectralCommend:(UIButton *)sender {
    
    //只显示文字
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = @"正获取光谱数据";
    hud.margin = 10.f;
    
    
    [[WSBKBlueToothSDK shared] sendCommandType:WSBKBlueToothSDKSpectralCommandType getDeviceData:^(NSString * _Nonnull response, NSString * _Nonnull response2) {
        self.SpectralDataLab.text = response;
        self.highVoltageLab.text = response2;
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    } failed:^(NSString * _Nonnull failed) {
        NSLog(@"failed: %@",failed);
    }];
    [[WSBKBlueToothSDK shared] stopScanPeripheral];


}

- (IBAction)sendTemperatureCommend:(UIButton *)sender {

    [[WSBKBlueToothSDK shared] sendCommandType:WSBKBlueToothSDKTemperatureCommandType getDeviceData:^(NSString * _Nonnull response, NSString * _Nonnull response2) {
        self.temperatureLab.text = response;
    } failed:^(NSString * _Nonnull failed) {
        NSLog(@"failed: %@",failed);
    }];
}

- (IBAction)sendDeviceCodeCommend:(UIButton *)sender {
   
    [[WSBKBlueToothSDK shared] sendCommandType:WSBKBlueToothSDKDeviceSNCommandType getDeviceData:^(NSString * _Nonnull response, NSString * _Nonnull response2) {
        self.deviceCodeLab.text = response;
    } failed:^(NSString * _Nonnull failed) {
        NSLog(@"failed: %@",failed);
    }];

}

- (IBAction)sendDeviceVersionCommend:(id)sender {

    [[WSBKBlueToothSDK shared] sendCommandType:WSBKBlueToothSDKDeviceVersionCommandType getDeviceData:^(NSString * _Nonnull response, NSString * _Nonnull response2) {
        self.deviceVersionLab.text = response;
    } failed:^(NSString * _Nonnull failed) {
        NSLog(@"failed: %@",failed);
    }];
    
}


- (void)setBlueConnectStaus:(BOOL)blueConnectStaus {
    _blueConnectStaus = blueConnectStaus;
    if (blueConnectStaus) {
        [self.blueToothConnect setTitle:@"断开连接" forState:UIControlStateNormal];
        
    } else {
        [self.blueToothConnect setTitle:@"连接蓝牙" forState:UIControlStateNormal];
        [[WSBKBlueToothSDK shared] disconnectAllPeripherals];
    }
}


#pragma mark 代理回调
- (void)systemBluetoothClose {
    // 系统蓝牙被关闭、提示用户去开启蓝牙
}

- (void)sysytemBluetoothOpen {
    // 系统蓝牙已开启、开始扫描周边的蓝牙设备
}

- (void)disconnectPeripheral:(CBPeripheral *)peripheral {
    self.blueConnectStaus = NO;
}

- (void)getScanResultPeripherals:(NSArray *)peripheralInfoArr {
    self.bluetoothListView.peripheralDataArray = [NSMutableArray arrayWithArray:peripheralInfoArr];
}

// 连接成功
- (void)connectSuccess {
    self.blueConnectStaus = YES;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)connectFailed {
    // 连接失败、做连接失败的处理
    self.blueConnectStaus = NO;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


@end
