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

#import "SLRecomFriendsVc.h"
#import "LeftImageDesignTxtxField.h"
#import "SLRecomFriendsCell.h"
#import "SLRecomFriendModel.h"

@interface SLRecomFriendsVc ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConst;

@property (weak, nonatomic) IBOutlet LeftImageDesignTxtxField *textField;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *friendsModel;

@property (nonatomic, strong) NSMutableArray *searchFriendsModel;

@end

@implementation SLRecomFriendsVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.tableView registerNibCellWithReuseIdentifierClass:SLRecomFriendsCell.class];
    self.topConst.constant = kNavBarHeight;
    self.navigationBar.separatorHeight = 0.0f;
    self.title = @"推荐好友";
    [self requestData];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self.searchFriendsModel removeAllObjects];
    if (textField.text.length) {
        [self.friendsModel enumerateObjectsUsingBlock:^(SLRecomFriendModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.userNamePY rangeOfString:self.textField.text].length > 0) {
                [self.searchFriendsModel addObject:obj];
            }
        }];
    }else {
        [self.searchFriendsModel addObjectsFromArray:self.friendsModel];
    }
    [self.tableView reloadData];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    return YES;
}

- (void)requestData {
    WS(weakSelf);
    [self.tableView tableviewRefresh:self.friendsModel TableViewRefresh:^(NSInteger pageNo, TableListCount listCount) {
        NSDictionary *parameter = @{@"PageIndex": @(pageNo),
                                    @"PageSize": @(SLPageSize),
        };
        [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Contacts/FriendRecommendation" parameter:parameter success:^(id responseObject) {
            NSArray *dataArr = [responseObject objectForKey:@"data"];
            if (dataArr.count) {
                NSArray *friendModels = [SLRecomFriendModel mj_objectArrayWithKeyValuesArray:dataArr];
                [self.friendsModel addObjectsFromArray:friendModels];
                [self textFieldDidEndEditing:self.textField];
                listCount([dataArr count]);
                if (pageNo == 1) {
                    [weakSelf.tableView.mj_header removeFromSuperview];
                }
            }else {
                listCount(-1);
            }
        } failure:^(SPRequestError *error) {
            listCount(-1);
        }];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.searchFriendsModel.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLRecomFriendsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(SLRecomFriendsCell.class)];
    __block SLRecomFriendModel *model = [self.searchFriendsModel objectAtIndex:indexPath.row];
    [cell confignRecomFriendCell:model];
    WS(weakSelf);
    cell.cellBlock = ^(BOOL isAdd) {
        [weakSelf.friendsModel enumerateObjectsUsingBlock:^(SLRecomFriendModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.userId isEqualToString:model.userId]) {
                if (isAdd) {
                    obj.addSuccess = YES;
                }else {
                    [weakSelf.friendsModel removeObject:obj];
                }
                *stop = YES;
            }
        }];
        if (isAdd) {
            model.addSuccess = YES;
        }else {
            [self.searchFriendsModel removeObject:model];
        }
        [weakSelf.tableView reloadData];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}

- (NSMutableArray *)friendsModel {
    if (!_friendsModel) {
        _friendsModel = [NSMutableArray array];
    }
    return _friendsModel;
}

- (NSMutableArray *)searchFriendsModel {
    if (!_searchFriendsModel) {
        _searchFriendsModel = [NSMutableArray array];
    }
    return _searchFriendsModel;
}

@end
