//
//  AMapNavCommonUtil.m
//  Pods
//
//  Created by admin on 2026/2/11.
//

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

/// 共享 HUD 实例
static MBProgressHUD *_sharedHUD = nil;

@implementation AMapNavCommonUtil

#pragma mark - MBProgressHUD

+ (UIWindow *)_keyWindow {
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene *scene in [UIApplication sharedApplication].connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive) {
                for (UIWindow *window in scene.windows) {
                    if (window.isKeyWindow) return window;
                }
            }
        }
    }
    return [UIApplication sharedApplication].keyWindow;
}

+ (void)showLoadingWithMsg:(NSString *)msg {
    dispatch_async(dispatch_get_main_queue(), ^{
//        [self dismiss];

        UIWindow *window = [self _keyWindow];
        if (!window) return;

        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
        hud.contentColor = [UIColor whiteColor];

        if (msg.length > 0) {
            hud.label.text = msg;
            hud.label.font = [UIFont systemFontOfSize:14];
        }

        hud.removeFromSuperViewOnHide = YES;
        _sharedHUD = hud;
    });
}

+ (void)dismiss {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (_sharedHUD) {
            [_sharedHUD hideAnimated:YES];
            _sharedHUD = nil;
        } else {
            //兜底：隐藏所有 HUD（防止有遗漏）
            UIWindow *window = [self _keyWindow];
            if (window) {
                [MBProgressHUD hideHUDForView:window animated:YES];
            }
        }
    });
}

+ (void)showMsg:(NSString *)msg {
    if (msg.length == 0) return;

    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [self _keyWindow];
        if (!window) return;

        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
        hud.contentColor = [UIColor whiteColor];
        hud.label.text = msg;
        hud.label.font = [UIFont systemFontOfSize:14];
        hud.removeFromSuperViewOnHide = YES;

        // 2.0s 后自动消失
        [hud hideAnimated:YES afterDelay:2.0];
    });
}

#pragma mark - 字符串判断


BOOL stringIsEmpty (NSString *str)
{
    if (str == nil || str == NULL)
    {
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    if ([str isKindOfClass:[NSString class]])
    {
        NSString * newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSSet *emptySet = [NSSet setWithObjects:@"", @"null", @"(null)", @"<null>", @"NULL", @"无",@"kZero", nil];
        if ([emptySet containsObject:str] || [emptySet containsObject:newStr]) {
            return YES;
        } else {
            return [newStr length] == 0;
        }
    }
    return NO;
}

BOOL stringIsNotEmpty (NSString *str)
{
    return ! stringIsEmpty(str);
}

/// 字符串转数值
+(NSString *)stringValueFromStr:(NSString *)str {
    if (stringIsEmpty(str)) {
        return @"";
    }
    return str;
}



#pragma mark - 获取图片
+(UIImage *)imageWithName:(NSString *)name {
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"AMapNavIOSSDK" withExtension:@"bundle"];
    NSBundle *containnerBundle = [NSBundle bundleWithURL:url];
    
    NSString * path = [containnerBundle pathForResource:[NSString stringWithFormat:@"%@@2x.png" , name]  ofType:nil];
    
    UIImage * arrowImage = [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return arrowImage;
}

+(UIImage *)imageWithName3x:(NSString *)name {
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"AMapNavIOSSDK" withExtension:@"bundle"];
    NSBundle *containnerBundle = [NSBundle bundleWithURL:url];
    
    NSString * path = [containnerBundle pathForResource:[NSString stringWithFormat:@"%@@3x.png" , name]  ofType:nil];
    
    UIImage * arrowImage = [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return arrowImage;
}

@end
