//
//  SLSexSetVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/5/31.
//

#import "SLSexSetVc.h"
#import "SLBaseCellView.h"

@interface SLSexSetVc ()

@property (nonatomic, strong) NSString *currentSelSex;

@end

@implementation SLSexSetVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"设置性别";
    YMBarButtonItem *rightBarItem = [[YMBarButtonItem alloc] initWithTitle:@"完成  " target:self action:@selector(preserveBtnClick)];
    rightBarItem.titleInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    self.navigationBar.rightBarButtonItem = rightBarItem;
    [self confignSexSetView];
    self.currentSelSex = [SLGlobalInfo SharedInstance].roleModel.aiSex;
    [self updateSexSetView];
}

- (void)updateSexSetView {
    [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:SLBaseCellView.class]) {
            SLBaseCellView *baseCell = obj;
            baseCell.leftImageView.image = ImageName(@"check_circle");
            if ([baseCell.titleLabel.text isEqualToString:self.currentSelSex]) {
                baseCell.leftImageView.image = ImageName(@"check_circle_filled");
            }
        }
    }];
}

- (void)confignSexSetView {
    NSArray *arr = @[[SLPageModelDetail initWithTitle:@"男" bgImage:@"check_circle"],
      [SLPageModelDetail initWithTitle:@"女" bgImage:@"check_circle"],];
    [arr enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SLBaseCellView *baseCell = [[SLBaseCellView alloc] initWithFrame:CGRectZero];
        baseCell.titleLabel.text = obj.title;
        baseCell.subTitleLabel.text = @"";
        baseCell.leftImageView.image = ImageName(obj.bgImage);
        baseCell.rightImageViw.hidden = YES;
        baseCell.tag = 8888 + idx;
        WS(weakSelf);
        __block SLBaseCellView *blockBaseCell = baseCell;
        [baseCell addTapWithBlock:^{
            blockBaseCell.contentView.backgroundColor = SLColSel;
            [UIView animateWithDuration:0.5 animations:^{
                blockBaseCell.contentView.backgroundColor = [UIColor whiteColor];
            }];
            weakSelf.currentSelSex = idx ? @"女" : @"男";
            [weakSelf updateSexSetView];
        }];
        baseCell.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:baseCell];
        CGFloat top = 56 * idx * KScaleW + kNavBarHeight + 10;
        [baseCell mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.mas_equalTo(56 * KScaleW);
            make.top.equalTo(self.view).offset(top);
        }];
        
    }];
}

- (void)preserveBtnClick {
    [[SLHttpCenter SharedInstance] getWithUrl:@"/api/User/UpdateAIGender" parameter:@{@"aiGender" : self.currentSelSex} success:^(id responseObject) {
        [ZFToast ShowWithMessage:@"保存成功"];
        RoleModel *newRoleModel = [SLGlobalInfo SharedInstance].roleModel;
        newRoleModel.aiSex = self.currentSelSex;
        [SLGlobalInfo SharedInstance].roleModel = newRoleModel;
    } failure:^(SPRequestError *error) {
        
    }];
}

@end
