# Orca
#
# Copyright 2026 Igalia, S.L.
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

"""Tests value-change announcements for a slider and a progress bar in web content."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from . import helpers
from .harness import keyboard
from .helpers import BrailleLine

if TYPE_CHECKING:
    from .orca_fixtures import NativeAppSession


@pytest.mark.native_app
def test_caret_navigation_top_to_bottom(web_sliders: NativeAppSession) -> None:
    """Tests Down-arrow caret navigation through the whole page (layout mode on)."""

    session = web_sliders
    helpers.reset_web_state(session)

    # In layout mode the "Volume" label shares the slider's line.
    for expected in (
        ["Volume", "horizontal slider", "2", "50 percent."],
        ["Download"],
        ["30 percent."],
    ):
        keyboard.tap_key(keyboard.KEYSYM_DOWN)
        assert helpers.speech(session) == expected


@pytest.mark.native_app
def test_caret_navigation_top_to_bottom_layout_off(web_sliders: NativeAppSession) -> None:
    """Tests the same navigation with layout mode disabled: the label is its own line."""

    session = web_sliders
    helpers.reset_web_state(session)
    session.orca.set("CaretNavigator", "LayoutMode", False)
    for expected in (
        ["Volume"],
        ["Volume", "horizontal slider", "2", "50 percent."],
        ["Download"],
        ["30 percent."],
    ):
        keyboard.tap_key(keyboard.KEYSYM_DOWN)
        assert helpers.speech(session) == expected


@pytest.mark.native_app
def test_caret_navigation_bottom_to_top(web_sliders: NativeAppSession) -> None:
    """Tests Up-arrow caret navigation from the bottom of the page (layout mode on)."""

    session = web_sliders
    helpers.reset_web_state(session)
    helpers.move_to_bottom(session)

    for expected in (
        ["Download"],
        ["Volume", "horizontal slider", "2", "50 percent."],
        ["Sliders", "heading 1"],
    ):
        keyboard.tap_key(keyboard.KEYSYM_UP)
        assert helpers.speech(session) == expected


@pytest.mark.native_app
def test_caret_navigation_bottom_to_top_layout_off(web_sliders: NativeAppSession) -> None:
    """Tests the same navigation with layout mode disabled: the label is its own line."""

    session = web_sliders
    helpers.reset_web_state(session)
    session.orca.set("CaretNavigator", "LayoutMode", False)
    helpers.move_to_bottom(session)
    for expected in (
        ["Download"],
        ["Volume", "horizontal slider", "2", "50 percent."],
        ["Volume"],
        ["Sliders", "heading 1"],
    ):
        keyboard.tap_key(keyboard.KEYSYM_UP)
        assert helpers.speech(session) == expected


@pytest.mark.native_app
def test_slider_value_changes_and_progress_bar(web_sliders: NativeAppSession) -> None:
    """Tests slider value changes within min/max bounds and a progress bar's value."""

    session = web_sliders
    helpers.reset_web_state(session)

    # Tab gives the slider DOM focus, which switches Orca to focus mode so the
    # arrow keys change its value rather than moving the caret.
    keyboard.tap_key(keyboard.KEYSYM_TAB)
    assert helpers.speech(session) == [
        "Volume",
        "horizontal slider",
        "2",
        "50 percent.",
        "Focus mode",
    ]

    keyboard.tap_key(keyboard.KEYSYM_UP)
    assert helpers.speech(session) == ["3"]

    keyboard.tap_key(keyboard.KEYSYM_UP)
    assert helpers.speech(session) == ["4"]

    # Already at the maximum; pressing Up again changes nothing and says nothing.
    keyboard.tap_key(keyboard.KEYSYM_UP)
    assert helpers.speech(session) == []

    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == ["3"]

    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == ["2"]

    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == ["1"]

    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == ["0"]

    # Already at the minimum; pressing Down again changes nothing and says nothing.
    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == []

    session.orca.press_orca_key(keyboard.KEYSYM_A)
    assert helpers.speech(session) == ["Browse mode"]

    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == ["Download"]

    keyboard.tap_key(keyboard.KEYSYM_DOWN)
    assert helpers.speech(session) == ["30 percent."]


def _where_am_i(session: NativeAppSession) -> tuple[list[str], list[tuple[int, str, str | None]]]:
    keyboard.tap_key(keyboard.KEYSYM_KP_ENTER)
    return helpers.capture(session)


@pytest.mark.native_app
def test_where_am_i_on_slider(web_sliders: NativeAppSession) -> None:
    """Tests Where Am I on a horizontal slider (at its current value of 0)."""

    session = web_sliders
    helpers.reset_web_state(session)

    helpers.tab_and_swallow_presentation(session)
    assert _where_am_i(session) == (
        ["Volume", "horizontal slider", "0", "0 percent."],
        [
            BrailleLine(1, "Volume 0 horizontal slider", "Volume 0 horizontal slider", "\x00" * 26),
            BrailleLine(1, "Volume 0 horizontal slider", "Volume 0 horizontal slider", "\x00" * 26),
        ],
    )
