//
//  WSBKBluetoothListView.m
//  WSBKLiquidCheck
//
//  Created by vispek on 2021/9/12.
//

#import "WSBKBluetoothListView.h"


@interface WSBKBluetoothListView () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UILabel *progressLab;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;




@property (nonatomic, strong) UILabel *tipLabl;
@end

@implementation WSBKBluetoothListView

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    NSLog(@"willMoveToSuperview");
}

- (void)setPeripheralDataArray:(NSMutableArray *)peripheralDataArray {
    _peripheralDataArray = peripheralDataArray;
    [self.tableView reloadData];
}


+ (instancetype)fromNib {
    return [[UINib nibWithNibName:NSStringFromClass([WSBKBluetoothListView class]) bundle:[NSBundle bundleForClass:[WSBKBluetoothListView class]]] instantiateWithOwner:nil options:nil].firstObject;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
    
    [self.activityIndicator startAnimating];
    
    self.tipLabl = [UILabel new];
    self.tipLabl.frame = CGRectMake(0, 0, 100, 100);
    self.tipLabl.text = @"暂无可用设备";
    self.tipLabl.textColor = [UIColor blackColor];
    [self.tipLabl sizeToFit];
    self.tipLabl.hidden = YES;
    [self addSubview:self.tipLabl];
}


- (void)setup {
    self.layer.cornerRadius = 18;
    
    self.tableView.hidden = YES;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.image = [UIImage imageNamed:@"img_yuan1"];
    self.imageView.hidden= YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}


//取消按钮点击
- (IBAction)cancelBtnClick:(UIButton *)sender {
    if (self.negativeButtonClicked) {
        self.negativeButtonClicked(self);
    }
    [self.activityIndicator stopAnimating];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    self.tableView.hidden = (self.peripheralDataArray.count == 0);
    return self.peripheralDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"WSBKBluetoothListViewcell";
    
   WSBKPeripheralInfoModel *model = [self.peripheralDataArray objectAtIndex:indexPath.row];
    CBPeripheral *peripheral = model.peripheral;
    NSDictionary *advertisementData = model.advertisementData;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//不变色
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    
    //peripheral的显示名称,优先用kCBAdvDataLocalName的定义，若没有再使用peripheral name
    NSString *peripheralName;
    if ([advertisementData objectForKey:@"kCBAdvDataLocalName"]) {
        peripheralName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]];
    }else if(!([peripheral.name isEqualToString:@""] || peripheral.name == nil)){
        peripheralName = peripheral.name;
    }else{
        peripheralName = [peripheral.identifier UUIDString];
    }
    
//    self.deviceCode = peripheralName;
    cell.textLabel.text = peripheralName;
//    //信号和服务
//    cell.detailTextLabel.text = [NSString stringWithFormat:@"RSSI:%@",RSSI];
    return cell;
}

//设置每行表格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

//选择表格视图某一行调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[WSBKBlueToothSDK shared] disconnectAllPeripherals];
    [[WSBKBlueToothSDK shared] stopScanPeripheral];

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WSBKPeripheralInfoModel *model = [self.peripheralDataArray objectAtIndex:indexPath.row];
    [[WSBKBlueToothSDK shared] connectPeripheral:model.peripheral];
    
    if (self.selectedCellClicked) {
        self.selectedCellClicked(self);
    }
    
}

//插入table数据
-(void)insertTableView:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    
    NSArray *peripherals = [self.peripheralDataArray valueForKey:@"peripheral"];
    if(![peripherals containsObject:peripheral]) {
        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:peripherals.count inSection:0];
        [indexPaths addObject:indexPath];
        
        NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
        [item setValue:peripheral forKey:@"peripheral"];
        [item setValue:RSSI forKey:@"RSSI"];
        [item setValue:advertisementData forKey:@"advertisementData"];
        
        [self.peripheralDataArray addObject:item];
        
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (void)dealloc {
}


@end
