//
//  ChatRecordController.m
//  AIIM
//
//  Created by qitewei on 2025/5/20.
//

#import "ChatRecordController.h"
#import "chatCellView.h"

#import "UserNetApi.h"

@interface ChatRecordController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView * backgroudImageView;
@property (nonatomic, strong) UITableView * tableView;

@end

@implementation ChatRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self setupRecordData];
    [self configUI];
}

- (void)configUI{
    [self setNavigationTitle:@"聊天记录"];
    [self setNavigationBarTransparent:YES];
    [self setNavigationBarBackgroundColor:UIColor.clearColor];
    [self setNavigationTitleColor:UIColor.whiteColor font:SYSBFONT(18)];
    [self addBarButtonWithImage:kImageMake(@"fanhui") position:BarButtonItemPositionLeft action:@selector(backToChat)];
    self.view.backgroundColor = UIColor.blackColor;
    
    [self.view addSubview:self.backgroudImageView];
    [self.backgroudImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(SCREEN_TOP);
        make.left.right.bottom.mas_equalTo(0);
    }];
}

#pragma mark data
- (void)setupRecordData{
    for (ChatMessageModel * model in self.dataArray) {
        NSLog(@"fromId:%@",model.fromId);
        [UserNetApi getUserinfo_id:model.fromId succ:^(int code, NSDictionary * _Nullable result) {
            NSDictionary * dataDict = result[@"data"] ?: @{};
            
            for (ChatMessageModel * item in self.dataArray) {
                if ([dataDict[@"id"] isEqualToString:item.fromId]) {
                    item.avatar = dataDict[@"avatar"];
                    item.nickName = dataDict[@"name"];
                }
            }
            
//            if (self.dataArray.lastObject == model) {
//                
//            }
            [self.tableView reloadData];
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
}

#pragma mark private
- (void)backToChat{
    [self dismissViewControllerAnimated:YES completion:nil];
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChatMessageModel *message = self.dataArray[indexPath.row];
    return [chatCellView cellHeightForMessage:message];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    chatCellView * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(chatCellView.class) forIndexPath:indexPath];
    cell.parentViewController = self;
    
    ChatMessageModel *message = self.dataArray[indexPath.row];
    cell.messageModel = message;
    
    return cell;
}

#pragma mark lazy
- (UIImageView *)backgroudImageView{
    if (!_backgroudImageView) {
        _backgroudImageView = [[UIImageView alloc] initWithImage:kImageMake(@"loginBG")];
    }
    return _backgroudImageView;
}

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

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

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

@end
