//
//  MBProgressHUD+Addtions.m
//  RealEstateManagement
//
//  Created by Z on 16/6/24.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "MBProgressHUD+Addtions.h"

//hud 延迟多久消失
static CGFloat const delay = 1.2;

@implementation MBProgressHUD (Addtions)

//创建一个hud 配置hud样式
+ (MBProgressHUD *)j_hudWithView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.contentColor = kMainBlueColor;
    hud.label.font = [UIFont systemFontOfSize:18];
    hud.detailsLabel.font = [UIFont systemFontOfSize:16];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

//成功的hud
+ (void)j_success:(NSString *)success complete:(void (^)(void))complete {
    [self j_customViewImageName:@"Checkmark" text:success complete:complete];
}

//失败的hud
+ (void)j_error:(NSString *)error complete:(void (^)(void))complete {
    if ([error isEqualToString:@"A connection failure occurred"]) {
        error = @"网络连接失败，请重试！";
    }
    [self j_customViewImageName:@"cross" text:error complete:complete];
}

//自定义hud图片
+ (MBProgressHUD *)j_customViewImageName:(NSString *)imageName text:(NSString *)text complete:(void (^)(void))complete{
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    hud.mode = MBProgressHUDModeCustomView;
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.tintColor = kMainBlueColor;
    hud.customView = imageView;
    hud.detailsLabel.text = text;
    [hud showAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self j_hideLoadingView];
        if (complete) {
            complete();
        }
    });
    return hud;
}

/**
 *  只有菊花
 */
+ (void)j_loading {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    [hud showAnimated:YES];
}

+ (MBProgressHUD *)j_loadingOnView:(UIView *)view {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:view];
    [hud showAnimated:YES];
    return hud;
}

/**
 *  菊花+文字
 *
 *  @param text 提示文字
 */
+ (void)j_loading:(NSString *)text {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    hud.label.text = text;
    [hud showAnimated:YES];
}

/**
 *  菊花+文字
 */
+ (MBProgressHUD *)j_loadingOnView:(UIView *)view text:(NSString *)text {
    MBProgressHUD *hud = [self j_hudWithView:view];
    hud.label.text = text;
    return hud;
}

/**
 *  仅文字提示  自动消失
 *
 *  @param text 提示文字
 */
+ (void)j_textOnly:(NSString *)text {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    [hud showAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self j_hideLoadingView];
    });
}


/**
 *  隐藏kwindow上面的所有hud
 */
+ (void)j_hideLoadingView {
    [MBProgressHUD j_hideOnView:kWindow];
}

+ (void)j_hideOnView:(UIView *)view {
    MBProgressHUD *hud;
    for (id obj in view.subviews) {
        if ([obj isKindOfClass:[MBProgressHUD class]]) {
            hud = obj;
            [hud hideAnimated:YES];
            hud = nil;
        }
    }
}

/**
 *  删除所有hud
 */
+ (void)j_removeAllHud {
    for (id view in kWindow.subviews) {
        if ([view isKindOfClass:[MBProgressHUD class]]) {
            MBProgressHUD *hud = view;
            [hud removeFromSuperview];
            hud = nil;
        }
    }
}

@end
