//
//  GMHudModule.m
//  Pods
//
//  Created by wangyang on 2016/10/21.
//
//

#import "GMHudModule.h"
#import <MBProgressHUD/MBProgressHUD.h>

@implementation GMHudModule
+ (NSTimeInterval)timeForHUDString:(NSString *)string {
    NSInteger count = string.length;
    if (count < 8) {
        return 1.0;
    }else if (count >= 8 && count < 15) {
        return 2;
    } else if (count >= 15 && count < 25) {
        return 3;
    } else {
        return 4;
    }
}

+ (void)toast:(NSString *)text{
    
    [self toastInView:UIApplication.sharedApplication.keyWindow text:text];
}

+ (void)showComplete:(NSString *)text
{
    [self hideLoading];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:UIApplication.sharedApplication.keyWindow animated:YES];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ToastFinish"]];
    hud.mode = MBProgressHUDModeCustomView;
    
    if (text!=nil && text.length>20) {
        hud.detailsLabelText = text;
    }else{
        hud.labelText = text;
    }
    NSTimeInterval timeInterval = [self timeForHUDString:text];
    [hud hide:YES afterDelay:timeInterval];
}

+ (void)showWarning:(NSString *)text
{
    [self hideLoading];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:UIApplication.sharedApplication.keyWindow animated:YES];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ToastWarn"]];
    // Set custom view mode
    hud.mode = MBProgressHUDModeCustomView;
    if (text!=nil && text.length>20) {
        hud.detailsLabelText = text;
    }else{
        hud.labelText = text;
    }
    NSTimeInterval timeInterval = [self timeForHUDString:text];
    [hud hide:YES afterDelay:timeInterval];
}


+ (void)showLoading:(NSString *)text
{
    [self hideLoading];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:UIApplication.sharedApplication.keyWindow animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = text;
}

+ (void)toastInView:(UIView *)view text:(NSString *)text {
    
    [self hideLoading];
    if (text.length == 0) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.userInteractionEnabled = YES;
    if (text!=nil && text.length>18) {
        hud.detailsLabelText = text;
    }else{
        hud.labelText = text;
    }
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1.0];
}

+ (void)hideLoading {
    [MBProgressHUD hideHUDForView:UIApplication.sharedApplication.keyWindow animated:YES];
}

@end
