//
//  RDSHomeModel.m
//  Temperature
//
//  Created by RD on 2022/12/15.
//

#import "RDSHomeModel.h"

@implementation RDSHomeModel

+ (void)rds_getHomeListIsAdmin:(BOOL)is_admin finished:(void (^)(NSError * ))finished{
    
    [RDSDemoApiHelper rds_getHomeListIsAdmin:is_admin success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
           
            NSArray *data = responseObject[@"data"];
            NSMutableArray *homes = [RDSHomeModel mj_objectArrayWithKeyValuesArray:data];
            
            if(is_admin){
                TheDataManager.homesAdmin = homes.mutableCopy;
            }else{
                TheDataManager.homes = homes.mutableCopy;
            }
            
            BOOL hasSelectHome = NO;
            for (int i = 0; i<homes.count; i++) {
                RDSHomeModel *home = homes[i];
                if([home.record_id isEqualToString:TheDataManager.current_home_id]){
                    TheDataManager.current_home_name = home.name;
                    TheDataManager.current_home_index = i;
                    hasSelectHome = YES;
                    break;
                }
            }
            if(hasSelectHome == NO){
                RDSHomeModel *home = homes.firstObject;
                TheDataManager.current_home_name = home.name;
                TheDataManager.current_home_index = 0;
            }
            
            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)
    }];
}

- (void)setMember:(NSMutableArray<RDSMemberModel *> *)member{
    if(member.count == 0) return;
    
    if([member.firstObject isKindOfClass:[RDSMemberModel class]]){
        _member = member;
    }else{
        NSMutableArray *memberArr = [RDSMemberModel mj_objectArrayWithKeyValuesArray:member];
        _member = memberArr.mutableCopy;
    }
}

+ (void)rds_getHomeDetailWithHomeId:(NSString *)homeId finished:(void (^)(RDSHomeModel *, NSError *))finished{
    
    [RDSDemoApiHelper rds_getHomeDetailWithHomeId:(NSString *)homeId success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
           
            NSDictionary *data = responseObject[@"data"];
            RDSHomeModel *home = [RDSHomeModel mj_objectWithKeyValues:data];
            
            home.member = [RDSMemberModel mj_objectArrayWithKeyValuesArray:data[@"member"]];
            home.room = [RDSRoomModel mj_objectArrayWithKeyValuesArray:data[@"room"]];
            
            if ([home.record_id isEqualToString:TheDataManager.current_home_id]){
                TheDataManager.detailHome = home;
                TheDataManager.rooms = home.room.mutableCopy;
                TheDataManager.gateway = home.gateway;
                TheDataManager.current_home_name = home.name;
            }
            BLOCK_SAFE_RUN(finished,home, nil)
        } else{
            NSError *error = [NSError errorWithDomain:responseObject[@"message"] code:[responseObject[@"code"] intValue] userInfo:nil];
            BLOCK_SAFE_RUN(finished,nil, error)
        }
    } failure:^(NSError *error) {
        
    }];
}
@end
