//
//  ViewController.m
//  AILinkBleSDKSourceCode
//
//  Created by iot_user on 2020/4/7.
//  Copyright © 2020 IOT. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"

#import <AILinkBleSDK/ELAILinkBleManager.h>
#import <AILinkBleSDK/ELBluetoothManager.h>
#import "ELDemoDeviceModel.h"
#import "ELDeviceTypeCell.h"
#import "ELDemoScanVC.h"

#import "AiLinkSuperViewController.h"
#import "InheritScanViewController.h"
#import "BabyScaleViewController.h"
#import "BloodScanViewController.h"
#import "ELWIFIBloodPressureVC.h"
#import "HeightGuageScanViewController.h"
#import "BroadcastHeightGuage.h"
#import "RemoteControlScanViewController.h"
#import "ForeheadScanViewController.h"
#import "ThermometerScanViewController.h"
#import "WheelMonitorScanViewController.h"
#import "BodyFatScaleScanViewController.h"
#import "BodyScaleVC.h"
#import "BroadcastScaleViewController.h"
#import "EightScaleScanViewController.h"
#import "ToothbrushScanViewController.h"
#import "ELBfsWifiScanVC.h"
#import "OximeterScanViewController.h"
#import "BloodSugarScanViewController.h"
#import "CoffeeScaleScanViewController.h"
#import "FoodThermometerScanViewController.h"
#import "BroadcastNutritionScaleViewController.h"
#import "NutritionScaleScanViewController.h"
#import "AiFreshNutritionScaleScanViewController.h"
#import "FaceMaskScanViewController.h"
#import "SkipScanViewController.h"
#import "HeightBodyFatScaleVC.h"


#import "MeatProbeVC.h"
#import "MeatProbeBoxVC.h"
#import "HygrothermographVC.h"
#import "MeatProbeScreenBoxVC.h"
#import "MultipleMeatProbeBoxVC.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

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

