//
//  CustomizeScreen.swift
//  Tests iOS
//
//  Screen object for the Customize sub-tab — theme, voting layout, and day view style pickers.
//

import XCTest

struct CustomizeScreen {
    let app: XCUIApplication

    // MARK: - Theme Mode Buttons

    func themeButton(named name: String) -> XCUIElement {
        app.buttons[UITestID.Customize.themeButton(name)]
    }

    // MARK: - Voting Layout Buttons

    func votingLayoutButton(named name: String) -> XCUIElement {
        app.buttons[UITestID.Customize.votingLayoutButton(name)]
    }

    // MARK: - Day View Style Buttons

    func dayViewStyleButton(named name: String) -> XCUIElement {
        app.buttons[UITestID.Customize.dayStyleButton(name)]
    }

    func iconPackButton(named name: String) -> XCUIElement {
        app.buttons[UITestID.Customize.iconPackButton(name)]
    }

    func appThemeCard(named name: String) -> XCUIElement {
        app.element(UITestID.Customize.appThemeCard(name))
    }

    // MARK: - Actions

    func selectTheme(_ name: String) {
        tapHorizontallyScrollableButton(themeButton(named: name))
    }

    func selectVotingLayout(_ name: String) {
        tapHorizontallyScrollableButton(votingLayoutButton(named: name))
    }

    func selectDayViewStyle(_ name: String) {
        tapHorizontallyScrollableButton(dayViewStyleButton(named: name))
    }

    func selectIconPack(_ name: String) {
        let button = iconPackButton(named: name)
        _ = app.swipeUntilExists(button, direction: .up, maxSwipes: 6)
        button.tapWhenReady(timeout: 5)
    }

    func personalityPackButton(named name: String) -> XCUIElement {
        app.element(UITestID.Customize.personalityPackButton(name))
    }

    func selectPersonalityPack(_ name: String) {
        let button = personalityPackButton(named: name)
        _ = app.swipeUntilExists(button, direction: .up, maxSwipes: 8)
        button.tapWhenReady(timeout: 5)
    }

    // MARK: - Assertions

    func assertThemeButtonExists(_ name: String, file: StaticString = #file, line: UInt = #line) {
        XCTAssertTrue(
            themeButton(named: name).waitForExistence(timeout: 5),
            "Theme button '\(name)' should exist",
            file: file, line: line
        )
    }

    @discardableResult
    func openThemePicker(file: StaticString = #file, line: UInt = #line) -> Bool {
        let browseButton = app.element(UITestID.Settings.browseThemesButton)
        guard browseButton.waitForExistence(timeout: 5) else {
            XCTFail("Browse Themes button should exist", file: file, line: line)
            return false
        }
        browseButton.tapWhenReady(timeout: 5, file: file, line: line)

        let firstCard = appThemeCard(named: "Zen Garden")
        return firstCard.waitForExistence(timeout: 5)
    }

    // MARK: - Private

    private func tapHorizontallyScrollableButton(_ button: XCUIElement) {
        if button.waitForExistence(timeout: 1) {
            button.tapWhenReady(timeout: 3)
            return
        }

        for _ in 0..<6 {
            app.swipeLeft()
            if button.waitForExistence(timeout: 1) {
                button.tapWhenReady(timeout: 3)
                return
            }
        }

        for _ in 0..<6 {
            app.swipeRight()
            if button.waitForExistence(timeout: 1) {
                button.tapWhenReady(timeout: 3)
                return
            }
        }
    }
}
