//
//  SLHomeView.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/10.
//

#import "SLHomeView.h"
#import "SLMessageViewController.h"
#import "SLHomeMsgTableViewCell.h"

@interface SLHomeView()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation SLHomeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self confignMyView];
    }
    return self;
}

- (void)confignMyView
{
    [self.tableView registerNibCellWithReuseIdentifierClass:SLHomeMsgTableViewCell.class];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.equalTo(@(kNavBarHeight));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self.navView.mas_bottom).offset(1);
    }];
    NSArray *navs = @[[SLPageModelDetail initWithTitle:@"新增好友" bgImage:@"icon_home_add"],
                      [SLPageModelDetail initWithTitle:@"搜索" bgImage:@"icon_home_search"]];
    [self.navView confignNavViewWith:navs];
    WS(weakSelf);
    self.navView.rightBtnBlock = ^(YMNavBarRightBtnIdx idx) {
        
    };
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

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

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SLHomeMsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLHomeMsgTableViewCell"];
    SLChatListModel *chatModel = [self.viewModels objectAtIndex:indexPath.row];
    [cell confignCellWithModel:chatModel];
    return cell;
}

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

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (UITableViewCellEditingStyleDelete == editingStyle)
    {
        SLChatListModel *chatModel = [self.viewModels objectAtIndex:indexPath.row];
        [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Chat/DelChatRecord" parameter:@{@"friendId" : chatModel.friendId} success:^(id responseObject) {
            [self delChatDataWith:chatModel];
            [self.viewModels removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        } failure:^(SPRequestError *error) {
            
        }];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SLChatListModel *chatModel = [self.viewModels objectAtIndex:indexPath.row];
    SLMessageViewController *vc = [SLMessageViewController loadViewControllewWithNib];
    SLFriendsModel *model = [[SLFriendsModel alloc] init];
    model.userId = chatModel.friendId;
    model.userName = chatModel.dataName;
    model.userHead = chatModel.dataHead;
    vc.friendModel = model;
    [self.viewController navPushViewController:vc animated:YES];
}

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

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

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

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

- (void)delChatDataWith:(SLChatListModel *)chatModel {
    NSString *key = [NSString stringWithFormat:@"%@--to--%@",[SLGlobalInfo SharedInstance].loginInfo.user.userId, chatModel.friendId];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (SLCustNavView *)navView {
    if (!_navView) {
        _navView = [[SLCustNavView alloc] initWithFrame:CGRectZero];
        _navView.infoLabel.text = @"聊天";
        [self addSubview:_navView];
    }
    return _navView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray<SLChatListModel *> *)viewModels {
    if (!_viewModels) {
        _viewModels = [NSMutableArray array];
    }
    return _viewModels;
}

@end
