//
//  ABottomBarView.m
//  AMapNavIOSSDK
//
//  Created by admin on 2026/3/25.
//

#import "ABottomBarView.h"
#import "AMapNavCommonUtil.h"
#import <Masonry/Masonry.h>

// 主题绿
static inline UIColor *ABottomBarThemeGreen(void) {
    return [UIColor colorWithRed:0x1A/255.0 green:0x6E/255.0 blue:0x45/255.0 alpha:1.0];
}

@interface ABottomBarView () <UITextFieldDelegate>

/// 白色圆角背景卡片
@property (nonatomic, strong) UIView       *cardView;

/// 搜索图标
@property (nonatomic, strong) UIImageView  *searchIconView;

/// 目的地输入框
@property (nonatomic, strong) UITextField  *searchField;

/// 规划路线按钮
@property (nonatomic, strong) UIButton     *calRouteButton;

@end

@implementation ABottomBarView

#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self _buildUI];
    }
    return self;
}

- (instancetype)init {
    return [self initWithFrame:CGRectZero];
}

#pragma mark - Build UI

- (void)_buildUI {
    // ── 背景卡片 ──────────────────────────────────────────
    UIView *card = [[UIView alloc] init];
//    card.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:0.96];
    card.backgroundColor = [UIColor whiteColor];
    card.layer.cornerRadius = 16;
    // 顶部阴影
    card.layer.shadowColor   = [UIColor blackColor].CGColor;
    card.layer.shadowOpacity = 0.10;
    card.layer.shadowRadius  = 10;
    card.layer.shadowOffset  = CGSizeMake(0, -3);
    card.layer.masksToBounds = NO;
    [self addSubview:card];
    self.cardView = card;

    [card mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    // ── 搜索框容器（白色圆角行） ──────────────────────────
    UIView *searchRow = [[UIView alloc] init];
    searchRow.backgroundColor = [UIColor whiteColor];
    searchRow.layer.cornerRadius = 5;
    searchRow.layer.masksToBounds = YES;
    searchRow.layer.borderColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1].CGColor;
    searchRow.layer.borderWidth = 1;
    [card addSubview:searchRow];

    [searchRow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(card).offset(-18);
        make.left.equalTo(card).offset(16);
        make.right.equalTo(card).offset(-16);
        make.height.mas_equalTo(50);
    }];

    // ── 搜索图标 ──────────────────────────────────────────
    UIImageView *searchIcon = [[UIImageView alloc] init];
    searchIcon.contentMode = UIViewContentModeScaleAspectFit;
    searchIcon.image = [AMapNavCommonUtil imageWithName3x:@"search_icon"];
    [searchRow addSubview:searchIcon];
    self.searchIconView = searchIcon;

    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(searchRow);
        make.left.equalTo(searchRow).offset(12);
        make.width.height.mas_equalTo(18);
    }];

    // ── 目的地输入框 ──────────────────────────────────────
    UITextField *field = [[UITextField alloc] init];
    field.placeholder = @"请输入目的地，不输入则自动匹配附近成本最低加氢站";
    field.font = [UIFont systemFontOfSize:14];
    field.textColor = [UIColor colorWithWhite:0.1 alpha:1];
    field.borderStyle = UITextBorderStyleNone;
    field.backgroundColor = [UIColor clearColor];
    field.delegate = self;
    // placeholder 颜色
    if (field.placeholder) {
        field.attributedPlaceholder = [[NSAttributedString alloc]
            initWithString:field.placeholder
                attributes:@{NSForegroundColorAttributeName:
                                 [UIColor colorWithWhite:0.65 alpha:1],
                             NSFontAttributeName:
                                 [UIFont systemFontOfSize:13]}];
    }
    [searchRow addSubview:field];
    self.searchField = field;

    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(searchRow);
        make.left.equalTo(searchIcon.mas_right).offset(8);
        make.right.equalTo(searchRow).offset(-12);
        make.top.bottom.equalTo(searchRow);
    }];

    // ── 规划路线按钮 ──────────────────────────────────────
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = ABottomBarThemeGreen();
    btn.layer.cornerRadius = 24;
    btn.layer.masksToBounds = YES;

    // 左侧图标
    UIImageView *routeIcon = [[UIImageView alloc] init];
    routeIcon.contentMode = UIViewContentModeScaleAspectFit;
    routeIcon.image = [AMapNavCommonUtil imageWithName3x:@"cal_ruoute_icon"];
    routeIcon.userInteractionEnabled = NO;
    [btn addSubview:routeIcon];

    // 标题
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"规划路线";
    titleLbl.textColor = [UIColor whiteColor];
    titleLbl.font = [UIFont boldSystemFontOfSize:16];
    titleLbl.userInteractionEnabled = NO;
    [btn addSubview:titleLbl];

    // 图标和文字水平居中整体
    [routeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btn);
        make.right.equalTo(titleLbl.mas_left).offset(-8);
        make.width.height.mas_equalTo(22);
    }];

    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btn);
        // 两者整体水平居中：titleLbl 向右偏移 (22+8)/2 = 15pt
        make.centerX.equalTo(btn).offset(15);
    }];

    [btn addTarget:self action:@selector(_onCalRouteTapped) forControlEvents:UIControlEventTouchUpInside];
    [card addSubview:btn];
    self.calRouteButton = btn;
    
    CGFloat off_y = AMP_TabbarHeight;
    off_y = 0;
#ifdef kAMapSDKDebugFlag
    off_y = 0;
#endif
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchRow.mas_bottom).offset(20);
        make.left.equalTo(card).offset(16);
        make.right.equalTo(card).offset(-16);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(card).offset(-40 + (-AMP_TabbarHeight - 13));
    }];
}

#pragma mark - Public

- (void)setDestinationText:(NSString *)destinationText {
    _destinationText = destinationText;
    self.searchField.text = destinationText;
}

#pragma mark - Actions

- (void)_onCalRouteTapped {
    if ([self.delegate respondsToSelector:@selector(bottomBarViewDidTapCalRoute:)]) {
        [self.delegate bottomBarViewDidTapCalRoute:self];
    }
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // 不弹起键盘，直接通知外部展示搜索页
    if ([self.delegate respondsToSelector:@selector(bottomBarViewDidTapSearchField:)]) {
        [self.delegate bottomBarViewDidTapSearchField:self];
    }
    return NO;
}

@end
