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

#import "RDSMyInfoVC.h"
#import "RDSRootControl.h"
#import "LCActionSheet.h"
#import "RDSImagePicker.h"
#import "RDSSetPwdVC.h"
#import "RDSResetPwdVC.h"


@interface RDSMyInfoVC ()<LCActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIButton *headBtn;
@property (weak, nonatomic) IBOutlet UILabel *userNameLab;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;

@end

@implementation RDSMyInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"个人信息";
    
    [self p_getCurrentUserInfo];
    [self refreshUserInfoUI];
}

- (void)p_getCurrentUserInfo{
    
    RDS_WEAKSELF(weakSelf)
    [RDSUserInfoModel rds_getCurrentUserInfoFinished:^(NSError *error) {
        if (!error) {
            [weakSelf refreshUserInfoUI];
        }
    }];
    
    
}

- (void)refreshUserInfoUI{
    
    RDS_WEAKSELF(weakSelf)
    dispatch_async(dispatch_get_main_queue(), ^{
        
        weakSelf.userNameLab.text = TheDataManager.currentUser.user_name;
        weakSelf.phoneLab.text = TheDataManager.currentUser.phone;
        
        if (!TheDataManager.currentUser.headImg) {
            [TheDataManager.currentUser rds_getHeadImgFinished:^(NSError *error) {
                [weakSelf.headBtn setImage:TheDataManager.currentUser.headImg forState:UIControlStateNormal];
            }];
        }else{
            [weakSelf.headBtn setImage:TheDataManager.currentUser.headImg forState:UIControlStateNormal];
        }
    
    });
}

/// 点击头像
- (IBAction)onHeadClick {
    
    LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:nil
                                                  buttonTitles:@[@"拍照", @"从手机相册选择"]
                                                redButtonIndex:-1
                                                      delegate:self];
    [actionSheet show];
}

/// 点击用户名
- (IBAction)onUserNameClick {
    
    RDS_WEAKSELF(weakSelf)
    [self rds_alertInputTitle:@"设置用户名" msg:nil placeholder:TheDataManager.currentUser.user_name doneAction:^(NSString *name) {
        [weakSelf p_updateUserInfoChangeName:name photoUrl:nil];
        TheDataManager.currentUser.user_name = name;
    }];
}

/// 点击手机号
- (IBAction)onPhoneClick {
    
}


// 设置密码
//1、未设置过密码 跳转到 设置密码 页面
//2、设置过密码 跳转到 重置密码 页面
- (IBAction)setPwdAction:(id)sender {
    
    if(TheDataManager.currentUser.set_password){
        RDSResetPwdVC *vc = [[RDSResetPwdVC alloc] init];
        [self pushViewController:vc animated:YES];
    }
    else{
        RDSSetPwdVC *vc = [[RDSSetPwdVC alloc] init];
        [self pushViewController:vc animated:YES];
    }
    
}

// 注销账户
- (IBAction)unsubscribeAction:(id)sender {
    
    RDS_WEAKSELF(weakSelf)
    [TheAlertCtrl rds_alertWithTitle:@"提示" message:@"注销账户将清空用户所有数据，确认注销？" cancel:@"取消" sure:@"注销账户" cancelColor:nil sureColor:RDSRedColor cancelAction:nil sureAction:^{
        [weakSelf p_unsubscribe];
    }];

    
}

// 注销账户
- (void)p_unsubscribe{
    [RDSDemoApiHelper rds_unsubscribeSuccess:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
           
            [RDSRootControl shareControl].isLoginSuccess = NO;
            
        } else{
            [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
        }
        
    } failure:^(NSError *error) {
        [RDSHudShower showBottomToast:@"连接服务器失败"];
    }];
}

// 退出登录
- (IBAction)logoutAction:(id)sender {
    
    RDS_WEAKSELF(weakSelf)
    [RDSHudShower showWithStatus:@"加载中..." autoDismiss:NO];
    [RDSDemoApiHelper rds_logoutSuccess:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
            [RDSHudShower dismissHUD];
            [RDSRootControl shareControl].isLoginSuccess = NO;
        } else{
            [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
        }
        
    } failure:^(NSError *error) {
        [RDSHudShower showBottomToast:@"连接服务器失败"];
        [RDSRootControl shareControl].isLoginSuccess = NO;
    }];
    
    TheDataManager.token = @"";
}


#pragma mark - LCActionSheetDelegate
- (void)actionSheet:(LCActionSheet *)actionSheet didClickedButton:(UIButton *)button {
    
    if (button.tag >= 2) return; // 取消
    
    RDS_WEAKSELF(weakSelf)
    EImageSourceType type = button.tag == 1 ? EImageFromPhoto : EImageFromCamera;
    [[RDSImagePicker sharePicker] rds_pickImageFrom:type needEdit:YES complected:^(UIImage *pickImage) {
        if (pickImage) {
            [weakSelf p_uploadUserPic:pickImage];
        }
    }];
}

// 上传头像
- (void)p_uploadUserPic:(UIImage *)userPic {
    
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_postImage:userPic success:^(id responseObject) {

        if ([responseObject[@"code"] intValue] == 0) {

            NSDictionary *data = responseObject[@"data"];
            NSString *url = data[@"url"];
            TheDataManager.currentUser.photo = url;
            TheDataManager.currentUser.headImg = userPic;
            [weakSelf p_updateUserInfoChangeName:nil photoUrl:url];
            [RDSHudShower showCenterToast:@"设置成功"];

        } else{
            [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
        }

    } failure:^(NSError *error) {
        [RDSHudShower showBottomToast:@"连接服务器失败"];
    }];


    return;
    
}

- (void)p_updateUserInfoChangeName:(NSString *)userName photoUrl:(NSString *)photoUrl{
    
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_updateUserInfoChangeUserName:userName photoUrl:photoUrl defaultHomeId:nil isSetPwd:NO success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 0) {
            [weakSelf refreshUserInfoUI];
            [RDSHudShower showCenterToast:@"设置成功"];
            [kNotificationCenter postNotificationName:RDSUpdateUserInfoNotice object:nil];
        } else{
            [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
        }
        
    } failure:^(NSError *error) {
        [RDSHudShower showBottomToast:@"连接服务器失败"];
    }];
}

@end
