//
//  UIButtonConfigurationTests.swift
//  suixinkanTests
//

import UIKit
import XCTest
@testable import suixinkan

/// `UIButton` configuration 内容边距辅助方法测试。
@MainActor
final class UIButtonConfigurationTests: XCTestCase {

    func testContentInsetsCreatePlainConfiguration() {
        let button = UIButton(type: .system)

        button.setConfigurationContentInsets(
            NSDirectionalEdgeInsets(top: 2, leading: 4, bottom: 6, trailing: 8)
        )

        let contentInsets = button.configuration?.contentInsets
        XCTAssertEqual(contentInsets?.top, 2)
        XCTAssertEqual(contentInsets?.leading, 4)
        XCTAssertEqual(contentInsets?.bottom, 6)
        XCTAssertEqual(contentInsets?.trailing, 8)
    }

    func testContentInsetsPreserveExistingConfiguration() {
        let image = UIImage(systemName: "star.fill")
        var configuration = UIButton.Configuration.filled()
        configuration.title = "保留标题"
        configuration.image = image
        configuration.imagePlacement = .trailing
        configuration.imagePadding = 7
        configuration.baseForegroundColor = .red
        let button = UIButton(configuration: configuration)

        button.setConfigurationContentInsets(
            NSDirectionalEdgeInsets(top: 1, leading: 3, bottom: 5, trailing: 9)
        )

        XCTAssertEqual(button.configuration?.title, "保留标题")
        XCTAssertEqual(button.configuration?.image, image)
        XCTAssertEqual(button.configuration?.imagePlacement, .trailing)
        XCTAssertEqual(button.configuration?.imagePadding, 7)
        XCTAssertEqual(button.configuration?.baseForegroundColor, .red)
        XCTAssertEqual(button.configuration?.contentInsets.leading, 3)
        XCTAssertEqual(button.configuration?.contentInsets.trailing, 9)
    }

    func testContentInsetsPreserveLegacyButtonContentAndAllowUpdates() {
        let image = UIImage(systemName: "star")
        let button = UIButton(type: .system)
        button.setTitle("原始标题", for: .normal)
        button.setTitleColor(.blue, for: .normal)
        button.setImage(image, for: .normal)

        button.setConfigurationContentInsets(
            NSDirectionalEdgeInsets(top: 2, leading: 4, bottom: 2, trailing: 4)
        )

        XCTAssertEqual(button.currentTitle, "原始标题")
        XCTAssertEqual(button.currentTitleColor, .blue)
        XCTAssertEqual(button.currentImage, image)

        button.setTitle("更新标题", for: .normal)
        button.setTitleColor(.red, for: .normal)
        XCTAssertEqual(button.currentTitle, "更新标题")
        XCTAssertEqual(button.currentTitleColor, .red)
    }

    func testImagePlacementPreservesExistingConfiguration() {
        var configuration = UIButton.Configuration.plain()
        configuration.title = "电话"
        configuration.contentInsets = NSDirectionalEdgeInsets(top: 1, leading: 2, bottom: 3, trailing: 4)
        let button = UIButton(configuration: configuration)

        button.setConfigurationImagePlacement(.trailing, padding: 6)

        XCTAssertEqual(button.configuration?.title, "电话")
        XCTAssertEqual(button.configuration?.contentInsets.leading, 2)
        XCTAssertEqual(button.configuration?.contentInsets.trailing, 4)
        XCTAssertEqual(button.configuration?.imagePlacement, .trailing)
        XCTAssertEqual(button.configuration?.imagePadding, 6)
    }
}
