//
//  ChatAtMemberView.m
//  AIIM
//
//  Created by qitewei on 2025/5/29.
//

#import "ChatAtMemberView.h"
#import "ChatAtMemberCell.h"
#import "UDManager.h"

@interface ChatAtMemberView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIButton * leftBtn;
@property (nonatomic, strong) UIButton * rightBtn;
@property (nonatomic, strong) UILabel * titleLbl;
@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, assign) BOOL isBatchMode;
@property (nonatomic, strong) NSMutableArray * selectedMemberArray;
@end

@implementation ChatAtMemberView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.whiteColor;
        self.isBatchMode = NO;
        [self configUI];
        [self configContrains];
    }
    return self;
}

- (void)configUI{
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    [self addSubview:self.titleLbl];
    [self addSubview:self.tableView];
}

- (void)configContrains{
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(16);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(16);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(20);
    }];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(14);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.leftBtn.mas_centerY);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.leftBtn.mas_bottom).offset(20);
        make.left.right.bottom.mas_equalTo(0);
    }];
}

#pragma mark setter
- (void)setMemberArray:(NSArray *)memberArray{
    NSDictionary *userinfo = [UDManager.shareInstance getDDManager:dkuserinfo];
    NSMutableArray * tempArr = [NSMutableArray arrayWithArray:memberArray];
    for (NSDictionary * member in memberArray) {
        if ([member[@"id"] isEqualToString:userinfo[@"id"]]) {
            [tempArr removeObjectAtIndex:[memberArray indexOfObject:member]];
            break;
        }
    }
    _memberArray = tempArr;
    [self.tableView reloadData];
}

#pragma mark event
- (void)leftButtonClicked{
    if (self.isBatchMode) {
        //取消多选
        self.isBatchMode = NO;
        [_rightBtn setTitle:NSLocalizedString(@"Common_multi", @"") forState:UIControlStateNormal];
        [_leftBtn setTitle:NSLocalizedString(@"Common_close", @"") forState:UIControlStateNormal];
        [self.tableView reloadData];
    }else{
        //关闭窗口
        !self.closeAtViewBlock ?:self.closeAtViewBlock();
    }
}

- (void)rightButtonClicked{
    if (self.isBatchMode) {
        //@多人
        if (self.selectedMemberArray.count == 0) {
            [MBProgressHUD showWithText:NSLocalizedString(@"At_alter", @"")];
            return;
        }
        
        !self.atMembersBlock ?: self.atMembersBlock(self.selectedMemberArray);
    }else{
        //@多人模式
        _isBatchMode = YES;
        [_rightBtn setTitle:NSLocalizedString(@"Common_done", @"") forState:UIControlStateNormal];
        [_leftBtn setTitle:NSLocalizedString(@"Common_cancel", @"") forState:UIControlStateNormal];
        [self.tableView reloadData];
    }
}

#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.memberArray) {
        return self.isGroupMaster ?self.memberArray.count+1 :self.memberArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isGroupMaster && indexPath.row == 0) {
        ChatAtMemberCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(ChatAtMemberCell.class) forIndexPath:indexPath];
        cell.avatar.image = kImageMake(@"at_everyone");
        cell.nickNameLbl.text = NSLocalizedString(@"At_everyone", @"");
        return cell;
    }
    
    ChatAtMemberCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(ChatAtMemberCell.class) forIndexPath:indexPath];
    cell.memberDict = self.isGroupMaster ?self.memberArray[indexPath.row-1] :self.memberArray[indexPath.row];
    cell.isBatchMode = self.isBatchMode;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.isGroupMaster && indexPath.row == 0) {
        !self.atEveryoneBlock ?: self.atEveryoneBlock();
        return;
    }
    
    if (!self.isBatchMode) {
        //@单人
        NSDictionary * member;
        if (self.isGroupMaster) {
            member = self.memberArray[indexPath.row-1];
        }else{
            member = self.memberArray[indexPath.row];
        }
        !self.atSingleMemberBlock ?: self.atSingleMemberBlock(member);
    }else{
        ChatAtMemberCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.selectedStateBtn.selected = !cell.selectedStateBtn.isSelected;
        if (cell.selectedStateBtn.isSelected) {
            if (self.isGroupMaster) {
                [self.selectedMemberArray addObject:self.memberArray[indexPath.row-1]];
            }else{
                [self.selectedMemberArray addObject:self.memberArray[indexPath.row]];
            }
        }else{
            if (self.isGroupMaster) {
                [self.selectedMemberArray removeObject:self.memberArray[indexPath.row-1]];
            }else{
                [self.selectedMemberArray removeObject:self.memberArray[indexPath.row]];
            }
        }
    }
}

#pragma mark lazy
- (UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setTitle:NSLocalizedString(@"Common_close", @"") forState:UIControlStateNormal];
        [_leftBtn setTitleColor:globalColor(GCTypeBlack) forState:UIControlStateNormal];
        _leftBtn.titleLabel.font = SYSFONT(16);
        [_leftBtn addTarget:self action:@selector(leftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setTitle:NSLocalizedString(@"Common_multi", @"") forState:UIControlStateNormal];
        [_rightBtn setTitleColor:globalColor(GCTypeBlack) forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = SYSFONT(16);
        [_rightBtn addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

- (UILabel *)titleLbl{
    if (!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.text = NSLocalizedString(@"At_title", @"");
        _titleLbl.font = SYSBFONT(14);
        _titleLbl.textAlignment = NSTextAlignmentCenter;
        _titleLbl.textColor = globalColor(GCTypeBlack);
    }
    return _titleLbl;
}

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

- (NSMutableArray *)selectedMemberArray{
    if (!_selectedMemberArray) {
        _selectedMemberArray = [NSMutableArray array];
    }
    return _selectedMemberArray;
}

@end
