//
//  KBDirectionIndicatorView.m
//  keyBoard
//
//  Created by Mac on 2025/11/5.
//

#import "KBDirectionIndicatorView.h"
#import "UIView+HW_Frame.h"

@interface KBDirectionIndicatorView ()
@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIView *rightView;
@end

@implementation KBDirectionIndicatorView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        _leftView = [UIView new];
        _rightView = [UIView new];
        _leftView.backgroundColor = _rightView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1];
        [self addSubview:_leftView];
        [self addSubview:_rightView];
    }
    return self;
}

- (CGSize)indicatorSize { return CGSizeMake(34, 13); }

- (void)setupSubviews {
    CGSize size = [self indicatorSize];
    self.hw_size = size;
    CGFloat h = 5, c = h/2.0;
    _leftView.frame  = CGRectMake(0, 0, size.width/2 + c, h);
    _rightView.frame = CGRectMake(size.width/2 - c, 0, size.width/2 + c, h);
    _leftView.hw_centerY = _rightView.hw_centerY = self.hw_height/2.0;
    _leftView.layer.cornerRadius = MIN(_leftView.hw_width, _leftView.hw_height)/2;
    _rightView.layer.cornerRadius = MIN(_rightView.hw_width, _rightView.hw_height)/2;
}

// 默认库会在很多时机传 HWIndicatorStateNormal，这里忽略，不改变我们基于状态的朝向
- (void)didChangeToState:(HWIndicatorState)state {
    // no-op 或者根据需要做轻微弹性动画
}

- (void)applyPresentationState:(PresentationState)state {
    CGFloat angle = (CGFloat)(20.0 * M_PI / 180.0);
    void (^animate)(void) = ^{
        switch (state) {
            case PresentationStateShort: // 最低：指向上(∧)
                self.leftView.transform  = CGAffineTransformMakeRotation(-angle);
                self.rightView.transform = CGAffineTransformMakeRotation(angle);
                break;
            case PresentationStateLong: // 最高：指向下(∨)
            default:
                self.leftView.transform  = CGAffineTransformMakeRotation(angle);
                self.rightView.transform = CGAffineTransformMakeRotation(-angle);
                break;
        }
    };
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut animations:animate completion:nil];
}
@end
