//
//  KBKeyButton.m
//  CustomKeyboard
//

#import "KBKeyButton.h"
#import "KBKey.h"
#import "KBSkinManager.h"

@implementation KBKeyButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self applyDefaultStyle];
    }
    return self;
}

- (void)applyDefaultStyle {
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
    KBSkinTheme *t = [KBSkinManager shared].current;
    [self setTitleColor:t.keyTextColor forState:UIControlStateNormal];
    [self setTitleColor:t.keyTextColor forState:UIControlStateHighlighted];
    self.backgroundColor = t.keyBackground;
    self.layer.cornerRadius = 6.0;      // 圆角
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor; // 阴影效果
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowRadius = 1.5;
    [self refreshStateAppearance];
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    // 简单按压反馈：选中态不改变透明度，避免和高亮态冲突
    if (self.isSelected) {
        self.alpha = 1.0;
    } else {
        self.alpha = highlighted ? 0.2 : 1.0;
    }
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    [self refreshStateAppearance];
}

- (void)refreshStateAppearance {
    // 选中态用于 Shift/CapsLock 等特殊按键的高亮显示
    KBSkinTheme *t = [KBSkinManager shared].current;
    if (self.isSelected) {
        self.backgroundColor = t.keyHighlightBackground ?: t.keyBackground;
    } else {
        self.backgroundColor = t.keyBackground;
    }
}

@end
