import sys

import pytest

from clarabot_nano.utility import parse_environment_flag
from clarabot_nano_tests.testing.control import run_nano_control_tester
from clarabot_nano_tests.testing.server.manager import DevServerManager


async def _run_control_window_tests(
    tmpdir,
    reset_user_app_dirs,
    dev_nano_root_dir,
    testing_server_manager: DevServerManager,
    testing_server_user0,
    disable_systray,
):
    """
    Test:
        - basic behaviour of window and tray icon
    """
    test_log = await run_nano_control_tester(
        tmpdir,
        testing_server_manager,
        reset_user_app_dirs,
        dev_nano_root_dir,
        "clarabot_nano_tests.control.cases._window_geom:_WindowGeomControlApplicationTester",
        {"disable_systray": disable_systray},
        timeout=20.0,
    )
    target_log = (
        [
            "Starting _WindowGeomControlApplicationTester",
            ("Systray disabled for testing." if disable_systray else "Systray available: True"),
            "Phase 0 passed.",
            "Phase 1 passed.",
            "Phase 2 passed.",
            "Phase 3 passed.",
        ]
        + (
            [
                "Phase 4 passed, jumping to 13.",
            ]
            if disable_systray
            else [
                "Phase 4 passed.",
                "Phase 5 passed.",
                "Phase 6 passed.",
                "Phase 7 passed.",
                "Phase 8 passed.",
                "Phase 9 passed.",
                "Phase 10 passed.",
                "Phase 11 passed.",
                "Phase 12 passed.",
            ]
        )
        + [
            "Test phases passed.",
            "Exiting",
            "",
        ]
    )
    assert test_log == target_log, test_log


@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="tray behaves differently on darwin")
@pytest.mark.skipif(parse_environment_flag("CBNANO_TEST_WITHOUT_SERVER"), reason="server-dependant tests disabled")
@pytest.mark.asyncio
async def test_control_window_with_systray(
    tmpdir,
    reset_user_app_dirs,
    dev_nano_root_dir,
    testing_server_manager: DevServerManager,
    testing_server_user0,
):
    await _run_control_window_tests(
        tmpdir, reset_user_app_dirs, dev_nano_root_dir, testing_server_manager, testing_server_user0, False
    )


@pytest.mark.skipif(parse_environment_flag("CBNANO_TEST_WITHOUT_SERVER"), reason="server-dependant tests disabled")
@pytest.mark.asyncio
async def test_control_window_no_systray(
    tmpdir,
    reset_user_app_dirs,
    dev_nano_root_dir,
    testing_server_manager: DevServerManager,
    testing_server_user0,
):
    await _run_control_window_tests(
        tmpdir, reset_user_app_dirs, dev_nano_root_dir, testing_server_manager, testing_server_user0, True
    )
