//
//  KBKeyBoardMainView.m
//  CustomKeyboard
//
//  Created by Mac on 2025/10/28.
//

#import "KBKeyBoardMainView.h"
#import "KBToolBar.h"
#import "KBKeyboardView.h"
#import "KBFunctionView.h"
#import "KBKey.h"
#import "Masonry.h"
#import "KBSkinManager.h"

@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate>
@property (nonatomic, strong) KBToolBar *topBar;
@property (nonatomic, strong) KBKeyboardView *keyboardView;
// 注意：功能面板的展示/隐藏由外部控制器决定，此处不再直接管理显隐
@end
@implementation KBKeyBoardMainView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [KBSkinManager shared].current.keyboardBackground;
        // 顶部栏
        self.topBar = [[KBToolBar alloc] init];
        self.topBar.delegate = self;
        [self addSubview:self.topBar];
        [self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.mas_top).offset(6);
            make.height.mas_equalTo(40);
        }];
        
        // 键盘区域
        self.keyboardView = [[KBKeyboardView alloc] init];
        self.keyboardView.delegate = self;
        [self addSubview:self.keyboardView];
        [self.keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.topBar.mas_bottom).offset(4);
            make.bottom.equalTo(self.mas_bottom).offset(-4);
        }];
        
        // 功能面板切换交由外部控制器处理；此处不直接创建/管理
        
    }
    return self;
}


#pragma mark - KBToolBarDelegate

- (void)toolBar:(KBToolBar *)toolBar didTapActionAtIndex:(NSInteger)index {
    // 将事件抛给外部控制器，由其决定是否切换到功能面板
    if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapToolActionAtIndex:)]) {
        [self.delegate keyBoardMainView:self didTapToolActionAtIndex:index];
    }
}

- (void)toolBarDidTapSettings:(KBToolBar *)toolBar {
    if ([self.delegate respondsToSelector:@selector(keyBoardMainViewDidTapSettings:)]) {
        [self.delegate keyBoardMainViewDidTapSettings:self];
    }
}

#pragma mark - KBKeyboardViewDelegate

- (void)keyboardView:(KBKeyboardView *)keyboard didTapKey:(KBKey *)key {
    switch (key.type) {
        case KBKeyTypeCharacter:
            // 文本插入交由上层控制器处理
            if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
                [self.delegate keyBoardMainView:self didTapKey:key];
            }
            break;
        case KBKeyTypeBackspace:
            if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
                [self.delegate keyBoardMainView:self didTapKey:key];
            }
            break;
        case KBKeyTypeSpace:
            if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
                [self.delegate keyBoardMainView:self didTapKey:key];
            }
            break;
        case KBKeyTypeReturn:
            if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
                [self.delegate keyBoardMainView:self didTapKey:key];
            }
            break;
        case KBKeyTypeModeChange: {
            // 切换 字母 <-> 数字 布局
            keyboard.layoutStyle = (keyboard.layoutStyle == KBKeyboardLayoutStyleLetters) ? KBKeyboardLayoutStyleNumbers : KBKeyboardLayoutStyleLetters;
            [keyboard reloadKeys];
        } break;
        case KBKeyTypeGlobe:
            if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
                [self.delegate keyBoardMainView:self didTapKey:key];
            }
            break;
        case KBKeyTypeCustom:
            // 自定义占位：切换语言或其它操作
            if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
                [self.delegate keyBoardMainView:self didTapKey:key];
            }
            break;
        case KBKeyTypeShift:
            // Shift 已在 KBKeyboardView 内部处理
            break;
    }
}

// 切换功能面板交由外部控制器处理（此处不再实现）

// 设置页展示改由 KeyboardViewController 统一处理
#pragma mark - Theme

- (void)kb_applyTheme {
    KBSkinManager *mgr = [KBSkinManager shared];
    BOOL hasImg = ([mgr currentBackgroundImage] != nil);
    UIColor *bg = mgr.current.keyboardBackground;
    self.backgroundColor = hasImg ? [UIColor clearColor] : bg;
    self.keyboardView.backgroundColor = hasImg ? [UIColor clearColor] : bg;
    [self.keyboardView reloadKeys];
}

@end
