//
//  HomeLocationReportCardView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 首页位置上报卡片。
final class HomeLocationReportCardView: UIView {

    var onReportTap: (() -> Void)?

    private let titleLabel = UILabel()
    private let addressLabel = UILabel()
    private let reportButton = UIButton(type: .system)

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = AppColor.cardBackground
        layer.cornerRadius = AppRadius.md

        titleLabel.text = "位置上报"
        titleLabel.font = .app(.title)
        titleLabel.textColor = AppColor.textPrimary

        addressLabel.font = .app(.body)
        addressLabel.textColor = AppColor.textSecondary
        addressLabel.numberOfLines = 2

        reportButton.setTitle("立即上报", for: .normal)
        reportButton.setTitleColor(.white, for: .normal)
        reportButton.backgroundColor = AppColor.primary
        reportButton.layer.cornerRadius = AppRadius.sm
        reportButton.titleLabel?.font = .app(.bodyMedium)
        reportButton.contentEdgeInsets = UIEdgeInsets(top: AppSpacing.xs, left: 14, bottom: AppSpacing.xs, right: 14)
        reportButton.addTarget(self, action: #selector(reportTapped), for: .touchUpInside)

        addSubview(titleLabel)
        addSubview(addressLabel)
        addSubview(reportButton)

        titleLabel.snp.makeConstraints { make in
            make.top.leading.equalToSuperview().offset(AppSpacing.screenHorizontalInset)
        }
        addressLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(AppSpacing.xs)
            make.leading.equalToSuperview().offset(AppSpacing.screenHorizontalInset)
            make.bottom.equalToSuperview().offset(-AppSpacing.screenHorizontalInset)
            make.trailing.lessThanOrEqualTo(reportButton.snp.leading).offset(-AppSpacing.sm)
        }
        reportButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-AppSpacing.screenHorizontalInset)
            make.centerY.equalToSuperview()
        }
    }

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

    func apply(address: String, isReporting: Bool) {
        addressLabel.text = address
        reportButton.isEnabled = !isReporting
        reportButton.alpha = isReporting ? 0.6 : 1
    }

    @objc private func reportTapped() { onReportTap?() }
}
