//
//  RDSUserInfoModel.m
//  Temperature
//
//  Created by RD on 2022/11/24.
//

#import "RDSUserInfoModel.h"
#import "UIImageView+webImage.h"

@implementation RDSUserInfoModel

+ (void)rds_getCurrentUserInfoFinished:(void (^)(NSError *))finished{
    
    [RDSDemoApiHelper rds_getCurrentUserInfoSuccess:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
           
            NSDictionary *data = responseObject[@"data"];
            TheDataManager.currentUser = [RDSUserInfoModel mj_objectWithKeyValues:data];
            TheDataManager.phone = TheDataManager.currentUser.phone;
            TheDataManager.isSetPassword = TheDataManager.currentUser.set_password;// 用于退出登录后，显示密码登录界面，还是手机验证码登录界面
            // 当前家庭ID
            if(kNULLString(TheDataManager.current_home_id)){
                TheDataManager.current_home_id = TheDataManager.currentUser.default_home_id;
            }
            BLOCK_SAFE_RUN(finished, nil)
        } else{
            BLOCK_SAFE_RUN(finished, nil)
        }
        
    } failure:^(NSError *error) {
        BLOCK_SAFE_RUN(finished, error)
    }];
}

// 初始化，防止拿到null崩溃
+ (RDSUserInfoModel *)rds_allocInit{
    RDSUserInfoModel *m = [[RDSUserInfoModel alloc] init];
    m.record_id = @"";
    m.user_name = @"";
    m.password = @"";
    m.phone = @"";
    m.status = @"";
    m.photo = @"";
    m.default_home_id = @"";
    m.created_at = @"";
    return m;
}

- (void)setPhoto:(NSString *)photo{
    NSString *url = [NSString stringWithFormat:@"%@%@",TheApiHelper.baseUrlHeadImg,photo];
    if(!kNULLString(photo) && ![url isEqualToString:_photo]){
        // 头像 @"https://app.yongxulvjian.com/s/yongxu/1pw4ums5i76cpev21uz4cdc2002hywvh/cropped-710501474.jpg"
        _photo = url;
        
        RDS_WEAKSELF(weakSelf)
        UIImageView *imgView = [[UIImageView alloc] init];
        [imgView rds_downloadImage:url placeHoler:[UIImage imageNamed:@"icon_default_user_1"] success:^(SDImageCacheType cacheType, UIImage *image) {
            weakSelf.headImg = image;
            TheDataManager.currentUser.headImg = image;
        } failure:^(NSError *error) {
            weakSelf.headImg = nil;
        } received:^(CGFloat progress) {

        }];
    }
}

- (void)rds_getHeadImgFinished:(void (^)(NSError *))finished{
    RDS_WEAKSELF(weakSelf)
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView rds_downloadImage:_photo placeHoler:[UIImage imageNamed:@"icon_default_user_1"] success:^(SDImageCacheType cacheType, UIImage *image) {
        weakSelf.headImg = image;
        TheDataManager.currentUser.headImg = image;
        BLOCK_SAFE_RUN(finished,nil)
    } failure:^(NSError *error) {
        weakSelf.headImg = [UIImage imageNamed:@"icon_default_user_1"];
        BLOCK_SAFE_RUN(finished,nil)
    } received:^(CGFloat progress) {
        
    }];
}
@end
