//
//  HomeCommonMenuStoreTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 常用应用持久化测试。
final class HomeCommonMenuStoreTests: XCTestCase {

    private var defaults: UserDefaults!
    private var store: HomeCommonMenuStore!

    override func setUp() {
        defaults = UserDefaults(suiteName: "HomeCommonMenuStoreTests")!
        defaults.removePersistentDomain(forName: "HomeCommonMenuStoreTests")
        store = HomeCommonMenuStore(defaults: defaults)
    }

    override func tearDown() {
        defaults.removePersistentDomain(forName: "HomeCommonMenuStoreTests")
        super.tearDown()
    }

    func testDefaultCommonURIsTakeFirstFourAndExcludeMessageCenter() {
        let permissions = [
            HomePermissionItem(id: "0", name: "消息", uri: "message_center"),
        ] + (1...5).map {
            HomePermissionItem(id: "\($0)", name: "菜单\($0)", uri: "uri_\($0)")
        }
        let uris = HomeCommonMenuStore.defaultCommonURIs(from: permissions)
        XCTAssertEqual(uris, ["uri_1", "uri_2", "uri_3", "uri_4"])
    }

    func testSavedMessageCenterIsRemovedFromLegacyCommonURIs() {
        let key = "scenic_user_u1_role_photographer_common_uris"
        defaults.set(["message_center", "wallet"], forKey: key)

        let uris = store.savedCommonURIs(accountScope: "scenic_user_u1", roleCode: "photographer")

        XCTAssertEqual(uris, ["wallet"])
        XCTAssertEqual(defaults.stringArray(forKey: key), ["wallet"])
    }

    func testSavedCommonURIsOverrideDefault() {
        let permissions = [
            HomePermissionItem(id: "1", name: "钱包", uri: "wallet"),
            HomePermissionItem(id: "2", name: "云盘", uri: "cloud_management"),
            HomePermissionItem(id: "3", name: "任务", uri: "task_management"),
        ]
        let allMenus = HomeMenuCatalog.visibleMenus(from: permissions)
        store.saveCommonURIs(["cloud_management"], accountScope: "scenic_user_u1", roleCode: "photographer")
        let common = store.commonMenus(
            from: allMenus,
            permissions: permissions,
            accountScope: "scenic_user_u1",
            roleCode: "photographer"
        )
        XCTAssertEqual(common.map(\.uri), ["cloud_management"])
        XCTAssertEqual(
            defaults.stringArray(forKey: "scenic_user_u1_role_photographer_common_uris"),
            ["cloud_management"]
        )
    }

    func testIncompleteScopeDoesNotPersistCommonURIs() {
        store.saveCommonURIs(["wallet"], accountScope: "", roleCode: "photographer")
        store.saveCommonURIs(["wallet"], accountScope: "scenic_user_u1", roleCode: "")

        XCTAssertNil(defaults.object(forKey: "_role_photographer_common_uris"))
        XCTAssertNil(defaults.object(forKey: "scenic_user_u1_role__common_uris"))
    }

    func testSplitMenusSeparatesCommonAndMore() {
        let menus = [
            HomeMenuItem(uri: "wallet", title: "钱包", iconName: "wallet.pass"),
            HomeMenuItem(uri: "cloud_management", title: "云盘", iconName: "icloud"),
            HomeMenuItem(uri: "task_management", title: "任务", iconName: "checklist"),
            HomeMenuItem(uri: "message_center", title: "消息中心", iconName: "bell"),
        ]
        let split = HomeCommonMenuStore.splitMenus(
            from: menus,
            commonURIs: ["wallet", "task_management", "message_center"]
        )
        XCTAssertEqual(split.common.map(\.uri), ["wallet", "task_management"])
        XCTAssertEqual(split.more.map(\.uri), ["cloud_management"])
    }

    func testPhotographerReportCatalogItemExists() {
        let item = HomeMenuCatalog.item(for: "report_photographer")

        XCTAssertEqual(item?.title, "举报摄影师")
        XCTAssertEqual(item?.iconName, "shield.lefthalf.filled")
    }

    func testHomeMenuCatalogUsesIOS16CompatibleIcons() {
        XCTAssertEqual(HomeMenuCatalog.item(for: "space_settings")?.iconName, "home_menu_space")
        XCTAssertEqual(HomeMenuCatalog.item(for: "wallet")?.iconName, "home_menu_wallet")
        XCTAssertEqual(HomeMenuCatalog.item(for: "sample_management")?.iconName, "sample_ic_home_menu_route")
        XCTAssertEqual(HomeMenuCatalog.item(for: "travel_album")?.iconName, "rectangle.stack.badge.plus")
        XCTAssertEqual(HomeMenuCatalog.item(for: "store")?.iconName, "building.2.fill")
        XCTAssertEqual(HomeMenuCatalog.item(for: "/scenic-queue")?.iconName, "person.3.fill")
        XCTAssertEqual(HomeMenuItem.moreFunctions.iconName, "ellipsis")
    }
}
