//
//  HomeScenicHeaderView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 首页顶部景区选择条。
final class HomeScenicHeaderView: UIView {

    var onTap: (() -> Void)?

    private let titleLabel = UILabel()
    private let arrowView = UIImageView(image: UIImage(systemName: "chevron.down"))

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = AppColor.cardBackground
        titleLabel.font = .app(.subtitle)
        titleLabel.textColor = AppColor.textPrimary
        arrowView.tintColor = AppColor.textSecondary
        arrowView.contentMode = .scaleAspectFit

        addSubview(titleLabel)
        addSubview(arrowView)

        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(AppSpacing.screenHorizontalInset)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualTo(arrowView.snp.leading).offset(-AppSpacing.xs)
        }
        arrowView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-AppSpacing.screenHorizontalInset)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }

        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        addGestureRecognizer(tap)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func apply(scenicName: String) {
        let trimmed = scenicName.trimmingCharacters(in: .whitespacesAndNewlines)
        titleLabel.text = trimmed.isEmpty ? "请选择景区" : trimmed
    }

    @objc private func handleTap() {
        onTap?()
    }
}
