//
//  OrderListUITokensTests.swift
//  suixinkanTests
//

import UIKit
import XCTest
@testable import suixinkan

/// 订单列表 UI Token 与 badge 映射校验。
final class OrderListUITokensTests: XCTestCase {

    func testOrderTokensMatchAndroid() {
        XCTAssertEqual(OrderTokens.filterBackground.hexRGB, 0xF4F4F4)
        XCTAssertEqual(OrderTokens.label563.hexRGB, 0x4B5563)
        XCTAssertEqual(OrderTokens.timestamp.hexRGB, 0x6B7280)
        XCTAssertEqual(OrderTokens.depositSuccess.hexRGB, 0x27AE60)
        XCTAssertEqual(OrderTokens.depositRefunded.hexRGB, 0x6B7280)
        XCTAssertEqual(OrderTokens.searchHeight, 36)
        XCTAssertEqual(OrderTokens.filterPillHeight, 35)
        XCTAssertEqual(OrderTokens.actionButtonHeight, 48)
        XCTAssertEqual(OrderTokens.cardRadius, 12)
    }

    func testPhotographerOrderStatusBadgeMapping() {
        let badge = AppStatusBadge()
        badge.applyOrderStatus(18, text: "已付定金")
        badge.applyOrderStatus(30, text: "已完成")
        badge.applyOrderStatus(50, text: "已退款")
        XCTAssertFalse(badge.isHidden)
    }

    func testDepositOrderStatus50UsesNeutralGray() {
        let badge = AppStatusBadge()
        badge.applyDepositOrderStatus(50, text: "已退款")
        XCTAssertEqual(badge.backgroundColor?.hexRGB, OrderTokens.depositRefundedBackground.hexRGB)
    }

    func testDepositOrderStatus30UsesDepositGreen() {
        let badge = AppStatusBadge()
        badge.applyDepositOrderStatus(30, text: "已完成")
        XCTAssertEqual(badge.backgroundColor?.hexRGB, OrderTokens.depositSuccessBackground.hexRGB)
    }

    func testPhotographerStatus50UsesDangerNotGray() {
        let badge = AppStatusBadge()
        badge.applyOrderStatus(50, text: "已退款")
        XCTAssertEqual(badge.backgroundColor?.hexRGB, AppColor.dangerBackground.hexRGB)
    }

    func testOrderTypeChipIntrinsicSizeWrapsTextWithPadding() {
        let chip = OrderTypeChipView()
        chip.apply(text: "订单类型")

        let size = chip.intrinsicContentSize
        XCTAssertGreaterThan(size.width, "订单类型".size(withAttributes: [.font: UIFont.app(.caption)]).width)
        XCTAssertGreaterThan(size.height, UIFont.app(.caption).lineHeight)
        XCTAssertFalse(chip.isHidden)
    }
}

private extension UIColor {
    var hexRGB: UInt {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        let r = Int(round(red * 255))
        let g = Int(round(green * 255))
        let b = Int(round(blue * 255))
        return UInt(r << 16 | g << 8 | b)
    }
}
