//
//  BlackListController.m
//  AIIM
//
//  Created by qitewei on 2025/5/22.
//

#import "BlackListController.h"

#import "FriendListCell.h"
#import "FriendNetApi.h"

@interface BlackListController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@end

@implementation BlackListController
#pragma mark lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self getBlackcListData];
}

- (void)viewWillAppear:(BOOL)animated{
    [self setNavigationBarHidden:NO animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [self setNavigationBarHidden:YES animated:NO];
}

#pragma mark UI
- (void)configUI{
    [self setNavigationBarHidden:NO animated:YES];
    [self setNavigationTitle:NSLocalizedString(@"userCenter-heimdan", @"")];
    // 设置导航栏背景色
    [self setNavigationBarBackgroundColor:UIColor.clearColor];
    
    // 设置标题颜色和字体
    [self setNavigationTitleColor:[UIColor whiteColor] font:SYSBFONT(18)];
    
    // 设置返回按钮
    [self setBackButtonTitle:@""];
    [self setBackButtonColor:[UIColor whiteColor]];
    
    UIImageView * bgImageView = [[UIImageView alloc] initWithImage:kImageMake(@"loginBG")];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(SCREEN_TOP+10);
        make.left.right.bottom.mas_equalTo(0);
    }];
}

#pragma mark api
- (void)getBlackcListData{
    [FriendNetApi getblackList:@{} succ:^(int code, NSDictionary * _Nullable result) {
        NSLog(@"blackList result:%@",result);
        if (code == 200) {
            [self.dataArray addObjectsFromArray:result[@"data"]];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)removeFromBlacklist:(NSDictionary *)user{
    [FriendNetApi removeFromBlacklistWithId:user[@"id"] succ:^(int code, NSDictionary * _Nullable result) {
        if (code == 200) {
            [self.dataArray removeObject:user];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(FriendListCell.class) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    [cell fillWithData:self.dataArray[indexPath.row]];
    return cell;
}

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    weakSelf(self);
    NSDictionary * user = self.dataArray[indexPath.row];
    // 创建删除操作
    UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
                                                                               title:NSLocalizedString(@"GroupCtr-yichu", @"")
                                                                             handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        // 更新数据
        [weakself removeFromBlacklist:user];
        
        // 告诉系统操作已完成
        completionHandler(YES);
    }];
    
    // 设置删除按钮颜色
    deleteAction.backgroundColor = [UIColor redColor];
    
    // 创建配置并返回
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
    config.performsFirstActionWithFullSwipe = YES; // 完全滑动直接执行删除
    return config;
}

#pragma mark emptyDataset
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Common_nodata", @"")];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return -100.f;
}

#pragma mark lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        [_tableView registerClass:FriendListCell.class forCellReuseIdentifier:NSStringFromClass(FriendListCell.class)];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark statusBar
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
