//
//  RDSRoomModel.m
//  Temperature
//
//  Created by RD on 2022/11/30.
//

#import "RDSRoomModel.h"

@implementation RDSRoomModel


+ (void)rds_getRoomListFinished:(void (^)(NSError *))finished{
    
    [RDSDemoApiHelper rds_getRoomListSuccess:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
           
            NSArray *data = responseObject[@"data"];
            NSMutableArray *rooms = [RDSRoomModel mj_objectArrayWithKeyValuesArray:data];
            TheDataManager.rooms = rooms.mutableCopy;
            
            BLOCK_SAFE_RUN(finished, nil)
        } else{
            
            //responseObject[@"message"]
            NSError *error = [NSError errorWithDomain:responseObject[@"message"] code:-1 userInfo:nil];
            BLOCK_SAFE_RUN(finished, error)
        }
        
    } failure:^(NSError *error) {
        BLOCK_SAFE_RUN(finished, error)
    }];
}

+ (void)rds_delRoomId:(NSString *)record_id finished:(void (^)(NSError *))finished{
    
    [RDSDemoApiHelper rds_deleteRoomsRecord_ids:@[record_id] success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
            BLOCK_SAFE_RUN(finished, nil)
        } else{
            BLOCK_SAFE_RUN(finished, nil)
        }
        
    } failure:^(NSError *error) {
        BLOCK_SAFE_RUN(finished, error)
    }];
}


- (void)rds_getRoomDetailFinished:(void (^)(NSError *))finished{
    
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_getRoomDetailRecord_id:self.record_id success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
            NSDictionary *data = responseObject[@"data"];
            weakSelf.air_quality = [data[@"air_quality"] intValue];
            weakSelf.co2 = [data[@"co2"] intValue];
            weakSelf.duration = data[@"duration"];
            weakSelf.fan_speed = [data[@"fan_speed"] intValue];
            weakSelf.fan_value = [data[@"fan_value"] intValue];
            weakSelf.humidity = [data[@"humidity"] intValue];
            weakSelf.mode = [data[@"mode"] intValue];
            weakSelf.power = [data[@"power"] boolValue];
            weakSelf.new_fan = [data[@"new_fan"] boolValue];
            weakSelf.set_temp = [data[@"set_temp"] intValue];
            weakSelf.temperature = [data[@"temperature"] intValue];
            weakSelf.timer_status = [data[@"timer_status"] boolValue];
            
            BLOCK_SAFE_RUN(finished, nil)
        } else{
            BLOCK_SAFE_RUN(finished, nil)
        }
    } failure:^(NSError *error) {
        BLOCK_SAFE_RUN(finished, error)
    }];
}

- (void)rds_getRoomStatusFinished:(void (^)(NSError * ))finished{
    
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_getRoomStatusWithControl_number:self.record_id success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
            NSDictionary *data = responseObject[@"data"];
            weakSelf.air_quality = [data[@"air_quality"] intValue];
            weakSelf.duration = data[@"duration"];
            weakSelf.fan_speed = [data[@"fan_speed"] intValue];
            weakSelf.fan_value = [data[@"fan_value"] intValue];
            weakSelf.humidity = [data[@"humidity"] intValue];
            weakSelf.mode = [data[@"mode"] intValue];
            weakSelf.power = [data[@"power"] boolValue];
            weakSelf.new_fan = [data[@"new_fan"] boolValue];
            weakSelf.set_temp = [data[@"set_temp"] intValue];
            weakSelf.temperature = [data[@"temperature"] intValue];
            weakSelf.timer_status = [data[@"timer_status"] boolValue];
            
            BLOCK_SAFE_RUN(finished, nil)
        } else{
            BLOCK_SAFE_RUN(finished, nil)
        }
        
    } failure:^(NSError *error) {
        BLOCK_SAFE_RUN(finished, error)
    }];
}
@end
