//
//  AppDelegate.m
//  keyBoard
//
//  Created by 张伟 on 2025/10/27.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "KBPermissionViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <Bugly/Bugly.h>
#import "BaseTabBarController.h"

static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoard.CustomKeyboard";

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self setupRootVC];
    /// 获取网络权限
    [self getNetJudge];
    /// Bugly
    BuglyConfig *buglyConfig = [BuglyConfig new];
    /// 设置GroupID进行配置
//    buglyConfig.applicationGroupIdentifier = @"";
    [Bugly startWithAppId:BuglyId config:buglyConfig];
    /// 判断获取键盘权限
    dispatch_async(dispatch_get_main_queue(), ^{
        [self kb_presentPermissionIfNeeded];
    });
    return YES;
}


- (void)setupRootVC{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    BaseTabBarController *vc = [[BaseTabBarController alloc] init];
    self.window.rootViewController = vc;
}

#pragma mark - Permission presentation

- (UIViewController *)kb_topMostViewController
{
    UIViewController *root = self.window.rootViewController;
    if (!root) return nil;
    UIViewController *top = root;
    while (top.presentedViewController) {
        top = top.presentedViewController;
    }
    return top;
}

- (void)kb_presentPermissionIfNeeded
{
    BOOL enabled = KBIsKeyboardEnabled();
    UIViewController *top = [self kb_topMostViewController];
    if (!top) return;
    if ([top isKindOfClass:[KBPermissionViewController class]]) {
        if (enabled) {
            [top dismissViewControllerAnimated:YES completion:nil];
        }
        return;
    }
    if (!enabled) {
        KBPermissionViewController *guide = [KBPermissionViewController new];
        guide.modalPresentationStyle = UIModalPresentationFullScreen;
        [top presentViewController:guide animated:YES completion:nil];
    }
}



-(void)getNetJudge {
    AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
    [netManager startMonitoring];
    [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
        if (status == AFNetworkReachabilityStatusReachableViaWiFi){
//            [PublicObj saveNetReachability:@"wifi"];
        }else if (status == AFNetworkReachabilityStatusReachableViaWWAN){
//            [PublicObj saveNetReachability:@"wwan"];
        }else{
//            [PublicObj saveNetReachability:@"unknown"];
        }
    }];
}


static BOOL KBIsKeyboardEnabled(void) {
    for (UITextInputMode *mode in [UITextInputMode activeInputModes]) {
        NSString *identifier = nil;
        @try {
            identifier = [mode valueForKey:@"identifier"]; // not a public API
        } @catch (__unused NSException *e) {
            identifier = nil;
        }
        if ([identifier isKindOfClass:[NSString class]] &&
            [identifier rangeOfString:kKBKeyboardExtensionBundleId].location != NSNotFound) {
            return YES;
        }
    }
    return NO;
}


@end
