//
//  HomeMenuIconFactoryTests.swift
//  suixinkanTests
//

import UIKit
import XCTest
@testable import suixinkan

/// 首页菜单图标加载测试。
final class HomeMenuIconFactoryTests: XCTestCase {

    func testAssetIconTakesPrecedence() {
        let image = HomeMenuIconFactory.image(named: "home_menu_space")

        XCTAssertNotNil(image)
        XCTAssertEqual(image?.size, HomeMenuIconFactory.canvasSize)
        XCTAssertNil(UIImage(systemName: "home_menu_space"))
    }

    func testSystemSymbolsPreserveSymbolRepresentation() {
        let iconNames = [
            "ellipsis",
            "dot.radiowaves.left.and.right",
            "rectangle.stack.badge.plus",
            "mappin.circle.fill",
        ]

        for iconName in iconNames {
            let image = HomeMenuIconFactory.image(named: iconName)

            XCTAssertNotNil(image)
            XCTAssertTrue(image?.isSymbolImage ?? false)
            XCTAssertNotNil(image?.symbolConfiguration)
        }
    }

    func testComplexSystemSymbolResolves() {
        XCTAssertNotNil(HomeMenuIconFactory.image(named: "dot.radiowaves.left.and.right"))
        XCTAssertNotNil(HomeMenuIconFactory.image(named: "rectangle.stack.badge.plus"))
    }

    func testLocationReportIconKeepsSystemSymbolLayers() {
        let image = HomeMenuIconFactory.image(named: "mappin.circle.fill")

        XCTAssertNotNil(image)
        XCTAssertTrue(image?.isSymbolImage ?? false)
    }
}

/// 首页常用应用 cell 的图标布局与复用测试。
@MainActor
final class HomeMenuCellTests: XCTestCase {

    func testIconKeepsFixedSizeWhenReusedAtIPhoneXWidth() throws {
        let cell = HomeMenuCell(frame: CGRect(x: 0, y: 0, width: 109, height: 109))
        let menus = [
            HomeMenuItem(uri: "space", title: "空间设置", iconName: "home_menu_space"),
            HomeMenuItem(uri: "live", title: "直播管理", iconName: "dot.radiowaves.left.and.right"),
            HomeMenuItem(uri: "location", title: "位置上报", iconName: "mappin.circle.fill"),
            HomeMenuItem(uri: "more", title: "更多功能", iconName: "ellipsis"),
        ]

        for menu in menus {
            cell.prepareForReuse()
            cell.apply(menu: menu)
            cell.setNeedsLayout()
            cell.layoutIfNeeded()

            let iconView = try XCTUnwrap(findImageView(in: cell, identifier: menu.iconName))
            let alignmentSize = iconView.alignmentRect(forFrame: iconView.frame).size
            XCTAssertEqual(alignmentSize.width, 24, accuracy: 0.001)
            XCTAssertEqual(alignmentSize.height, 24, accuracy: 0.001)
            XCTAssertEqual(iconView.superview?.bounds.size, CGSize(width: 40, height: 40))
            XCTAssertEqual(iconView.contentMode, .center)
            XCTAssertNil(iconView.preferredSymbolConfiguration)

            if menu.iconName == "home_menu_space" {
                XCTAssertEqual(iconView.image?.size, HomeMenuIconFactory.canvasSize)
                XCTAssertFalse(iconView.image?.isSymbolImage ?? true)
            } else {
                XCTAssertTrue(iconView.image?.isSymbolImage ?? false)
            }
        }
    }

    private func findImageView(in view: UIView, identifier: String) -> UIImageView? {
        if let imageView = view as? UIImageView,
           imageView.accessibilityIdentifier == identifier {
            return imageView
        }
        return view.subviews.lazy.compactMap {
            self.findImageView(in: $0, identifier: identifier)
        }.first
    }
}
