//
//  WithdrawalSettingsAuditViewController.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 提现设置审核状态页，展示银行卡审核结果。
final class WithdrawalSettingsAuditViewController: BaseViewController {

    private let bankCard: BankCardInfo

    private let scrollView = UIScrollView()
    private let contentStack = UIStackView()
    private let statusBadge = ProfileStatusBadgeView()
    private let rejectLabel = UILabel()
    private let frontImageView = UIImageView()
    private let backImageView = UIImageView()

    init(bankCard: BankCardInfo) {
        self.bankCard = bankCard
        super.init(nibName: nil, bundle: nil)
    }

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

    override func setupNavigationBar() {
        title = "提现设置"
    }

    override func setupUI() {
        view.backgroundColor = UIColor(hex: 0xF7FAFF)
        contentStack.axis = .vertical
        contentStack.spacing = 16

        rejectLabel.numberOfLines = 0
        rejectLabel.font = .systemFont(ofSize: 14)
        rejectLabel.textColor = UIColor(hex: 0xEF4444)

        [frontImageView, backImageView].forEach {
            $0.contentMode = .scaleAspectFit
            $0.backgroundColor = UIColor(hex: 0xF8FAFC)
            $0.layer.cornerRadius = 8
            $0.clipsToBounds = true
            $0.snp.makeConstraints { make in make.height.equalTo(140) }
        }

        view.addSubview(scrollView)
        scrollView.addSubview(contentStack)

        contentStack.addArrangedSubview(makeTitle("审核状态"))
        contentStack.addArrangedSubview(statusBadge)
        if !bankCard.rejectReason.isEmpty, bankCard.auditStatus == 3 {
            rejectLabel.text = "驳回原因：\(bankCard.rejectReason)"
            contentStack.addArrangedSubview(rejectLabel)
        }
        contentStack.addArrangedSubview(makeInfoRow("持卡人", bankCard.realName))
        contentStack.addArrangedSubview(makeInfoRow("开户银行", bankCard.bankName))
        contentStack.addArrangedSubview(makeInfoRow("支行", bankCard.branchName))
        contentStack.addArrangedSubview(makeInfoRow("卡号", ProfileViewModel.maskCardNumber(bankCard.cardNumber)))
        contentStack.addArrangedSubview(makeTitle("银行卡正面"))
        contentStack.addArrangedSubview(frontImageView)
        contentStack.addArrangedSubview(makeTitle("银行卡反面"))
        contentStack.addArrangedSubview(backImageView)

        applyStatus()
        frontImageView.loadRemoteImage(urlString: bankCard.frontUrl)
        backImageView.loadRemoteImage(urlString: bankCard.backUrl)
    }

    override func setupConstraints() {
        scrollView.snp.makeConstraints { make in make.edges.equalToSuperview() }
        contentStack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(16)
            make.width.equalTo(scrollView).offset(-32)
        }
    }

    private func applyStatus() {
        let text = bankCard.auditStatusLabel.isEmpty ? "审核中" : bankCard.auditStatusLabel
        switch bankCard.auditStatus {
        case 2:
            statusBadge.apply(style: .bankApproved, text: text)
        case 3:
            statusBadge.apply(style: .bankRejected, text: text)
        default:
            statusBadge.apply(style: .bankPending, text: text)
        }
    }

    private func makeTitle(_ text: String) -> UILabel {
        let label = UILabel()
        label.text = text
        label.font = .systemFont(ofSize: 15, weight: .semibold)
        return label
    }

    private func makeInfoRow(_ title: String, _ value: String) -> UILabel {
        let label = UILabel()
        label.font = .systemFont(ofSize: 14)
        label.textColor = AppColor.text666
        label.text = "\(title)：\(value)"
        return label
    }
}
