//
//  UIScrollView+KBEmptyView.m
//  keyBoard
//

#import "UIScrollView+KBEmptyView.h"

#if __has_include(<LYEmptyView/UIView+Empty.h>)
#import <LYEmptyView/UIView+Empty.h>
#import <LYEmptyView/LYEmptyView.h>
#define KB_HAS_LY 1
#else
#define KB_HAS_LY 0
#endif

@implementation UIScrollView (KBEmptyView)

- (void)kb_makeDefaultEmptyViewWithImage:(UIImage *)image
                                   title:(NSString *)title
                                  detail:(NSString *)detail
                             buttonTitle:(NSString *)buttonTitle
                              tapHandler:(KBEmptyAction)tapHandler
                           buttonHandler:(KBEmptyAction)buttonHandler {
#if KB_HAS_LY
    NSString *t = title ?: @"暂无数据";
    LYEmptyView *ev = nil;

    if (buttonTitle.length > 0) {
        __weak typeof(self) weakSelf = self;
        ev = [LYEmptyView emptyActionViewWithImage:image
                                          titleStr:t
                                         detailStr:detail ?: @""
                                       btnTitleStr:buttonTitle
                                     btnClickBlock:^{ if (buttonHandler) buttonHandler(); [weakSelf setNeedsLayout]; }];
    } else {
        ev = [LYEmptyView emptyViewWithImage:image titleStr:t detailStr:detail ?: @""];
    }

    if (tapHandler) { ev.tapEmptyViewBlock = ^{ tapHandler(); }; }

    // 统一样式
    ev.emptyViewIsCompleteCoverSuperView = NO;
    ev.ignoreContentInset = YES;
    ev.contentViewOffset = -10;
    ev.subViewMargin = 14;
    ev.titleLabFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
    ev.titleLabTextColor = [UIColor colorWithWhite:0.45 alpha:1.0];
    ev.detailLabFont = [UIFont systemFontOfSize:14];
    ev.detailLabTextColor = [UIColor colorWithWhite:0.6 alpha:1.0];
    ev.detailLabLineSpacing = 3;
    ev.detailLabMaxLines = 2;
    ev.actionBtnFont = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
    ev.actionBtnCornerRadius = 6;
    ev.actionBtnBorderWidth = 1;
    ev.actionBtnBorderColor = [UIColor colorWithWhite:0.88 alpha:1.0];
    ev.actionBtnTitleColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
    ev.actionBtnBackGroundColor = [UIColor whiteColor];
    ev.actionBtnHorizontalMargin = 20;

    // 默认关闭自动显隐，交给调用方用 begin/end 控制
    ev.autoShowEmptyView = NO;

    self.ly_emptyView = ev;
#else
    (void)image; (void)title; (void)detail; (void)buttonTitle; (void)tapHandler; (void)buttonHandler;
#endif
}

- (void)kb_setLYAutoShowEnabled:(BOOL)enabled {
#if KB_HAS_LY
    self.ly_emptyView.autoShowEmptyView = enabled;
#else
    (void)enabled;
#endif
}

- (void)kb_beginLoadingForEmpty {
#if KB_HAS_LY
    [self ly_startLoading];
#endif
}

- (void)kb_endLoadingForEmpty {
#if KB_HAS_LY
    [self ly_endLoading];
#endif
}

@end

