//
//  UITableView+Refresh.m
//  SharedParking
//
//  Created by gui_huan on 2018/5/22.
//  Copyright © 2018年 tangshuanghui. All rights reserved.
//

#import "UITableView+Refresh.h"
#import "UITableView+CYLTableViewPlaceHolder.h"
#import "CYLTableViewPlaceHolderDelegate.h"
#import "TTTableViewPlaceView.h"
#import <objc/runtime.h>
@interface UITableView (MJRefresh)<CYLTableViewPlaceHolderDelegate>
@property (nonatomic, strong) TTTableViewPlaceView *tablePlaceView;

@end
@implementation UITableView (Refresh)
static char *count = "listCount";

- (void)tableviewRefresh:(NSMutableArray *)dataArray TableViewRefresh:(TableViewRefresh)tableViewRefresh{
    WEAKSELF;
    __block NSInteger pageNo = 1;
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [dataArray removeAllObjects];
        pageNo = 1;
        tableViewRefresh(pageNo, wkSelf.listCount);
    }];
    
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        pageNo++;
        tableViewRefresh(pageNo, wkSelf.listCount);
    }];
    [self.mj_header beginRefreshing];
    self.listCount = ^(NSInteger listCount) {
        [wkSelf tableviewRefreshpostListCount:listCount];
    };
    if (!ZYiPhoneX) {
        self.mj_header.ignoredScrollViewContentInsetTop = 0;
    }
//    self.mj_header.automaticallyChangeAlpha = YES;
//    
//    if (@available(iOS 11.0, *)) {
//            self.mj_header.ignoredScrollViewContentInsetTop = 44;
//            self.contentInset = UIEdgeInsetsMake(ZYiPhoneX ? -54 : 0, 0, 100, 0);
//        }else{
//            self.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
//        }

}
- (void)tableviewRefreshpostListCount:(NSInteger)listCount{
    [self endRefreshing];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.mj_footer.state = listCount != SLPageSize ? MJRefreshStateNoMoreData : MJRefreshStateIdle;
        if (!self.tablePlaceView && listCount <= 0) {
            self.tablePlaceView = [[TTTableViewPlaceView alloc]initWithFrame:self.frame];
            WEAKSELF;
            self.tablePlaceView.placeImageViewTap = ^{
                [wkSelf.mj_header beginRefreshing];
            };
        }
        self.tablePlaceView.placeImageView.image = listCount < 0 ? ImageName(@"bd_wangluoyichang") : ImageName(@"icon_blank");
        self.tablePlaceView.placeImageView.userInteractionEnabled = listCount < 0 ? YES : NO;
        self.tablePlaceView.titleLabel.hidden = listCount < 0 ? YES : NO;
        [self cyl_reloadData];
    });
}
- (void)endRefreshing{
    if([self.mj_header isRefreshing]) {
        [self.mj_header endRefreshing];
    }
}

- (TableListCount)listCount{
    return objc_getAssociatedObject(self, count);
}
- (void)setListCount:(TableListCount)listCount{
    objc_setAssociatedObject(self, count, listCount, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#pragma mark - CYLTableViewPlaceHolderDelegate
- (UIView *)makePlaceHolderView
{
    return self.tablePlaceView;
}

- (void)placeImageTap{
    
}
- (TTTableViewPlaceView *)tablePlaceView{
    return objc_getAssociatedObject(self, @selector(tablePlaceView));
}
- (void)setTablePlaceView:(TTTableViewPlaceView *)tablePlaceView{
    objc_setAssociatedObject(self, @selector(tablePlaceView), tablePlaceView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (void)resetInset {
  
    // sectionheader停留解决
//    CGFloat insetT = - self.mj_offsetY > _scrollViewOriginalInset.top ? - self.mj_offsetY : _scrollViewOriginalInset.top;
//    insetT = insetT > self.mj_h + _scrollViewOriginalInset.top ? self.mj_h + _scrollViewOriginalInset.top : insetT;
////    self.insetTDelta = _scrollViewOriginalInset.top - insetT;
//    // 避免 CollectionView 在使用根据 Autolayout 和 内容自动伸缩 Cell, 刷新时导致的 Layout 异常渲染问题
//    if (fabs(self.mj_insetT - insetT) > FLT_EPSILON) {
//        self.mj_insetT = insetT;
//    }
}

- (void)dealloc{
    CheckRunWhere;
}
@end
