//
//  SLHomeViewController.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/8.
//

#import "SLHomeViewController.h"
#import "SLHomeView.h"
#import "SLChatListModel.h"

@interface SLHomeViewController ()

@property (nonatomic, strong) SLHomeView *homeView;

@property (nonatomic, strong) NSMutableArray *chatList;
@end

@implementation SLHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (@available(iOS 11.0, *)) {
        self.homeView.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.navigationBar.hidden = YES;
    WS(weakSelf);
    self.homeView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf requestChatList:^{
            weakSelf.homeView.tableView.mj_header.state = MJRefreshStateIdle;
        }];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    [SLUserModel requestUserRoleModel];
    [self requestChatList:^{
        
    }];
}

- (void)requestChatList:(void(^)(void))compBlock {
    NSString *userId = [SLGlobalInfo SharedInstance].loginInfo.user.userId;
    if (userId) {
        [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Chat/GetChatList" parameter:@{} success:^(id responseObject) {
            NSArray *chatList = [responseObject objectForKey:@"data"];
            self.chatList = [SLChatListModel mj_objectArrayWithKeyValuesArray:chatList];
            self.homeView.viewModels = self.chatList;
            [self.homeView.tableView reloadData];
            compBlock();
        } failure:^(SPRequestError *error) {
            compBlock();
        }];
    }
}

- (SLHomeView *)homeView {
    if (!_homeView) {
        _homeView = [[SLHomeView alloc] initWithFrame:CGRectZero];
        [self.view addSubview:_homeView];
    }
    return _homeView;
}

- (NSMutableArray *)chatList {
    if (!_chatList) {
        _chatList = [NSMutableArray array];
    }
    return _chatList;
}
@end
