//
//  RDSMyVC.m
//  Temperature
//
//  Created by RD on 2022/11/2.
//

#import "RDSMyVC.h"
#import "RDSHomeListVC.h"
#import "UIView+RDSClip.h"
#import "UIImage+Extension.h"
#import "RDSUserProtocolViewController.h"
#import "RDSMyInfoVC.h"
#import "RDSHomeModel.h"
#import "SPAlertController.h"
#import "RDSSystemSetVC.h"

@interface RDSMyVC ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *headBtn;
@property (weak, nonatomic) IBOutlet UILabel *userNameLab;
@property (weak, nonatomic) IBOutlet UILabel *userIdLab;
@property (weak, nonatomic) IBOutlet UIView *myHomeView;
@property (weak, nonatomic) IBOutlet UILabel *homeNameLab;
@property (weak, nonatomic) IBOutlet UIButton *myHomeBtn;

@property (nonatomic, strong) SPAlertController *homeAlert;
@property (nonatomic, strong) RDSHomeModel *selectHome;
@property (nonatomic, strong) UIPickerView *homePick;
@property (nonatomic, strong) UIButton *defaultHomeBtn;
@end

@implementation RDSMyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"我的";
    
    [self setUI];
    
    [kNotificationCenter addObserver:self selector:@selector(p_refreshUserInfo)name:RDSUpdateUserInfoNotice object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self p_getHomeList];
}

- (void)setUI{
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self p_refreshUserInfo];
    [self p_setupHomeAlert];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

    [_myHomeView rds_setCornerRadiusWithType:Corner_Radius_Or_BorderLine_UP Radius:20];
    _myHomeView.backgroundColor = UIColor.clearColor;
    UIImage *img = [UIImage rds_imageWithColor:RGB(183, 231, 233) toColor:RGB(7, 185, 185) imgSize:_myHomeBtn.frame.size];
    [_myHomeBtn setBackgroundImage:img forState:UIControlStateNormal];
    
}

- (void)p_refreshUserInfo{
    _userNameLab.text = TheDataManager.currentUser.user_name;
    _userIdLab.text = [NSString stringWithFormat:@"账号:%@",TheDataManager.currentUser.phone];
    [_headBtn setImage:TheDataManager.currentUser.headImg forState:UIControlStateNormal];
}

- (void)p_getHomeList{
    
    RDS_WEAKSELF(weakSelf)
    [RDSHomeModel rds_getHomeListIsAdmin:NO finished:^(NSError * _Nonnull error) {
        
        weakSelf.homeNameLab.text = TheDataManager.current_home_name;
        [weakSelf.homePick reloadAllComponents];
    }];
}

/// 头像
- (IBAction)onHeadClick {
    RDSMyInfoVC *vc = [[RDSMyInfoVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}


/// 切换家庭
- (IBAction)onMyHomeClick {
     
    _selectHome = TheDataManager.homes[TheDataManager.current_home_index];
    [self.homePick selectRow:TheDataManager.current_home_index inComponent:0 animated:NO];
    [self presentViewController:self.homeAlert animated:YES completion:nil];
    _defaultHomeBtn.selected = NO;
}


/// 家庭管理
- (IBAction)onHomeListClick {
    
    RDSHomeListVC *vc = [[RDSHomeListVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}


/// 我的场景
- (IBAction)onSceneListClick {
    RDSSystemSetVC *vc = [[RDSSystemSetVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}


/// 帮助与反馈
- (IBAction)onHelpClick {
    
    RDSUserProtocolViewController *vc = [[RDSUserProtocolViewController alloc] init];
    vc.vcTitle = @"帮助与反馈";
    vc.url = kHelpList;
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}


/// 系统设置
- (IBAction)onSetClick {
    
    RDSSystemSetVC *vc = [[RDSSystemSetVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}

- (void)onSelectHome{
    
    if(_defaultHomeBtn.isSelected){
        [RDSHudShower showWithStatus:@"切换中..."];
        
        [RDSDemoApiHelper rds_setDefaultHome:_selectHome.record_id success:^(id responseObject) {
            
            if ([responseObject[@"code"] intValue] == 0) {
                [RDSHudShower dismissHUD];
            } else{
                [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
            }
        } failure:^(NSError *error) {
            [RDSHudShower showBottomToast:@"连接服务器失败"];
        }];
    }
    
    TheDataManager.current_home_id = _selectHome.record_id;
    TheDataManager.current_home_name = _selectHome.name;
    _homeNameLab.text = _selectHome.name;
}


- (void)p_setupHomeAlert{
    RDS_WEAKSELF(weakSelf)
    SPAlertController *alertController = [SPAlertController alertControllerWithTitle:@"切换家庭" message:@"" preferredStyle:SPAlertControllerStyleAlert animationType:SPAlertAnimationTypeDefault];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-56*2, 200)];
    
    
    UIPickerView *homePick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-56*2, 180)];
    homePick.delegate = self;
    homePick.dataSource = self;
    self.homePick = homePick;
    [homePick selectRow:TheDataManager.current_home_index inComponent:0 animated:NO];
    [view addSubview:homePick];
    
    UIView *defaultHome = [[UIView alloc] initWithFrame:CGRectMake(80, 170, 100, 20)];
    UIButton *btnImg = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [btnImg setImage:[UIImage imageNamed:@"icon_check"] forState:UIControlStateNormal];
    [btnImg setImage:[UIImage imageNamed:@"icon_check_sel"] forState:UIControlStateSelected];
    self.defaultHomeBtn = btnImg;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 0, 90, 20)];
    [btn setTitleColor:RDSGreenColor forState:UIControlStateNormal];;
    [btn setTitle:@"设为默认家庭" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [btnImg addTarget:self action:@selector(p_setDefaultHome) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(p_setDefaultHome) forControlEvents:UIControlEventTouchUpInside];
    
    [defaultHome addSubview:btnImg];
    [defaultHome addSubview:btn];
    
    [view addSubview:defaultHome];
    
    
    // 插入一个view
    [alertController insertComponentView:view];
    
    SPAlertAction *action1 = [SPAlertAction actionWithTitle:@"取消" style:SPAlertActionStyleDefault handler:nil];
    SPAlertAction *action2 = [SPAlertAction actionWithTitle:@"确定" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
        [weakSelf onSelectHome];
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    _homeAlert = alertController;
}

- (void)p_setDefaultHome{
    _defaultHomeBtn.selected = !_defaultHomeBtn.isSelected;
}

#pragma mark pickerView  delegate
//滚动选中的行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if(TheDataManager.homes.count == 0){
        _selectHome = [[RDSHomeModel alloc] init];
        return;
    }
    _selectHome = TheDataManager.homes[row];
}

//每行显示的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    RDSHomeModel *hm = TheDataManager.homes[row];
    return hm.name;
}

//列宽
//-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
//
//    return 100;
//}

#pragma mark pickerView  dataSource
// 多少组
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// 行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return TheDataManager.homes.count;
}


- (void)dealloc{
    DDLog(@"dealloc~~");
    [kNotificationCenter removeObserver:self];
}

@end
