//
//  XVAlertTools.m
//  fastservice
//
//  Created by xuning on 5/5/25.
//  Copyright © 2025 FastService. All rights reserved.
//

#import "XVAlertTools.h"

@implementation XVAlertTools

void showAlertMessage(NSString *message){
    [LEEAlert alert].config
    .LeeBackgroundStyleTranslucent(0)
    .LeeContent(message)
    .LeeAction(@"好", ^{
        
        // 点击事件Block
    })
    .LeeShow();
}

void showAlertTitleAndMessage(NSString *title, NSString *message,NSString *ok) {
    
    [LEEAlert alert].config
    .LeeBackgroundStyleTranslucent(0)
    .LeeTitle(title)
    .LeeContent(message)
    .LeeAction(ok, ^{
        
        // 点击事件Block
    })
    .LeeShow();
    
}

void showFullScreen(BOOL flag){
    theApp().window.userInteractionEnabled = !flag;
    
}
void showFullView(BOOL flag,UIViewController * bvc){
    bvc.view.userInteractionEnabled = !flag;
}
void showIndicator(BOOL flag,NSString * title, EventVoidHandler block){
    
    
//    if (flag) {
//        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:theApp().window animated:YES];
//        hud.tag = 70021;
//        hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
//        hud.contentColor = [UIColor whiteColor];
//        hud.userInteractionEnabled = NO;
//        hud.mode = MBProgressHUDModeIndeterminate;
//        hud.label.text = title;
//        hud.label.font = [UIFont systemFontOfSize:14];
//        hud.label.textColor = [UIColor whiteColor];
//        hud.removeFromSuperViewOnHide = YES;
//    }else{
//        dispatch_async(dispatch_get_main_queue(), ^{
//            MBProgressHUD *hud = (MBProgressHUD *)[theApp().window viewWithTag:70021];
//            [hud hideAnimated:YES];
//        });
//
//        BlockCallWithVoidArg(block);
//    }
    
    


    
    UIView *activityView = (UIView *)[theApp().window viewWithTag:70021];
    if (flag) {
        if (activityView==nil) {
            activityView = [[UIView alloc] initWithFrame:CGRectMake(110, 120, 100, 100)];
            activityView.tag = 70021;
            activityView.backgroundColor = [UIColor clearColor];
            UIView * v = [[UIView alloc]initWithFrame:activityView.bounds];
            v.backgroundColor = [UIColor blackColor];
            v.alpha = .4;
            v.layer.cornerRadius = 5;
            v.layer.masksToBounds = YES;
            [activityView addSubview:v];
            [activityView.layer setContents:(id)[[UIImage imageNamed:@"incativy.png"] CGImage]];

            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3, 65, 95, 32)];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = title;
            label.numberOfLines = 0;
            label.font = [UIFont systemFontOfSize:12];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor clearColor];
            [v addSubview:label];

            UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            activity.center = CGPointMake(activityView.width / 2.0, activityView.height / 2.0 - 5);
            activity.tag = 794232;
            [activity startAnimating];
            [activityView addSubview:activity];
            activityView.center = CGPointMake(theApp().window.center.x, theApp().window.center.y - 100);
            [theApp().window addSubview:activityView];
        }

    }
    else{
        if (activityView && [activityView superview]) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[activityView viewWithTag:794232];

                [UIView animateWithDuration:.5 animations:^{
                    activityView.alpha = 0;
                } completion:^(BOOL finished) {
                    if (activityIndicator) {
                        [activityIndicator stopAnimating];
                    }
                    [activityView removeFromSuperview];
                    BlockCallWithVoidArg(block);
                }];
            });
        }
    }

}


@end
