//
//  RDSAddRoomVC.m
//  Temperature
//
//  Created by RD on 2022/11/19.
//

#import "RDSAddRoomVC.h"
#import "RDSInputWiFiVC.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "OPQRCodeViewController.h"

#import "RDSBindSubDeviceVC.h"// 添加分控

@interface RDSAddRoomVC ()<OPQRCodeViewControllerDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *homeNameLab;
@property (weak, nonatomic) IBOutlet UITextField *roomNameTF;
@property (weak, nonatomic) IBOutlet UITextField *deviceIDTF;// 设备名，用来连接设备
@property (weak, nonatomic) IBOutlet UIView *scanDeviceName;

@end

@implementation RDSAddRoomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"添加房间";
    
    // 蓝牙强提醒，没打开，就调用系统弹框提示前往设置
    NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey:@YES};
    CBCentralManager *cbCentralMgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    
    _homeNameLab.text = TheDataManager.current_home_name;
    _roomNameTF.delegate = self;
    _deviceIDTF.delegate = self;
    
    // 如果是红外产品
    if (self.deviceTypeModel.deviceType == RDSDeviceType_YongXuSpirit) {
        _deviceIDTF.text = @"ESP_GATTS_DEMO";
        _scanDeviceName.hidden = true;
    }

}

- (IBAction)onScanBtnClick {
    
    [self.view endEditing:YES];
    
    OPQRCodeViewController *vc = [[OPQRCodeViewController alloc] init];
    vc.delegate = self;
    vc.topOffset = -kNavHeight;
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}

- (IBAction)onNextBtnClick {
    
    if (self.roomNameTF.text.length <= 0) {
        [RDSHudShower showCenterToast:@"输入房间名称"];
        return;
    }
    if (self.deviceIDTF.text.length < 1) {
        [RDSHudShower showCenterToast:@"输入控制面板编号"];
        return;
    }
    
    
    [self.view endEditing:YES];
    
//    if(TheDataManager.rooms.count == 0){
    switch (self.deviceTypeModel.deviceType) {

        case RDSDeviceType_SubController: {
            RDSBindSubDeviceVC *vc = [[RDSBindSubDeviceVC alloc] init];
            vc.roomName = _roomNameTF.text;
            vc.deviceID = _deviceIDTF.text;
            vc.deviceTypeModel = self.deviceTypeModel;
            [self pushViewController:vc animated:YES];
            break;
        }
        default:
        {
            RDSInputWiFiVC *vc = [[RDSInputWiFiVC alloc] init];
            vc.roomName = _roomNameTF.text;
            vc.deviceID = _deviceIDTF.text;
            vc.deviceTypeModel = self.deviceTypeModel;
            [self pushViewController:vc animated:YES];
        }
    }
    
    
}

// 扫码返回字符串
- (void)rdy_didFinishPickingMediaWithInfo:(NSString *)stringValue{
    
    DDLog(@"二维码：%@",stringValue);
    if(!kNULLString(stringValue)){
        self.deviceIDTF.text = stringValue;
    }
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}

@end
