//
//  ACustomStepView.m
//  AMapNavIOSSDK
//
//  Created by admin on 2026/3/11.
//

#import "ACustomStepView.h"
#import <Masonry/Masonry.h>

#define kStepValue 0.5

@interface ACustomStepView ()
@property CGFloat value;
@property CGFloat maxValue;
@property CGFloat minValue;
@end

@implementation ACustomStepView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 4;
        self.layer.masksToBounds = YES;
        self.clipsToBounds = YES;
        self.layer.borderColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1].CGColor;
        self.layer.borderWidth = 1;
        
        [self setupSubviews];
        
        self.value = 0; // 初始值
    }
    return self;
}

- (instancetype)initWithValue:(CGFloat)currentValue maxValue:(CGFloat)maxValue min:(CGFloat)minValue {
//    self = [super initWithFrame:CGRectZero];
    
    if (self) {
        _value = currentValue;
        _maxValue = maxValue;
        _minValue = minValue;
    }
    
    
    return self;
}

- (void)setupSubviews {
    // 减按钮
    UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [minusButton setTitle:@"-" forState:UIControlStateNormal];
    minusButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [minusButton addTarget:self action:@selector(decrement:) forControlEvents:UIControlEventTouchUpInside];
    [minusButton setTintColor:[UIColor colorWithRed:0x35/255.0 green:0x35/255.0 blue:0x35/255.0 alpha:1]];
    [self addSubview:minusButton];
    
    // 加按钮
    UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [plusButton setTitle:@"+" forState:UIControlStateNormal];
    plusButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [plusButton setTintColor:[UIColor colorWithRed:0x35/255.0 green:0x35/255.0 blue:0x35/255.0 alpha:1]];
    
    [plusButton addTarget:self action:@selector(increment:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:plusButton];
    
    [plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.height.equalTo(@40);
    }];

    UIView * line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(plusButton.mas_bottom);
            make.height.equalTo(@1);
    }];
    
    
    [minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(plusButton.mas_bottom);
            make.height.equalTo(plusButton);
    }];
    
    
}

// 减1操作
- (void)decrement:(UIButton *)sender {
    if (self.value <= self.minValue) {
        return;
    }
    if (self.value - kStepValue < self.minValue) {
        self.value = self.minValue;
        return;
    }
    
    self.value = self.value - kStepValue;
}

// 加1操作
- (void)increment:(UIButton *)sender {
    if (self.value >= self.maxValue) {
        return;
    }
    if (self.value + kStepValue > self.maxValue) {
        self.value = self.maxValue;
        return;
    }
    
    self.value = self.value + kStepValue;
    
}


@end
