//
//  RDSDeviceTypeModel.m
//  Temperature
//
//  Created by 姓二名哈字富贵儿 on 2023/8/17.
//

#import "RDSDeviceTypeModel.h"

@implementation RDSDeviceTypeModel

- (void)setCode:(NSString *)code {
    _code = code;
    if ([code isEqualToString:@"YXK-Z/86-FG-A"] || [code isEqualToString:@"YXK-Z/86-FC-B"]) {
        _deviceType = RDSDeviceType_MasterController;
    } else if ([code isEqualToString:@"YXK-F/86-FG-A"] || [code isEqualToString:@"YXK-F/86-FC-B"]) {
        _deviceType = RDSDeviceType_SubController;
    } else if ([code isEqualToString:@"YXK-P/DN-JF-8"]) {
        _deviceType = RDSDeviceType_IntelligentWaterCollector;
    } else if([code isEqualToString:@"INFRARED"]) {
        _deviceType = RDSDeviceType_YongXuSpirit;
    }
}

- (NSString * _Nullable)getDeviceNameWithIndex:(NSInteger)index {
    if (index < self.deviceTypeList.count) {
        RDSDeviceTypeModel *deviceTypeModel = self.deviceTypeList[index];
        return deviceTypeModel.name;
    }
    return nil;
}

- (void)rds_fetchDeviceTypeListFinished:(void (^)(NSError * _Nullable))finished {
    [RDSDemoApiHelper rds_getDeviceTypeListSuccess:^(id responseObject) {
        NSLog(@"responseObject is %@", responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            NSDictionary *data = responseObject[@"data"];
            [RDSDeviceTypeModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{@"children": @"RDSDeviceTypeModel"};
            }];
            self->_deviceTypeList = [RDSDeviceTypeModel mj_objectArrayWithKeyValuesArray:data];
            BLOCK_SAFE_RUN(finished, nil)
        } else {
            NSError *error = [NSError errorWithDomain:responseObject[@"message"] code:[responseObject[@"code"] intValue] userInfo:nil];
            BLOCK_SAFE_RUN(finished, error)
        }
    } failure:^(NSError *error) {
        BLOCK_SAFE_RUN(finished, error)
    }];
}

@end
