//
//  BaseTableView.m
//  keyBoard
//

#import "BaseTableView.h"

// 可选引入：若未集成 DZNEmptyDataSet，此处不会编译进来
#if __has_include(<DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>)
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#define KB_HAS_DZN 1
#else
#define KB_HAS_DZN 0
#endif

@interface BaseTableView ()
#if KB_HAS_DZN
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
#endif
@end

@implementation BaseTableView

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



- (void)commonInit {
    // iOS 15+ 头部默认留白，统一归零（老系统无此属性，不会影响）
    self.sectionHeaderTopPadding = 0;
    self.backgroundColor = [UIColor whiteColor];
    self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

    // 空数据占位默认配置（保持“活”的：可开可关）
    _useEmptyDataSet = YES;                 // 默认开启
    _emptyShouldAllowScroll = YES;          // 默认允许滚动
    _emptyVerticalOffset = 0;               // 默认不偏移
    _emptyTitleText = @"暂无数据";          // 默认标题

#if KB_HAS_DZN
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
#endif
}

#pragma mark - Public

- (void)kb_reloadEmptyDataSet {
#if KB_HAS_DZN
    [self reloadEmptyDataSet];
#endif
}

- (void)setUseEmptyDataSet:(BOOL)useEmptyDataSet {
    _useEmptyDataSet = useEmptyDataSet;
#if KB_HAS_DZN
    // 切换时，动态挂/卸源委托
    self.emptyDataSetSource = useEmptyDataSet ? (id<DZNEmptyDataSetSource>)self : nil;
    self.emptyDataSetDelegate = useEmptyDataSet ? (id<DZNEmptyDataSetDelegate>)self : nil;
    [self reloadEmptyDataSet];
#endif
}

#pragma mark - DZNEmptyDataSet (仅当库存在时编译)
#if KB_HAS_DZN

// 是否展示空视图
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return self.useEmptyDataSet;
}

// 标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = self.emptyTitleText ?: @"";
    NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold],
                             NSForegroundColorAttributeName : [UIColor colorWithWhite:0.5 alpha:1.0] };
    return [[NSAttributedString alloc] initWithString:title attributes:attrs];
}

// 描述
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    if (self.emptyDescriptionText.length == 0) return nil;
    NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:14],
                             NSForegroundColorAttributeName : [UIColor colorWithWhite:0.6 alpha:1.0] };
    return [[NSAttributedString alloc] initWithString:self.emptyDescriptionText attributes:attrs];
}

// 图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return self.emptyImage;
}

// 按钮标题
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    if (self.emptyButtonTitle.length == 0) return nil;
    UIColor *color = (state == UIControlStateHighlighted) ? [UIColor darkTextColor] : [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
    NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold],
                             NSForegroundColorAttributeName : color };
    return [[NSAttributedString alloc] initWithString:self.emptyButtonTitle attributes:attrs];
}

// 垂直位置偏移
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return self.emptyVerticalOffset;
}

// 行为
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView { return self.emptyShouldAllowScroll; }
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView { return YES; }

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (self.emptyDidTapView) self.emptyDidTapView();
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if (self.emptyDidTapButton) self.emptyDidTapButton();
}

#endif

@end
