//
//  BasePullTableViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/7/12.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "BasePullTableViewController.h"

@interface BasePullTableViewController ()

@end

@implementation BasePullTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    [self setupTableViewAdditional];
}

#pragma mark -初始化设置

- (void)setupTableView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.pullPageIndex = ONE;
}

#pragma mark - 添加上、下拉刷新

-(void)setupTableViewAdditional
{
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingTarget:self refreshingAction:@selector(loadTableViewHeader)];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = headerRefresh;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadTableViewFooter)];
    self.tableView.mj_footer.automaticallyHidden = YES;
}

#pragma mark - 设置无数据代理
- (void)setupNotDataDelegate
{
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
}

#pragma mark -刷新回调

-(void)loadTableViewHeader
{
    [XBLoadingView showHUDViewWithDefault];
    [self.tableView.mj_footer resetNoMoreData];
    [self setupNotDataDelegate];
    _pullPageIndex = ONE;
    [self loadWebDataSource];
}

-(void)loadTableViewFooter
{
    [XBLoadingView showHUDViewWithDefault];
    [self setupNotDataDelegate];
    _pullPageIndex ++;
    [self loadWebDataSource];
}

#pragma mark -结束刷新

- (void)endRefresh:(EndRefreshType)type
{
    [XBLoadingView hideHUDViewWithDefault];
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    if (self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
    if (type == EndRefreshNotData) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    if (self.tableView.isEmptyDataSetVisible) {
        [self.tableView reloadEmptyDataSet];
    }
}

#pragma mark -加载数据 (需重写)

-(void)loadWebDataSource
{
    if (self.tableView.isEmptyDataSetVisible) {
        [self.tableView reloadEmptyDataSet];
    }
}

#pragma mark -UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UITableViewCell new];
}

#pragma mark - DZNEmptyDataSetSource Methods

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor grayColor]};
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:@"对不起，居然真的没有数据" attributes:dic];
    return attr;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 64;
}

#pragma mark - DZNEmptyDataSetDelegate Methods

- (BOOL)emptyDataSetShouldShow:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}


@end