@property (nonatomic, strong) NSArray<ELDemoDeviceModel *> *deviceArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = [NSString stringWithFormat:@"%@", self.class.appName];
    self.versionLabel.text = [NSString stringWithFormat:@"SDK ver: %@ \nDemo ver: %@", ELAILinkBleManager.sdkVersion, self.class.appVersion];
    
    self.deviceArray = @[
        
        [ELDemoDeviceModel modelWithIndex:@"-" imageName:@"ailink_connected_ic" title:@"Parse data yourself" subtitle:@"有AILink协议自己解析数据" entryVCName:NSStringFromClass(InheritScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"04" imageName:@"type_baby_scale_ic" title:@"Baby Scale" subtitle:@"婴儿秤" entryVCName:NSStringFromClass(BabyScaleViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"05" imageName:@"type_sphygmomanometer_ic" title:@"Sphygmomanometer" subtitle:@"血压计" entryVCName:NSStringFromClass(BloodScanViewController.class)],
        
        [ELDemoDeviceModel modelWithIndex:@"05B" imageName:@"type_sphygmomanometer_ic" title:@"WIFI_BLE Sphygmomanometer" subtitle:@"血压计_WIFI_BLE" entryVCName:NSStringFromClass(ELWIFIBloodPressureVC.class) cids:@[@(ELSupportDeviceTypeBLE_WIFIBlood)]],
        
        [ELDemoDeviceModel modelWithIndex:@"06" imageName:@"type_height_measuring_instrument" title:@"Height Guage" subtitle:@"身高仪" entryVCName:NSStringFromClass(HeightGuageScanViewController.class)],
        
        [ELDemoDeviceModel modelWithIndex:@"06B" imageName:@"type_height_measuring_instrument" title:@"Broadcast Height Guage" subtitle:@"广播身高仪" entryVCName:NSStringFromClass(BroadcastHeightGuage.class)],
        
        [ELDemoDeviceModel modelWithIndex:@"07" imageName:@"type_control_vision_ic" title:@"Remote Control" subtitle:@"遥控器" entryVCName:NSStringFromClass(RemoteControlScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"08" imageName:@"type_temperature_gun_ic" title:@"Forehead thermometer" subtitle:@"额温枪" entryVCName:NSStringFromClass(ForeheadScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"09" imageName:@"type_thermometer_ic" title:@"Digital thermometer" subtitle:@"温度计" entryVCName:NSStringFromClass(ThermometerScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"10" imageName:@"type_tpms_ic" title:@"TPMS" subtitle:@"胎压监测" entryVCName:NSStringFromClass(WheelMonitorScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"11" imageName:@"type_body_fat_ic" title:@"Bluetooth BodyfatScale" subtitle:@"体脂秤" entryVCName:NSStringFromClass(BodyFatScaleScanViewController.class)],
        
        [ELDemoDeviceModel modelWithIndex:@"11B" imageName:@"type_body_fat_ic" title:@"Bluetooth BodyScale (without body fat)" subtitle:@"体重秤" entryVCName:NSStringFromClass(BodyScaleVC.class) cids:@[@(ELSupportDeviceTypeBodyScale)]],
        
        [ELDemoDeviceModel modelWithIndex:@"12" imageName:@"type_body_fat_ic" title:@"Bluetooth BroadcastScale" subtitle:@"广播秤" entryVCName:NSStringFromClass(BroadcastScaleViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"13" imageName:@"8_electrode_measurement_type" title:@"Eight-electrode scale" subtitle:@"八电极体脂秤" entryVCName:NSStringFromClass(EightScaleScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"14" imageName:@"type_brush" title:@"wifi-ble toothbrush" subtitle:@"WiFi-ble牙刷" entryVCName:NSStringFromClass(ToothbrushScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"15" imageName:@"type_body_fat_ic" title:@"wifi-ble bodyfatScale" subtitle:@"WiFi-ble体脂秤" entryVCName:NSStringFromClass(ELBfsWifiScanVC.class)],
        [ELDemoDeviceModel modelWithIndex:@"16" imageName:@"oximeter_search_icon1" title:@"Bluetooth Oximeter" subtitle:@"血氧仪" entryVCName:NSStringFromClass(OximeterScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"17" imageName:@"type_blood_sugar" title:@"Bluetooth BloodGlucoseMeter" subtitle:@"血糖仪" entryVCName:NSStringFromClass(BloodSugarScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"18" imageName:@"type_coffee_scale_ic" title:@"CoffeeScale" subtitle:@"咖啡秤" entryVCName:NSStringFromClass(CoffeeScaleScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"19" imageName:@"foodtem_icon_100" title:@"FoodThermometer" subtitle:@"食物温度计" entryVCName:NSStringFromClass(FoodThermometerScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"20" imageName:@"ailink_nutrition_scale_scale_connect" title:@"BrodcastNutritionScale" subtitle:@"广播营养秤" entryVCName:NSStringFromClass(BroadcastNutritionScaleViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"21" imageName:@"ailink_nutrition_scale_scale_connect" title:@"NutritionScale" subtitle:@"连接营养秤" entryVCName:NSStringFromClass(NutritionScaleScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"22" imageName:@"ailink_nutrition_scale_scale_connect" title:@"AiFreshNutritionScale" subtitle:@"好营养营养秤" entryVCName:NSStringFromClass(AiFreshNutritionScaleScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"23" imageName:@"type_smart_mask" title:@"FaceMask" subtitle:@"智能口罩" entryVCName:NSStringFromClass(FaceMaskScanViewController.class)],
        [ELDemoDeviceModel modelWithIndex:@"24" imageName:@"ailink_smart_skip_rope_device_ic" title:@"SkippingRope" subtitle:@"跳绳" entryVCName:NSStringFromClass(SkipScanViewController.class)],
        
        [ELDemoDeviceModel modelWithIndex:@"25" imageName:@"height_body_fat_scale_type_ic" title:@"HeightBodyFatScale" subtitle:@"身高体脂秤" entryVCName:NSStringFromClass(HeightBodyFatScaleVC.class) cids:@[@(ELSupportDeviceTypeHeightBodyFatScale)]],
        
        [ELDemoDeviceModel modelWithIndex:@"26" imageName:@"meat_probe_type_ic" title:@"MeatProbe" subtitle:@"食物探针" entryVCName:NSStringFromClass(MeatProbeVC.class) cids:@[@(ELSupportDeviceTypeMeatProbe)]],
        [ELDemoDeviceModel modelWithIndex:@"27" imageName:@"ailink_cid85_type_ic" title:@"MeatProbeBox" subtitle:@"食物探针盒子" entryVCName:NSStringFromClass(MeatProbeBoxVC.class) cids:@[@(ELSupportDeviceTypeMeatProbeBox)]],
        
        [ELDemoDeviceModel modelWithIndex:@"27B" imageName:@"ailink_cid85_banding_cid_0062_100_ic" title:@"MeatProbeScreenBox" subtitle:@"食物探针带屏盒子" entryVCName:NSStringFromClass(MeatProbeScreenBoxVC.class) cids:@[@(0x0062)]],
        [ELDemoDeviceModel modelWithIndex:@"27C" imageName:@"aibbq_add_equipment_probe_100_ic" title:@"MultipleMeatProbeBox" subtitle:@"多食物探针盒子" entryVCName:NSStringFromClass(MultipleMeatProbeBoxVC.class) cids:@[@(0x006B)]],
        
        [ELDemoDeviceModel modelWithIndex:@"28" imageName:@"Hygrothermograph_add_icon2_100" title:@"Hygrothermograph" subtitle:@"温湿度计" entryVCName:NSStringFromClass(HygrothermographVC.class) cids:@[@(ELSupportDeviceTypeHygrothermograph)]],
    ];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.deviceArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ELDemoDeviceModel *device = self.deviceArray[indexPath.row];
    
    ELDeviceTypeCell *cell = [ELDeviceTypeCell subsribeCell:tableView];
    cell.indexLabel.text = device.index;
    cell.titleLabel.text = device.titleText;
    cell.subtitleLabel.text = device.subtitleText;
    cell.iconImageView.image = [UIImage imageNamed:device.imageName];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ELDemoDeviceModel *device = self.deviceArray[indexPath.row];
    if (device.newType) {
        ELDemoScanVC *vc = [[ELDemoScanVC alloc] init];
        vc.cids = device.cids;
        vc.demoDeviceModel = device;
//        vc.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.navigationController presentViewController:vc animated:YES completion:^{
            NSLog(@"");
        }];
        
        __weak typeof(self) weakSelf = self;
        vc.selectedBlock = ^(ELAILinkPeripheral * _Nonnull per) {
            UIViewController *vc = [[NSClassFromString(device.entryVCName) alloc] init];
            vc.title = [NSString stringWithFormat:@"%@_%@", device.titleText, device.subtitleText];
            [vc setValue:per forKey:@"per"];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    } else {
        UIViewController *vc = [[NSClassFromString(device.entryVCName) alloc] init];
        vc.title = [NSString stringWithFormat:@"%@_%@", device.titleText, device.subtitleText];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

+ (NSString *)appName {
    NSDictionary *dic = [[NSBundle mainBundle] infoDictionary];
    NSString *appName = [dic objectForKey:@"CFBundleDisplayName"];
    return appName;
}

+ (NSString *)appVersion {
    NSDictionary *dic = [[NSBundle mainBundle] infoDictionary];
    NSString * version = [dic objectForKey:@"CFBundleShortVersionString"];
    return version;
}

@end
