//
//  KBPermissionViewController.m
//  CustomKeyboard
//
//  Created by Mac on 2025/10/27.
//

#import "KBPermissionViewController.h"
#import <TargetConditionals.h>

@interface KBPermissionViewController ()

@end

@implementation KBPermissionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];

    UIButton *close = [UIButton buttonWithType:UIButtonTypeSystem];
    [close setTitle:@"X" forState:UIControlStateNormal];
    close.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
    close.tintColor = [UIColor darkTextColor];
    close.frame = CGRectMake(self.view.bounds.size.width - 44, 44, 28, 28);
    close.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
    [close addTarget:self action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:close];

    UILabel *title = [[UILabel alloc] init];
    title.text = @"启用输入法";
    title.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
    title.textColor = [UIColor blackColor];
    title.textAlignment = NSTextAlignmentCenter;
    title.frame = CGRectMake(24, 100, self.view.bounds.size.width - 48, 28);
    title.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:title];

    UILabel *tips = [[UILabel alloc] init];
    tips.text = @"1 开启键盘 > 2 允许完全访问";
    tips.font = [UIFont systemFontOfSize:14];
    tips.textColor = [UIColor darkGrayColor];
    tips.textAlignment = NSTextAlignmentCenter;
    tips.frame = CGRectMake(24, CGRectGetMaxY(title.frame) + 8, self.view.bounds.size.width - 48, 20);
    tips.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:tips];

    UIView *card = [[UIView alloc] initWithFrame:CGRectMake(32, CGRectGetMaxY(tips.frame) + 28, self.view.bounds.size.width - 64, 260)];
    card.backgroundColor = [UIColor whiteColor];
    card.layer.cornerRadius = 16;
    card.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor;
    card.layer.shadowOpacity = 1;
    card.layer.shadowRadius = 12;
    [self.view addSubview:card];

    UIButton *open = [UIButton buttonWithType:UIButtonTypeSystem];
    [open setTitle:@"去设置中开启" forState:UIControlStateNormal];
    open.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];
    [open setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    open.backgroundColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
    open.layer.cornerRadius = 8;
    CGFloat btnW = self.view.bounds.size.width - 64;
    open.frame = CGRectMake(32, CGRectGetMaxY(card.frame) + 32, btnW, 48);
    open.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [open addTarget:self action:@selector(openSettings) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:open];

    UILabel *help = [[UILabel alloc] init];
    help.text = @"没有找到键盘? 请前往 设置 > 通用 > 键盘 > 键盘 > 添加新键盘";
    help.font = [UIFont systemFontOfSize:12];
    help.textColor = [UIColor grayColor];
    help.textAlignment = NSTextAlignmentCenter;
    help.numberOfLines = 2;
    help.frame = CGRectMake(24, CGRectGetMaxY(open.frame) + 12, self.view.bounds.size.width - 48, 36);
    help.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self.view addSubview:help];
}

- (void)dismissSelf {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)openSettings {
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        if (@available(iOS 10.0, *)) {
            [app openURL:url options:@{} completionHandler:nil];
        } else {
            [app openURL:url];
        }
    }
}

@end
