//
//  SLRecomFriendsCell.m
//  SLAiELTS
//
//  Created by Gusont on 2023/4/21.
//

#import "SLRecomFriendsCell.h"

@interface SLRecomFriendsCell()

@property (nonatomic, strong)SLRecomFriendModel *recomFriendModel;
@end

@implementation SLRecomFriendsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.interestcrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.friendNumLabel.mas_bottom).offset(4);
        make.left.equalTo(self.friendNumLabel.mas_left).offset(-2);
        make.height.mas_equalTo(24);
        make.right.equalTo(self.contentView.mas_right).offset(-16);
    }];
    self.removeBtn.layer.borderWidth = 1.0f;
    self.removeBtn.layer.borderColor = [SLColor(@"#CFD8E3") CGColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)confignRecomFriendCell:(SLRecomFriendModel *)model {
    self.recomFriendModel = model;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:[model getUserHeadUrl]]];
    self.nameLabel.text = model.userName;
    self.friendNumLabel.text = model.mutualFriends;
    if (model.addSuccess) {
        self.removeBtn.hidden = YES;
        self.addBtn.layer.borderWidth = 1.0f;
        self.addBtn.layer.borderColor = [SLColor(@"#CFD8E3") CGColor];
        self.addBtn.backgroundColor = SLColor(@"#F5F8F7");
        [self.addBtn setTitle:@"  已添加  " forState:UIControlStateNormal];
        [self.addBtn setTitleColor:SLBlackColor forState:UIControlStateNormal];
        self.addBtn.enabled = NO;
    }else {
        self.removeBtn.hidden = NO;
        self.addBtn.layer.borderWidth = 0.0f;
        self.addBtn.layer.borderColor = [SLColor(@"#CFD8E3") CGColor];
        self.addBtn.backgroundColor = SLBlueColor;
        [self.addBtn setTitle:@"   添加   " forState:UIControlStateNormal];
        [self.addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.addBtn.enabled = YES;
    }
    
    [self.interestcrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
        obj = nil;
    }];
    NSArray *interests = [model.interests componentsSeparatedByString:@","];
    __block CGFloat x = 0;
    [interests enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGRect rect = YMBoundWidth(obj, [UIFont systemFontOfSize:12]);
        CGFloat width = rect.size.width;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text = obj;
        label.frame = CGRectMake(x, 0, width + 24, 24);
        label.textColor = SLColor(@"#636366");
        label.font = [UIFont systemFontOfSize:12];
        label.backgroundColor = SLColor(@"#F2F2F7");
        label.layer.cornerRadius = 12.0;
        label.textAlignment = NSTextAlignmentCenter;
        label.clipsToBounds = YES;
        [self.interestcrollView addSubview:label];
        x += width + 28;
    }];
    self.interestcrollView.contentSize = CGSizeMake(x, 24);
}

- (UIScrollView *)interestcrollView {
    if (!_interestcrollView) {
        _interestcrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _interestcrollView.bounces = NO;
        _interestcrollView.showsVerticalScrollIndicator = NO;
        _interestcrollView.showsHorizontalScrollIndicator = NO;
        [self.contentView addSubview:_interestcrollView];
    }
    return _interestcrollView;
}

- (IBAction)addBtnClick:(id)sender {
    [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Contacts/AddContacts" parameter:@{@"userId" : self.recomFriendModel.userId} success:^(id responseObject) {
        [ZFToast ShowWithMessage:@"添加成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.cellBlock(YES);
        });
    } failure:^(SPRequestError *error) {

    }];
    
}
- (IBAction)removeBtnClick:(id)sender {
    if (self.cellBlock) {
        self.cellBlock(NO);
    }
}

@end
