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

#import "TestClient.h"

@implementation TestClient
+ (void)alertTESTViewWithBlock:(EventVoidHandler)block{
//#ifdef DEBUG
//    BlockCallWithTwoArg(block, nil, 0);
//#else
    __block UITextField *tf = nil;
    
    [LEEAlert alert].config
    .LeeTitle(@"TEST")
    .LeeAddTextField(^(UITextField *textField) {
        
        // 这里可以进行自定义的设置
        textField.placeholder = @"请输入密码";
        
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
            
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        
        tf = textField; //赋值
    })
    .LeeAction(@"确认", ^{
        NSString *spw = @"kfw8888";
        if ([tf.text isEqualToString:spw]) {
            BlockCallWithVoidArg(block);
        }else{
            [MBProgressHUD showMessage:@"密码错误请重试"];
        }
        
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(@"取消", nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();

//#endif
    
}
#pragma mark -切环境
+ (void)changeEnvironment:(UIView *)view longPress:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        [TestClient alertTESTViewWithBlock:^{
            [TestClient changeEnvironment:view];
        }];
    }
}
+ (void)changeEnvironment:(UIView *)view{
        NSString *title = @"环境切换,当前正式环境";
        if ([AppConfig getEnvironmentType] == 0) {
            title = @"环境切换,当前正式环境";
        }else if ([AppConfig getEnvironmentType] == 1){
            title = @"环境切换,当前灰度环境";
        }else if ([AppConfig getEnvironmentType] == 2){
            title = @"环境切换,当前api1环境";
        }else if ([AppConfig getEnvironmentType] == 3){
            title = @"环境切换,当前api2环境";
        }
        else if ([AppConfig getEnvironmentType] == 4){
            title = @"环境切换,当前api3环境";
        }else if ([AppConfig getEnvironmentType] == 5){
            title = S(@"自定义:%@",[AppConfig getLocalCache].sserver_address);
        }
    
    [LEEAlert actionsheet].config
        .LeeTitle(@"切换环境")

    .LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeDefault;
        
        action.title = @"正式环境";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];
        [AppConfig setEnvironmentType:0];
        
        [TestClient exitApplication];

    })
    .LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeDefault;
        
        action.title = @"灰度环境";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];

        [AppConfig setEnvironmentType:1];
        
        [TestClient exitApplication];
    })

    .LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeDefault;
        
        action.title = @"api1环境";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];
        [AppConfig setEnvironmentType:2];
        
        [TestClient exitApplication];
    })

    .LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeDefault;
        
        action.title = @"api2环境";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];
        [AppConfig setEnvironmentType:3];
        
        [TestClient exitApplication];
    })

    .LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeDefault;
        
        action.title = @"api3环境";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];
        [AppConfig setEnvironmentType:4];
        
        [TestClient exitApplication];

    })
    .LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeDefault;
        
        action.title = @"自定义环境";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [TestClient showInputServerAddress];
        });
    })


    .LeeAddAction(^(LEEAction *action) {
        
        action.type = LEEActionTypeCancel;
        
        action.title = @"取消";
        
        action.titleColor = [UIColor blackColor];
        
        action.font = [UIFont systemFontOfSize:18.0f];
    })
    .LeeActionSheetCancelActionSpaceColor([UIColor colorWithWhite:0.92 alpha:1.0f]) // 设置取消按钮间隔的颜色
    .LeeActionSheetBottomMargin(0.0f) // 设置底部距离屏幕的边距为0
    .LeeCornerRadii(CornerRadiiMake(10, 10, 0, 0))   // 指定整体圆角半径
    .LeeActionSheetHeaderCornerRadii(CornerRadiiZero()) // 指定头部圆角半径
    .LeeActionSheetCancelActionCornerRadii(CornerRadiiZero()) // 指定取消按钮圆角半径
    .LeeConfigMaxWidth(^CGFloat(LEEScreenOrientationType type, CGSize size) {
        
        // 这是最大宽度为屏幕宽度 (横屏和竖屏)
        
        return size.width;
    })
    .LeeActionSheetBackgroundColor([UIColor whiteColor]) // 通过设置背景颜色来填充底部间隙
    #ifdef __IPHONE_13_0
    .LeeUserInterfaceStyle(UIUserInterfaceStyleLight)
    #endif
    .LeeShow();
    
}
+ (void)showInputServerAddress{
    __block UITextField *tf = nil;
    [LEEAlert alert].config
    .LeeTitle(@"自定义环境")
    .LeeContent(@"请输入自定义地址，保证格式正确，如：api.kfw.net")
    .LeeAddTextField(^(UITextField *textField) {
        
        // 这里可以进行自定义的设置
        textField.placeholder = @"请输入自定义地址";
        
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
            
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        
        tf = textField; //赋值
    })
    .LeeAction(@"确认", ^{
        if (StringIsNullOrEmpty(tf.text)) {
            [MBProgressHUD showMessage:@"输入无效"];
        }else{
            LocalCache *lc = [AppConfig getLocalCache];
            if ( [tf.text hasPrefix:@"http://"]) {
                lc.sserver_address = tf.text;
            }else{
                lc.sserver_address = S(@"http://%@",tf.text);
            }
            [AppConfig saveLocalCache:lc];
            
            [AppConfig setEnvironmentType:5];
            
            [TestClient exitApplication];
        }
        
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(@"取消", nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();

}
+ (void)exitApplication {
    id<UIApplicationDelegate> app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}
//5下单击处罚自定义经纬度
+ (void)changeCustomLocationWithTempVc:(UIViewController *)vc TempView:(UIView *)view fivePress:(UITapGestureRecognizer *)fivePress {
    if (fivePress.state == UIGestureRecognizerStateEnded) {
        LocalCache *lc = [AppConfig getLocalCache];
        CLLocationCoordinate2D coord = [AppConfig getLocationCoordinate];
        NSString *title = @"还未自定义位置，请选择";
        if (lc.is_custom == 1 && coord.latitude == 29.240234 && coord.longitude == 91.778328) {
            title = @"当前位置:(山南市)";//@"当前位置:(拉萨市政府)";
        }else if (lc.is_custom == 1 && coord.latitude == 29.660398 && coord.longitude == 91.125118){
            title = @"当前位置:(布达拉宫)";
        }else if (lc.is_custom == 1 && coord.latitude == 29.65886 && coord.longitude == 91.077957){
            title = @"当前位置:(地矿博物馆)";
        }else if (lc.is_custom == 1 && coord.latitude == 29.643951 && coord.longitude == 91.018759){
            title = @"当前位置:(西藏温州商会)";
        }else {
            if (lc.is_custom == 1){
                title = S(@"当前位置:(经度:%f , 纬度:%f)", coord.longitude, coord.latitude);
            } else {
                title = S(@"还未自定义位置，请选择");
            }
        }
        
        
        [LEEAlert actionsheet].config
            .LeeTitle(title)

        .LeeAddAction(^(LEEAction * _Nonnull action) {
            action.type = LEEActionTypeDefault;
            
            action.title = @"山南市";
            
            action.titleColor = [UIColor blackColor];
            
            action.font = [UIFont systemFontOfSize:18.0f];
            [TestClient setupCustomLocationDataWithTempVc:vc index:0];

        })
        .LeeAddAction(^(LEEAction * _Nonnull action) {
            action.type = LEEActionTypeDefault;
            
            action.title = @"布达拉宫";
            
            action.titleColor = [UIColor blackColor];
            
            action.font = [UIFont systemFontOfSize:18.0f];

            [TestClient setupCustomLocationDataWithTempVc:vc index:1];
        })

        .LeeAddAction(^(LEEAction * _Nonnull action) {
            action.type = LEEActionTypeDefault;
            
            action.title = @"地矿博物馆";
            
            action.titleColor = [UIColor blackColor];
            
            action.font = [UIFont systemFontOfSize:18.0f];
            [TestClient setupCustomLocationDataWithTempVc:vc index:2];
        })

        .LeeAddAction(^(LEEAction * _Nonnull action) {
            action.type = LEEActionTypeDefault;
            
            action.title = @"西藏温州商会";
            
            action.titleColor = [UIColor blackColor];
            
            action.font = [UIFont systemFontOfSize:18.0f];
            [TestClient setupCustomLocationDataWithTempVc:vc index:3];
        })

        .LeeAddAction(^(LEEAction * _Nonnull action) {
            action.type = LEEActionTypeDefault;
            
            action.title = @"自定义位置";
            
            action.titleColor = [UIColor blackColor];
            
            action.font = [UIFont systemFontOfSize:18.0f];
            [TestClient setupCustomLocationDataWithTempVc:vc index:4];
        })


        .LeeAddAction(^(LEEAction *action) {
            
            action.type = LEEActionTypeCancel;
            
            action.title = @"取消";
            
            action.titleColor = [UIColor blackColor];
            
            action.font = [UIFont systemFontOfSize:18.0f];
        })
        .LeeActionSheetCancelActionSpaceColor([UIColor colorWithWhite:0.92 alpha:1.0f]) // 设置取消按钮间隔的颜色
        .LeeActionSheetBottomMargin(0.0f) // 设置底部距离屏幕的边距为0
        .LeeCornerRadii(CornerRadiiMake(10, 10, 0, 0))   // 指定整体圆角半径
        .LeeActionSheetHeaderCornerRadii(CornerRadiiZero()) // 指定头部圆角半径
        .LeeActionSheetCancelActionCornerRadii(CornerRadiiZero()) // 指定取消按钮圆角半径
        .LeeConfigMaxWidth(^CGFloat(LEEScreenOrientationType type, CGSize size) {
            
            // 这是最大宽度为屏幕宽度 (横屏和竖屏)
            
            return size.width;
        })
        .LeeActionSheetBackgroundColor([UIColor whiteColor]) // 通过设置背景颜色来填充底部间隙
        #ifdef __IPHONE_13_0
        .LeeUserInterfaceStyle(UIUserInterfaceStyleLight)
        #endif
        .LeeShow();

        
        

    }
}
//保存设置
+ (void)setupCustomLocationDataWithTempVc:(UIViewController *)vc index:(int)index {
    LocalCache *lc = [AppConfig getLocalCache];
    if (index == 0 || index == 1 || index == 2 || index == 3) {
        NSString *longText = @"";
        NSString *latText = @"";
        if (index == 0) {

            longText = @"91.778328";
            latText = @"29.240234";
        } else if (index == 1) {
            longText = @"91.125118";
            latText = @"29.660398";
        } else if (index == 2) {
            longText = @"91.077957";
            latText = @"29.65886";
        } else if (index == 3) {
            longText = @"91.018759";
            latText = @"29.643951";
        }
        [AppConfig saveCustomLocationCoordinateWithLongitude:longText Latitude:latText];
        lc.is_custom = 1;
        [AppConfig saveLocalCache:lc];
    } else {
        if (index == 4) {
            [TestClient customLocationWith:vc];
        } else if (index == 5) {
            [AppConfig deleteCustomLocationCoordinate];
            lc.is_custom = 0;
            [AppConfig saveLocalCache:lc];
        } else {
            
        }
    }

}

//自定义经纬度
+(void)customLocationWith:(UIViewController *)vc {
    LocalCache *lc = [AppConfig getLocalCache];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"提示" message: @"自定义经纬度" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"经度";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"纬度";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField *longfield = textfields[0];
        UITextField *latfiled = textfields[1];
        if (StringNotNullAndEmpty(longfield.text) && StringNotNullAndEmpty(latfiled.text)) {
            [AppConfig saveCustomLocationCoordinateWithLongitude:longfield.text Latitude:latfiled.text];
            lc.is_custom = 1;
            [AppConfig saveLocalCache:lc];
        }
    }]];
    [vc presentViewController:alertController animated:YES completion:nil];
}

+ (void)dicMobClickStatistics:(NSString *)event{
    [TestClient dicMobClickStatistics:event url:nil];
}
+ (void)dicMobClickStatistics:(NSString *)event url:(NSString *)url{
    NSMutableDictionary *pDic = [NSMutableDictionary dictionary];
    [pDic setAnyObject:S(@"%d",[AppConfig getUserInfo].user_id) forKey:@"u_id"];
    [pDic setAnyObject:[AppConfig getUserInfo].mobile forKey:@"mobile"];
    [pDic setAnyObject:url forKey:@"url"];
    [pDic setAnyObject:S(@"%f",[[NSDate date] timeIntervalSince1970]) forKey:@"timep"];
//    [MobClick event:event attributes:pDic];
}

@end
