//
//  BroadcastHeightGuage.m
//  AILinkBleSDK_Example
//
//  Created by LarryZhang on 2023/6/26.
//  Copyright © 2023 zhengzida. All rights reserved.
//

#import "BroadcastHeightGuage.h"
#import <AILinkBleSDK/ELBroadcastHeightGaugeBleManager.h>

@interface BroadcastHeightGuage ()

@property (weak, nonatomic) IBOutlet UILabel *bleStatusLabel;

@property (weak, nonatomic) IBOutlet UITextView *logTextView;

@end


@interface BroadcastHeightGuage () <ELBroadcastHeightGaugeBleDelegate>

@end


@implementation BroadcastHeightGuage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initBle];
    [self scanBle];
}

- (void)dealloc {
    [self deinitBle];
}

#pragma mark - Action

- (IBAction)clearAction:(id)sender {
    self.logTextView.text = @"";
}

- (IBAction)shareLogAction:(id)sender {
}


#pragma mark - BLE

- (void)initBle {
    [ELBroadcastHeightGaugeBleManager shareManager].heightGaugeDelegate = self;
}

- (void)scanBle {
    [[ELBroadcastHeightGaugeBleManager shareManager] startScanAll];
}

- (void)deinitBle {
    [[ELBroadcastHeightGaugeBleManager shareManager] stopScan];
    [ELBroadcastHeightGaugeBleManager shareManager].heightGaugeDelegate = nil;
}

/**Callback Bluetooth status(回调蓝牙状态) */
- (void)broadcastHeightGaugeUpdateState:(ELBluetoothState)state {
    if (state == ELBluetoothStateAvailable) {
        self.bleStatusLabel.text = @"Connecting";
    } else if (state == ELBluetoothStateUnavailable) {
        self.bleStatusLabel.text = @"Bluetooth is disconnected";
    }

}

/** 回调身高数据 */
- (void)broadcastHeightGaugeDeviceDataModel:(ELBroadcastHeightGaugeBleDataModel *)model {
    
    static NSInteger serialNumber = 0;
    
    if (model.testState == BroadcastHeightGuage_TestState_Stable &&
        serialNumber != model.serialNumber) {
        
        NSString *log = [NSString stringWithFormat:@"MAC:%@ \nCID:0x%04X(%d) VID:0x%04X(%d) PID:0x%04X(%d)\n Height: %@ weightPoint: %@ heightUnit: %@", model.mac, model.cid - 0xFFFF, model.cid - 0xFFFF, model.vid, model.vid, model.pid, model.pid, @(model.height), @(model.heightPoint), AiLinkBleHeightUnitDic[@(model.heightUnit)]];
        [self addLog:log];
        
        serialNumber = model.serialNumber;
        
    } else if (model.testState == BroadcastHeightGuage_TestState_Failed && serialNumber != model.serialNumber) {
        
        NSString *log = @"测量出错啦";
        [self addLog:log];
        
        serialNumber = model.serialNumber;
    } else {
        
    }
    
    
    switch (model.testState) {
        case BroadcastHeightGuage_TestState_Measuring: {
            self.bleStatusLabel.text = @"Measuring";
            break;
        }
        case BroadcastHeightGuage_TestState_Stable: {
            self.bleStatusLabel.text = @"Stable";
            break;
        }
        case BroadcastHeightGuage_TestState_Failed: {
            self.bleStatusLabel.text = @"Failed";
            break;
        }
    }


}

#pragma mark - Log

- (void)addLog:(NSString *)log {
    [self addLog:log newline:YES];
}

- (void)addWithoutNewlineLog:(NSString *)log {
    [self addLog:log newline:NO];
}

- (void)addLog:(NSString *)log newline:(BOOL)newline {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"HH:mm:ss.SSS";
    NSString *time = [format stringFromDate:[NSDate date]];
    if (newline) {
        self.logTextView.text = [self.logTextView.text stringByAppendingFormat:@"%@ %@\n", time, log];
    } else {
        self.logTextView.text = [self.logTextView.text stringByAppendingFormat:@"%@ %@", time, log];
    }
    [self.logTextView scrollRangeToVisible:NSMakeRange(self.logTextView.text.length, 1)];
}


@end
