#!/usr/bin/env python3
#
# Copyright (c) 2024 Stormux
# Copyright (c) 2010-2012 The Orca Team
# Copyright (c) 2012 Igalia, S.L.
# Copyright (c) 2005-2010 Sun Microsystems Inc.
#
# 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.
#
# Forked from Orca screen reader.
# Cthulhu project: https://git.stormux.org/storm/cthulhu

"""Provides the default implementation for flat review for Cthulhu."""

__id__        = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2005-2008 Sun Microsystems Inc." \
                "Copyright (c) 2016 Igalia, S.L."
__license__   = "LGPL"

import gi
gi.require_version("Atspi", "2.0")
from gi.repository import Atspi
import re

from . import braille
from . import debug
from . import cthulhu
from . import cthulhu_state
from . import settings
from .ax_event_synthesizer import AXEventSynthesizer
from .ax_component import AXComponent
from .ax_object import AXObject
from .ax_text import AXText
from .ax_utilities import AXUtilities

EMBEDDED_OBJECT_CHARACTER = '\ufffc'

class Char:
    """A character's worth of presentable information."""

    def __init__(self, word, index, startOffset, string, x, y, width, height):
        """Creates a new char.

        Arguments:
        - word: the Word instance this belongs to
        - startOffset: the start offset with respect to the accessible
        - string: the actual char
        - x, y, width, height: the extents of this Char on the screen
        """

        self.word = word
        self.index = index
        self.startOffset = startOffset
        self.endOffset = startOffset + 1
        self.string = string
        self.x = x
        self.y = y
        self.width = width
        self.height = height


class Word:
    """A single chunk (word or object) of presentable information."""

    def __init__(self, zone, index, startOffset, string, x, y, width, height):
        """Creates a new Word.

        Arguments:
        - zone: the Zone instance this belongs to
        - index: the index of this Word in the Zone
        - startOffset: the start offset with respect to the accessible
        - string: the actual string
        - x, y, width, height: the extents of this Word on the screen
        """

        self.zone = zone
        self.index = index
        self.startOffset = startOffset
        self.string = string
        self.length = len(string)
        self.endOffset = self.startOffset + len(string)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.chars = []

    def __str__(self):
        return "WORD: '%s' (%i-%i) %s" % \
            (self.string.replace("\n", "\\n"),
             self.startOffset,
             self.endOffset,
             self.zone.accessible)

    def __getattribute__(self, attr):
        if attr != "chars":
            return super().__getattribute__(attr)

        chars = []
        for i, char in enumerate(self.string):
            start = i + self.startOffset
            extents = self._getCharExtents(start)
            chars.append(Char(self, i, start, char, *extents))

        return chars

    def _getCharExtents(self, start):
        # NOTE: We intentionally avoid faking character extents to improve reviewability.
        extents = self.x, self.y, self.width, self.height

        if AXObject.supports_text(self.zone.accessible):
            try:
                rect = Atspi.Text.get_range_extents(
                    self.zone.accessible, start, start + 1, Atspi.CoordType.SCREEN)
                extents = rect.x, rect.y, rect.width, rect.height
            except Exception as error:
                tokens = ["FLAT REVIEW: Exception in getRangeExtents:", error]
                debug.printTokens(debug.LEVEL_INFO, tokens, True)

        return extents

    def getRelativeOffset(self, offset):
        """Returns the char offset with respect to this word or -1."""

        if self.startOffset <= offset < self.startOffset + len(self.string):
            return offset - self.startOffset

        return -1


class Zone:
    """Represents text that is a portion of a single horizontal line."""

    WORDS_RE = re.compile(r"(\S+\s*)", re.UNICODE)
    TEXT_ROLES = (
        Atspi.Role.LABEL,
        Atspi.Role.MENU,
        Atspi.Role.MENU_ITEM,
        Atspi.Role.CHECK_MENU_ITEM,
        Atspi.Role.RADIO_MENU_ITEM,
        Atspi.Role.PAGE_TAB,
        Atspi.Role.PUSH_BUTTON,
        Atspi.Role.TABLE_CELL,
    )

    def __init__(self, accessible, string, x, y, width, height, role=None):
        """Creates a new Zone.

        Arguments:
        - accessible: the Accessible associated with this Zone
        - string: the string being displayed for this Zone
        - extents: x, y, width, height in screen coordinates
        - role: Role to override accessible's role.
        """

        self.accessible = accessible
        self.startOffset = 0
        self._string = string
        self.length = len(string)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.role = role or AXObject.get_role(accessible)
        self._words = []

    def __str__(self):
        return "ZONE: '%s' %s" % (self._string.replace("\n", "\\n"), self.accessible)

    def __getattribute__(self, attr):
        """To ensure we update the content."""

        if attr not in ["words", "string"]:
            return super().__getattribute__(attr)

        if attr == "string":
            return self._string

        if not self._shouldFakeText():
            return self._words

        extents = self._getFakeTextExtents()
        words = []
        for i, word in enumerate(re.finditer(self.WORDS_RE, self._string)):
            words.append(Word(self, i, word.start(), word.group(), *extents))

        self._words = words
        return words

    def _shouldFakeText(self):
        """Returns True if we should try to fake the text interface"""

        if self.role in self.TEXT_ROLES:
            return True

        return False

    def _getFakeTextExtents(self):
        # NOTE: We intentionally avoid faking word extents to improve reviewability.
        return self.x, self.y, self.width, self.height

    def _extentsAreOnSameLine(self, zone, pixelDelta=5):
        """Returns True if this Zone is physically on the same line as zone."""

        if self.width == 0 and self.height == 0:
            return zone.y <= self.y <= zone.y + zone.height

        if zone.width == 0 and self.height == 0:
            return self.y <= zone.y <= self.y + self.height

        highestBottom = min(self.y + self.height, zone.y + zone.height)
        lowestTop = max(self.y, zone.y)
        if lowestTop >= highestBottom:
            return False

        middle = self.y + self.height / 2
        zoneMiddle = zone.y + zone.height / 2
        if abs(middle - zoneMiddle) > pixelDelta:
            return False

        return True

    def onSameLine(self, zone):
        """Returns True if we treat this Zone and zone as being on one line."""

        if Atspi.Role.SCROLL_BAR in [self.role, zone.role]:
            return self.accessible == zone.accessible

        thisParent = AXObject.get_parent(self.accessible)
        thisParentRole = AXObject.get_role(thisParent)
        zoneParent = AXObject.get_parent(zone.accessible)
        zoneParentRole = AXObject.get_role(zoneParent)
        if Atspi.Role.MENU_BAR in [thisParentRole, zoneParentRole]:
            return thisParent == zoneParent

        return self._extentsAreOnSameLine(zone)

    def getWordAtOffset(self, charOffset):
        msg = f"FLAT REVIEW: Searching for word at offset {charOffset}"
        debug.printMessage(debug.LEVEL_INFO, msg, True)

        for word in self.words:
            tokens = ["FLAT REVIEW: Checking", word]
            debug.printTokens(debug.LEVEL_INFO, tokens, True)

            offset = word.getRelativeOffset(charOffset)
            if offset >= 0:
                return word, offset

        if self.length == charOffset and self.words:
            lastWord = self.words[-1]
            return lastWord, lastWord.length

        return None, -1

    def hasCaret(self):
        """Returns True if this Zone contains the caret."""

        return False

    def wordWithCaret(self):
        """Returns the Word and relative offset with the caret."""

        return None, -1

class TextZone(Zone):
    """A Zone whose purpose is to display text of an object."""

    def __init__(self, accessible, startOffset, string, x, y, width, height, role=None):
        super().__init__(accessible, string, x, y, width, height, role)

        self.startOffset = startOffset
        self.endOffset = self.startOffset + len(string)
        self._itext = None

    def __getattribute__(self, attr):
        """To ensure we update the content."""

        if attr not in ["words", "string"]:
            return super().__getattribute__(attr)

        string = AXText.get_substring(self.accessible, self.startOffset, self.endOffset)
        words = []
        for i, word in enumerate(re.finditer(self.WORDS_RE, string)):
            start, end = (pos + self.startOffset for pos in word.span())
            try:
                rect = Atspi.Text.get_range_extents(self.accessible, start, end, Atspi.CoordType.SCREEN)
                extents = rect.x, rect.y, rect.width, rect.height
            except Exception:
                extents = (self.x, self.y, self.width, self.height)
            words.append(Word(self, i, start, word.group(), *extents))

        self._string = string
        self._words = words
        return super().__getattribute__(attr)

    def hasCaret(self):
        """Returns True if this Zone contains the caret."""

        offset = AXText.get_caret_offset(self.accessible)
        if self.startOffset <= offset < self.endOffset:
            return True

        return self.endOffset == AXText.get_character_count(self.accessible)

    def wordWithCaret(self):
        """Returns the Word and relative offset with the caret."""

        if not self.hasCaret():
            return None, -1

        return self.getWordAtOffset(AXText.get_caret_offset(self.accessible))


class StateZone(Zone):
    """A Zone whose purpose is to display the state of an object."""

    def __init__(self, accessible, x, y, width, height, role=None):
        super().__init__(accessible, "", x, y, width, height, role)

    def __getattribute__(self, attr):
        """To ensure we update the state."""

        if attr not in ["string", "brailleString"]:
            return super().__getattribute__(attr)

        if attr == "string":
            generator = cthulhu_state.activeScript.speechGenerator
        else:
            generator = cthulhu_state.activeScript.brailleGenerator

        return self._getStateString(generator)

    def _getStateString(self, generator):
        result = generator.getStateIndicator(self.accessible, role=self.role)
        if result:
            return result[0]

        return ""


class ValueZone(Zone):
    """A Zone whose purpose is to display the value of an object."""

    def __init__(self, accessible, x, y, width, height, role=None):
        super().__init__(accessible, "", x, y, width, height, role)

    def __getattribute__(self, attr):
        """To ensure we update the value."""

        if attr not in ["string", "brailleString"]:
            return super().__getattribute__(attr)

        if attr == "string":
            generator = cthulhu_state.activeScript.speechGenerator
        else:
            generator = cthulhu_state.activeScript.brailleGenerator

        return self._getValueString(generator)

    def _getValueString(self, generator):
        # NOTE: This assembly is a stopgap until generators handle it directly.
        rolename = generator.getLocalizedRoleName(self.accessible)
        value = generator.getValue(self.accessible)
        if rolename and value:
            return f"{rolename} {value[0]}"

        return ""


class Line:
    """A Line is a single line across a window and is composed of Zones."""

    TEXT_BRAILLE_ROLES = (
        Atspi.Role.TEXT,
        Atspi.Role.PASSWORD_TEXT,
        Atspi.Role.TERMINAL,
    )
    TEXT_BRAILLE_FALLBACK_ROLES = (
        Atspi.Role.PARAGRAPH,
        Atspi.Role.HEADING,
        Atspi.Role.LINK,
    )

    def __init__(self,
                 index,
                 zones):
        """Creates a new Line, which is a horizontal region of text.

        Arguments:
        - index: the index of this Line in the window
        - zones: the Zones that make up this line
        """
        self.index = index
        self.zones = zones
        self.brailleRegions = None

    def __getattribute__(self, attr):
        if attr == "string":
            return " ".join([zone.string for zone in self.zones])

        if attr == "x":
            return min(zone.x for zone in self.zones)

        if attr == "y":
            return min(zone.y for zone in self.zones)

        if attr == "width":
            return sum(zone.width for zone in self.zones)

        if attr == "height":
            return max(zone.height for zone in self.zones)

        return super().__getattribute__(attr)

    def getBrailleRegions(self):
        # [[[WDW - We'll always compute the braille regions.  This
        # allows us to handle StateZone and ValueZone zones whose
        # states might be changing on us.]]]
        #
        if True or not self.brailleRegions:
            self.brailleRegions = []
            brailleOffset = 0
            for zone in self.zones:
                region = self._createBrailleRegion(zone)
                if len(self.brailleRegions):
                    pad = braille.Region(" ")
                    pad.brailleOffset = brailleOffset
                    self.brailleRegions.append(pad)
                    brailleOffset += 1

                zone.brailleRegion = region
                region.brailleOffset = brailleOffset
                self.brailleRegions.append(region)

                regionString = region.string
                brailleOffset += len(regionString)

            if not settings.disableBrailleEOL:
                if len(self.brailleRegions):
                    pad = braille.Region(" ")
                    pad.brailleOffset = brailleOffset
                    self.brailleRegions.append(pad)
                    brailleOffset += 1
                eol = braille.Region("$l")
                eol.brailleOffset = brailleOffset
                self.brailleRegions.append(eol)

        return self.brailleRegions

    def _createBrailleRegion(self, zone):
        if self._shouldUseTextBrailleRegion(zone):
            return braille.ReviewText(zone.accessible,
                                      zone.string,
                                      zone.startOffset,
                                      zone)

        try:
            brailleString = zone.brailleString
        except Exception:
            brailleString = zone.string

        return braille.ReviewComponent(zone.accessible,
                                       brailleString,
                                       0, # cursor offset
                                       zone)

    def _shouldUseTextBrailleRegion(self, zone):
        # The 'isinstance(zone, TextZone)' test is a sanity check
        # to handle problems with Java text. See Bug 435553.
        if not isinstance(zone, TextZone):
            return False

        role = AXObject.get_role(zone.accessible)
        if role in self.TEXT_BRAILLE_ROLES:
            return True

        # NOTE: Fallback roles kept for legacy cursor key routing support.
        return role in self.TEXT_BRAILLE_FALLBACK_ROLES

class Context:
    """Contains the flat review regions for the current top-level object."""

    ZONE   = 0
    CHAR   = 1
    WORD   = 2
    LINE   = 3 # includes all zones on same line
    WINDOW = 4

    WRAP_NONE       = 0
    WRAP_LINE       = 1 << 0
    WRAP_TOP_BOTTOM = 1 << 1
    WRAP_ALL        = (WRAP_LINE | WRAP_TOP_BOTTOM)

    CONTAINER_ROLES = (Atspi.Role.MENU,)
    VALUE_ZONE_ROLES = (Atspi.Role.SCROLL_BAR, Atspi.Role.SLIDER, Atspi.Role.PROGRESS_BAR)
    REDUNDANT_NAME_ROLES = (Atspi.Role.TABLE_ROW,)
    USELESS_NAME_ROLES = (Atspi.Role.TABLE_CELL, Atspi.Role.LABEL)
    STATE_ZONE_ROLES = (
        Atspi.Role.CHECK_BOX,
        Atspi.Role.CHECK_MENU_ITEM,
        Atspi.Role.RADIO_BUTTON,
        Atspi.Role.RADIO_MENU_ITEM,
    )

    def __init__(self, script, root=None):
        """Create a new Context for script."""

        self.script = script
        self.zones = []
        self.lines = []
        self.lineIndex = 0
        self.zoneIndex = 0
        self.wordIndex = 0
        self.charIndex = 0
        self.targetCharInfo = None
        self.focusZone = None
        self.container = None
        self.focusObj = cthulhu.getActiveModeAndObjectOfInterest()[1] or cthulhu_state.locusOfFocus
        self.topLevel = None
        self.bounds = Atspi.Rect()

        frame, dialog = script.utilities.frameAndDialog(self.focusObj)
        if root is not None:
            self.topLevel = root
            tokens = ["FLAT REVIEW: Restricting flat review to", root]
            debug.printTokens(debug.LEVEL_INFO, tokens, True)
        else:
            self.topLevel = dialog or frame
        tokens = ["FLAT REVIEW: Frame:", frame, "Dialog:", dialog, ". Top level:", self.topLevel]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)

        try:
            self.bounds = AXComponent.get_rect(self.topLevel)
        except Exception:
            tokens = ["ERROR: Exception getting extents of", self.topLevel]
            debug.printTokens(debug.LEVEL_INFO, tokens, True)

        def isContainer(x):
            return AXObject.get_role(x) in self.CONTAINER_ROLES

        container = AXObject.find_ancestor(self.focusObj, isContainer)
        if not container and isContainer(self.focusObj):
            container = self.focusObj

        self.container = container or self.topLevel

        self.zones, self.focusZone = self.getShowingZones(self.container)
        self.lines = self.clusterZonesByLine(self.zones)
        if not (self.lines and self.focusZone):
            return

        for i, line in enumerate(self.lines):
            if self.focusZone in line.zones:
                self.lineIndex = i
                self.zoneIndex = line.zones.index(self.focusZone)
                word, offset = self.focusZone.wordWithCaret()
                if word:
                    self.wordIndex = word.index
                    self.charIndex = offset
                break

        msg = (
            f"FLAT REVIEW: On line {self.lineIndex}, zone {self.zoneIndex} "
            f"word {self.wordIndex}, char {self.charIndex}"
        )
        debug.printMessage(debug.LEVEL_INFO, msg, True)

    def splitTextIntoZones(self, accessible, string, startOffset, cliprect):
        """Traverses the string, splitting it up into separate zones if the
        string contains the EMBEDDED_OBJECT_CHARACTER, which is used by apps
        such as Firefox to handle containment of things such as links in
        paragraphs.

        Arguments:
        - accessible: the accessible
        - string: a substring from the accessible's text specialization
        - startOffset: the starting character offset of the string
        - cliprect: the extents that the Zones must fit inside.

        Returns a list of Zones for the visible text.
        """

        cliprect = self._ensureRect(cliprect)
        zones = []
        substrings = [(*m.span(), m.group(0))  for m in EMBEDDED_OBJECT_RE.finditer(string)]
        substrings = [(start + startOffset, end + startOffset, text)
                      for (start, end, text) in substrings]
        for (start, end, substring) in substrings:
            try:
                rect = Atspi.Text.get_range_extents(accessible, start, end, Atspi.CoordType.SCREEN)
                extents = rect.x, rect.y, rect.width, rect.height
            except Exception:
                extents = (0, 0, 0, 0)
            if self.script.utilities.containsRegion(extents, cliprect):
                clipping = self.script.utilities.intersection(extents, cliprect)
                zones.append(TextZone(accessible, start, substring, *clipping))

        return zones

    def getZonesFromText(self, accessible, cliprect):
        """Gets a list of Zones from an object that implements the
        AccessibleText specialization.

        Arguments:
        - accessible: the accessible
        - cliprect: the extents that the Zones must fit inside.

        Returns a list of Zones.
        """

        cliprect = self._ensureRect(cliprect)
        if not self.script.utilities.hasPresentableText(accessible):
            return []

        zones = self._getSingleLineEditableZones(accessible)
        if zones:
            return zones

        upperMax = lowerMax = AXText.get_character_count(accessible)
        upperMid = lowerMid = int(upperMax / 2)
        upperMin = lowerMin = 0
        oldMid = 0

        # performing binary search to locate first line inside clipped area
        while oldMid != upperMid:
            oldMid = upperMid
            rect = Atspi.Text.get_range_extents(accessible, upperMid, upperMid + 1,
                                                Atspi.CoordType.SCREEN)
            x, y, width, height = rect.x, rect.y, rect.width, rect.height
            if y > cliprect.y:
                upperMax = upperMid
            else:
                upperMin = upperMid
            upperMid = int((upperMax - upperMin) / 2) + upperMin

        # performing binary search to locate last line inside clipped area
        oldMid = 0
        limit = cliprect.y+cliprect.height
        while oldMid != lowerMid:
            oldMid = lowerMid
            rect = Atspi.Text.get_range_extents(accessible, lowerMid, lowerMid + 1,
                                                Atspi.CoordType.SCREEN)
            x, y, width, height = rect.x, rect.y, rect.width, rect.height
            if y > limit:
                lowerMax = lowerMid
            else:
                lowerMin = lowerMid
            lowerMid = int((lowerMax - lowerMin) / 2) + lowerMin

        msg = "FLAT REVIEW: Getting lines for %s offsets %i-%i" % (accessible, upperMin, lowerMax)
        debug.printMessage(debug.LEVEL_INFO, msg, True)

        lines = self.script.utilities.getLinesForRange(accessible, upperMin, lowerMax)
        tokens = ["FLAT REVIEW:", len(lines), "lines found for", accessible]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)

        for string, startOffset, endOffset in lines:
            zones.extend(self.splitTextIntoZones(accessible, string, startOffset, cliprect))

        return zones

    def _getSingleLineEditableZones(self, accessible):
        # NOTE: Temporary workaround while text handling is refined.
        if not (AXObject.supports_editable_text(accessible)
                and AXUtilities.is_single_line(accessible)):
            return []

        rect = AXComponent.get_rect(accessible)
        return [TextZone(accessible, 0, AXText.get_substring(accessible, 0, -1),
                         rect.x, rect.y, rect.width, rect.height)]

    def _insertStateZone(self, zones, accessible, extents):
        """If the accessible presents non-textual state, such as a
        checkbox or radio button, insert a StateZone representing
        that state."""

        # NOTE: Heuristic state-zone placement until handled by scripts.

        extents = self._ensureRect(extents)
        indicatorExtents = [extents.x, extents.y, 1, extents.height]
        role = AXObject.get_role(accessible)
        if role == Atspi.Role.TOGGLE_BUTTON:
            zone = StateZone(accessible, *indicatorExtents, role=role)
            if zone:
                zones.insert(0, zone)
            return

        if role == Atspi.Role.TABLE_CELL \
           and self.script.utilities.hasMeaningfulToggleAction(accessible):
            role = Atspi.Role.CHECK_BOX

        if role not in self.STATE_ZONE_ROLES:
            return

        zone = None
        stateOnLeft = True

        if len(zones) == 1 and isinstance(zones[0], TextZone):
            textZone = zones[0]
            textToLeftEdge = textZone.x - extents.x
            textToRightEdge = (extents.x + extents.width) - (textZone.x + textZone.width)
            stateOnLeft = textToLeftEdge > 20
            if stateOnLeft:
                indicatorExtents[2] = textToLeftEdge
            else:
                indicatorExtents[0] = textZone.x + textZone.width
                indicatorExtents[2] = textToRightEdge

        zone = StateZone(accessible, *indicatorExtents, role=role)
        if zone:
            if stateOnLeft:
                zones.insert(0, zone)
            else:
                zones.append(zone)

    def getZonesFromAccessible(self, accessible, cliprect):
        """Returns a list of Zones for the given accessible."""

        cliprect = self._ensureRect(cliprect)
        try:
            if AXObject.supports_component(accessible):
                rect = Atspi.Component.get_extents(accessible, Atspi.CoordType.SCREEN)
                extents = rect.x, rect.y, rect.width, rect.height
            else:
                return []
        except Exception:
            return []

        try:
            role = AXObject.get_role(accessible)
        except Exception:
            return []

        zones = self.getZonesFromText(accessible, cliprect)
        if not zones and role in self.VALUE_ZONE_ROLES:
            zones.append(ValueZone(accessible, *extents))
        elif not zones:
            string = ""
            if role not in self.REDUNDANT_NAME_ROLES:
                string = self.script.speechGenerator.getName(accessible, inFlatReview=True)

            if not string and role not in self.USELESS_NAME_ROLES:
                string = self.script.speechGenerator.getRoleName(accessible)
            if string:
                zones.append(Zone(accessible, string, *extents))

        self._insertStateZone(zones, accessible, extents)

        return zones

    def _isOrIsIn(self, child, parent):
        if not (child and parent):
            return False

        if child == parent:
            return True

        return AXObject.find_ancestor(child, lambda x: x == parent)

    @staticmethod
    def _ensureRect(rect):
        if rect is None:
            return Atspi.Rect()
        if hasattr(rect, "x") and hasattr(rect, "y") \
           and hasattr(rect, "width") and hasattr(rect, "height"):
            return rect

        try:
            x, y, width, height = rect
        except Exception:
            return Atspi.Rect()

        newRect = Atspi.Rect()
        newRect.x = x
        newRect.y = y
        newRect.width = width
        newRect.height = height
        return newRect
    def setCurrentToZoneWithObject(self, obj):
        """Attempts to set the current zone to obj, if obj is in the current context."""

        tokens = ["FLAT REVIEW: Current", self.getCurrentAccessible(),
                  f"line: {self.lineIndex}, zone: {self.zoneIndex},",
                  f"word: {self.wordIndex}, char: {self.charIndex})"]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)

        zone = self._findZoneWithObject(obj)
        tokens = ["FLAT REVIEW: Zone with", obj, "is", zone]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)
        if zone is None:
            return False

        for i, line in enumerate(self.lines):
            if zone in line.zones:
                self.lineIndex = i
                self.zoneIndex = line.zones.index(zone)
                word, offset = zone.wordWithCaret()
                if word:
                    self.wordIndex = word.index
                    self.charIndex = offset
                msg = "FLAT REVIEW: Updated current zone."
                debug.printMessage(debug.LEVEL_INFO, msg, True)
                break
        else:
            msg = "FLAT REVIEW: Failed to update current zone."
            debug.printMessage(debug.LEVEL_INFO, msg, True)
            return False

        tokens = ["FLAT REVIEW: Updated", self.getCurrentAccessible(),
                  f"line: {self.lineIndex}, zone: {self.zoneIndex},",
                  f"word: {self.wordIndex}, char: {self.charIndex})"]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)
        return True

    def _findZoneWithObject(self, obj):
        """Returns the existing zone which contains obj."""

        if obj is None:
            return None

        for zone in self.zones:
            if zone.accessible == obj:
                return zone

            # Some items get pruned from the flat review tree. For instance, a
            # tree item which has a descendant section whose text is the displayed
            # text of the tree item, that section will be in the flat review tree
            # but the ancestor item might not.
            if AXObject.is_ancestor(zone.accessible, obj):
                tokens = ["FLAT REVIEW:", zone.accessible, "is ancestor of zone accessible", obj]
                debug.printTokens(debug.LEVEL_INFO, tokens, True)
                return zone

        return None

    def getShowingZones(self, root, boundingbox=None):
        """Returns an unsorted list of all the zones under root and the focusZone."""

        if boundingbox is None:
            boundingbox = self.bounds
        boundingbox = self._ensureRect(boundingbox)

        objs = self.script.utilities.getOnScreenObjects(root, boundingbox)
        tokens = ["FLAT REVIEW:", len(objs), "on-screen objects found for", root]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)

        allZones, focusZone = [], None
        for o in objs:
            zones = self.getZonesFromAccessible(o, boundingbox)
            if not zones:
                descendant = self.script.utilities.realActiveDescendant(o)
                if descendant:
                    zones = self.getZonesFromAccessible(descendant, boundingbox)

            if not zones:
                continue

            allZones.extend(zones)
            if not focusZone and zones and self.focusObj and self._isOrIsIn(o, self.focusObj):
                zones = list(filter(lambda z: z.hasCaret(), zones)) or zones
                focusZone = zones[0]

        tokens = ["FLAT REVIEW:", len(allZones), "zones found for", root]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)
        return allZones, focusZone

    def clusterZonesByLine(self, zones):
        """Returns a sorted list of Line clusters containing sorted Zones."""

        if not zones:
            return []

        lineClusters = []
        sortedZones = sorted(zones, key=lambda z: z.y)
        newCluster = [sortedZones.pop(0)]
        for zone in sortedZones:
            if zone.onSameLine(newCluster[-1]):
                newCluster.append(zone)
            else:
                lineClusters.append(sorted(newCluster, key=lambda z: z.x))
                newCluster = [zone]

        if newCluster:
            lineClusters.append(sorted(newCluster, key=lambda z: z.x))

        lines = []
        for lineIndex, lineCluster in enumerate(lineClusters):
            lines.append(Line(lineIndex, lineCluster))
            for zoneIndex, zone in enumerate(lineCluster):
                zone.line = lines[lineIndex]
                zone.index = zoneIndex

        tokens = ["FLAT REVIEW: Zones clustered into", len(lines), "lines"]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)
        return lines

    def getCurrent(self, flatReviewType=ZONE):
        """Returns the current string, offset, and extent information."""

        # NOTE: Legacy name; prefer getCurrentItem() for clarity.

        zone = self._getCurrentZone()
        if not zone:
            return None, -1, -1, -1, -1

        current = zone
        if flatReviewType == Context.LINE:
            current = zone.line
        elif flatReviewType != Context.ZONE and zone.words:
            current = zone.words[self.wordIndex]
            if flatReviewType == Context.CHAR and current.chars:
                try:
                    current = current.chars[self.charIndex]
                except Exception:
                    return None, -1, -1, -1, -1

        return current.string, current.x, current.y, current.width, current.height

    def getCurrentItem(self, flatReviewType=ZONE):
        return self.getCurrent(flatReviewType)

    def setCurrent(self, lineIndex, zoneIndex, wordIndex, charIndex):
        """Sets the current character of interest.

        Arguments:
        - lineIndex: index into lines
        - zoneIndex: index into lines[lineIndex].zones
        - wordIndex: index into lines[lineIndex].zones[zoneIndex].words
        - charIndex: index lines[lineIndex].zones[zoneIndex].words[wordIndex].chars
        """

        self.lineIndex = lineIndex
        self.zoneIndex = zoneIndex
        self.wordIndex = wordIndex
        self.charIndex = charIndex
        self.targetCharInfo = self.getCurrent(Context.CHAR)

    def _getClickPoint(self):
        string, x, y, width, height = self.getCurrent(Context.CHAR)
        if (x < 0 and y < 0) or (width <= 0 and height <=0):
            return -1, -1

        # Click left of center to position the caret there.
        x = int(max(x, x + (width / 2) - 1))
        y = int(y + height / 2)

        return x, y

    def routeToCurrent(self):
        """Routes the mouse pointer to the current accessible."""

        x, y = self._getClickPoint()
        if x < 0 or y < 0:
            return False

        return AXEventSynthesizer.route_to_point(x, y)

    def clickCurrent(self, button=1):
        """Performs a mouse click on the current accessible."""

        x, y = self._getClickPoint()
        if x >= 0 and y >= 0 and AXEventSynthesizer.click_point(x, y, button):
            return True

        if AXEventSynthesizer.click_object(self.getCurrentAccessible(), button):
            return True

        return False

    def _getCurrentZone(self):
        if not (self.lines and 0 <= self.lineIndex < len(self.lines)):
            return None

        line = self.lines[self.lineIndex]
        if not (line and 0 <= self.zoneIndex < len(line.zones)):
            return None

        return line.zones[self.zoneIndex]

    def getCurrentAccessible(self):
        """Returns the current accessible."""

        zone = self._getCurrentZone()
        if not zone:
            return None

        return zone.accessible

    def getCurrentBrailleRegions(self):
        """Gets the braille for the entire current line.

        Returns [regions, regionWithFocus]
        """

        if (not self.lines) \
           or (not self.lines[self.lineIndex].zones):
            return [None, None]

        regionWithFocus = None
        line = self.lines[self.lineIndex]
        regions = line.getBrailleRegions()

        # Now find the current region and the current character offset
        # into that region.
        #
        for zone in line.zones:
            if zone.index == self.zoneIndex:
                regionWithFocus = zone.brailleRegion
                regionWithFocus.cursorOffset = 0
                if zone.words:
                    regionWithFocus.cursorOffset += zone.words[0].startOffset - zone.startOffset
                    for wordIndex in range(0, self.wordIndex):
                        regionWithFocus.cursorOffset += \
                            len(zone.words[wordIndex].string)
                regionWithFocus.cursorOffset += self.charIndex
                regionWithFocus.repositionCursor()
                break

        return [regions, regionWithFocus]

    def goBegin(self, flatReviewType=WINDOW):
        """Moves this context's locus of interest to the first char
        of the first relevant zone.

        Arguments:
        - flatReviewType: one of ZONE, LINE or WINDOW

        Returns True if the locus of interest actually changed.
        """

        if (flatReviewType == Context.LINE) or (flatReviewType == Context.ZONE):
            lineIndex = self.lineIndex
        elif flatReviewType == Context.WINDOW:
            lineIndex = 0
        else:
            raise Exception("Invalid type: %d" % flatReviewType)

        if flatReviewType == Context.ZONE:
            zoneIndex = self.zoneIndex
        else:
            zoneIndex = 0

        wordIndex = 0
        charIndex = 0

        moved = (self.lineIndex != lineIndex) \
                or (self.zoneIndex != zoneIndex) \
                or (self.wordIndex != wordIndex) \
                or (self.charIndex != charIndex) \

        if moved:
            self.lineIndex = lineIndex
            self.zoneIndex = zoneIndex
            self.wordIndex = wordIndex
            self.charIndex = charIndex
            self.targetCharInfo = self.getCurrent(Context.CHAR)

        return moved

    def goEnd(self, flatReviewType=WINDOW):
        """Moves this context's locus of interest to the last char
        of the last relevant zone.

        Arguments:
        - flatReviewType: one of ZONE, LINE, or WINDOW

        Returns True if the locus of interest actually changed.
        """

        if (flatReviewType == Context.LINE) or (flatReviewType == Context.ZONE):
            lineIndex = self.lineIndex
        elif flatReviewType == Context.WINDOW:
            lineIndex  = len(self.lines) - 1
        else:
            raise Exception("Invalid type: %d" % flatReviewType)

        if flatReviewType == Context.ZONE:
            zoneIndex = self.zoneIndex
        else:
            zoneIndex = len(self.lines[lineIndex].zones) - 1

        zone = self.lines[lineIndex].zones[zoneIndex]
        if zone.words:
            wordIndex = len(zone.words) - 1
            chars = zone.words[wordIndex].chars
            if chars:
                charIndex = len(chars) - 1
            else:
                charIndex = 0
        else:
            wordIndex = 0
            charIndex = 0

        moved = (self.lineIndex != lineIndex) \
                or (self.zoneIndex != zoneIndex) \
                or (self.wordIndex != wordIndex) \
                or (self.charIndex != charIndex) \

        if moved:
            self.lineIndex = lineIndex
            self.zoneIndex = zoneIndex
            self.wordIndex = wordIndex
            self.charIndex = charIndex
            self.targetCharInfo = self.getCurrent(Context.CHAR)

        return moved

    def goPrevious(self, flatReviewType=ZONE, 
                   wrap=WRAP_ALL, omitWhitespace=True):
        """Moves this context's locus of interest to the first char
        of the previous type.

        Arguments:
        - flatReviewType: one of ZONE, CHAR, WORD, LINE
        - wrap: if True, will cross boundaries, including top and
                bottom; if False, will stop on boundaries.

        Returns True if the locus of interest actually changed.
        """

        if not self.lines:
            debug.printMessage(debug.LEVEL_INFO, 'goPrevious(): no lines in context')
            return False

        moved = False

        if flatReviewType == Context.ZONE:
            if self.zoneIndex > 0:
                self.zoneIndex -= 1
                self.wordIndex = 0
                self.charIndex = 0
                moved = True
            elif wrap & Context.WRAP_LINE:
                if self.lineIndex > 0:
                    self.lineIndex -= 1
                    self.zoneIndex = len(self.lines[self.lineIndex].zones) - 1
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
                elif wrap & Context.WRAP_TOP_BOTTOM:
                    self.lineIndex = len(self.lines) - 1
                    self.zoneIndex = len(self.lines[self.lineIndex].zones) - 1
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
        elif flatReviewType == Context.CHAR:
            if self.charIndex > 0:
                self.charIndex -= 1
                moved = True
            else:
                moved = self.goPrevious(Context.WORD, wrap, False)
                if moved:
                    zone = self.lines[self.lineIndex].zones[self.zoneIndex]
                    if zone.words:
                        chars = zone.words[self.wordIndex].chars
                        if chars:
                            self.charIndex = len(chars) - 1
        elif flatReviewType == Context.WORD:
            zone = self.lines[self.lineIndex].zones[self.zoneIndex]
            accessible = zone.accessible
            lineIndex = self.lineIndex
            zoneIndex = self.zoneIndex
            wordIndex = self.wordIndex
            charIndex = self.charIndex

            if self.wordIndex > 0:
                self.wordIndex -= 1
                self.charIndex = 0
                moved = True
            else:
                moved = self.goPrevious(Context.ZONE, wrap)
                if moved:
                    zone = self.lines[self.lineIndex].zones[self.zoneIndex]
                    if zone.words:
                        self.wordIndex = len(zone.words) - 1

            # If we landed on a whitespace word or something with no words,
            # we might need to move some more.
            #
            zone = self.lines[self.lineIndex].zones[self.zoneIndex]
            if omitWhitespace \
               and moved \
               and ((len(zone.string) == 0) \
                    or (len(zone.words) \
                        and zone.words[self.wordIndex].string.isspace())):

                hasMoreText = False
                if self.lineIndex > 0 and isinstance(zone, TextZone):
                    prevZone = self.lines[self.lineIndex - 1].zones[-1]
                    if prevZone.accessible == zone.accessible:
                        hasMoreText = True

                # If we're on whitespace in the same zone, then let's
                # try to move on.  If not, we've definitely moved
                # across accessibles.  If that's the case, let's try
                # to find the first 'real' word in the accessible.
                # If we cannot, then we're just stuck on an accessible
                # with no words and we should do our best to announce
                # this to the user (e.g., "whitespace" or "blank").
                #
                if zone.accessible == accessible or hasMoreText:
                    moved = self.goPrevious(Context.WORD, wrap)
                else:
                    wordIndex = self.wordIndex - 1
                    while wordIndex >= 0:
                        if (not zone.words[wordIndex].string) \
                            or not len(zone.words[wordIndex].string) \
                            or zone.words[wordIndex].string.isspace():
                            wordIndex -= 1
                        else:
                            break
                    if wordIndex >= 0:
                        self.wordIndex = wordIndex

            if not moved:
                self.lineIndex = lineIndex
                self.zoneIndex = zoneIndex
                self.wordIndex = wordIndex
                self.charIndex = charIndex

        elif flatReviewType == Context.LINE:
            if wrap & Context.WRAP_LINE:
                if self.lineIndex > 0:
                    self.lineIndex -= 1
                    self.zoneIndex = 0
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
                elif (wrap & Context.WRAP_TOP_BOTTOM) \
                     and (len(self.lines) != 1):
                    self.lineIndex = len(self.lines) - 1
                    self.zoneIndex = 0
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
        else:
            raise Exception("Invalid type: %d" % flatReviewType)

        if moved and (flatReviewType != Context.LINE):
            self.targetCharInfo = self.getCurrent(Context.CHAR)

        return moved

    def goNext(self, flatReviewType=ZONE, wrap=WRAP_ALL, omitWhitespace=True):
        """Moves this context's locus of interest to first char of
        the next type.

        Arguments:
        - flatReviewType: one of ZONE, CHAR, WORD, LINE
        - wrap: if True, will cross boundaries, including top and
                bottom; if False, will stop on boundaries.
        """

        if not self.lines:
            debug.printMessage(debug.LEVEL_INFO, 'goNext(): no lines in context')
            return False

        moved = False

        if flatReviewType == Context.ZONE:
            if self.zoneIndex < (len(self.lines[self.lineIndex].zones) - 1):
                self.zoneIndex += 1
                self.wordIndex = 0
                self.charIndex = 0
                moved = True
            elif wrap & Context.WRAP_LINE:
                if self.lineIndex < (len(self.lines) - 1):
                    self.lineIndex += 1
                    self.zoneIndex  = 0
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
                    braille.clear()
                elif wrap & Context.WRAP_TOP_BOTTOM:
                    self.lineIndex  = 0
                    self.zoneIndex  = 0
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
                    braille.clear()
        elif flatReviewType == Context.CHAR:
            zone = self.lines[self.lineIndex].zones[self.zoneIndex]
            if zone.words:
                chars = zone.words[self.wordIndex].chars
                if chars:
                    if self.charIndex < (len(chars) - 1):
                        self.charIndex += 1
                        moved = True
                    else:
                        moved = self.goNext(Context.WORD, wrap, False)
                else:
                    moved = self.goNext(Context.WORD, wrap)
            else:
                moved = self.goNext(Context.ZONE, wrap)
        elif flatReviewType == Context.WORD:
            zone = self.lines[self.lineIndex].zones[self.zoneIndex]
            accessible = zone.accessible
            lineIndex = self.lineIndex
            zoneIndex = self.zoneIndex
            wordIndex = self.wordIndex
            charIndex = self.charIndex

            if zone.words:
                if self.wordIndex < (len(zone.words) - 1):
                    self.wordIndex += 1
                    self.charIndex = 0
                    moved = True
                else:
                    moved = self.goNext(Context.ZONE, wrap)
            else:
                moved = self.goNext(Context.ZONE, wrap)

            # If we landed on a whitespace word or something with no words,
            # we might need to move some more.
            #
            zone = self.lines[self.lineIndex].zones[self.zoneIndex]
            if omitWhitespace \
               and moved \
               and ((len(zone.string) == 0) \
                    or (len(zone.words) \
                        and zone.words[self.wordIndex].string.isspace())):

                # If we're on whitespace in the same zone, then let's
                # try to move on.  If not, we've definitely moved
                # across accessibles.  If that's the case, let's try
                # to find the first 'real' word in the accessible.
                # If we cannot, then we're just stuck on an accessible
                # with no words and we should do our best to announce
                # this to the user (e.g., "whitespace" or "blank").
                #
                if zone.accessible == accessible:
                    moved = self.goNext(Context.WORD, wrap)
                else:
                    wordIndex = self.wordIndex + 1
                    while wordIndex < len(zone.words):
                        if (not zone.words[wordIndex].string) \
                            or not len(zone.words[wordIndex].string) \
                            or zone.words[wordIndex].string.isspace():
                            wordIndex += 1
                        else:
                            break
                    if wordIndex < len(zone.words):
                        self.wordIndex = wordIndex

            if not moved:
                self.lineIndex = lineIndex
                self.zoneIndex = zoneIndex
                self.wordIndex = wordIndex
                self.charIndex = charIndex

        elif flatReviewType == Context.LINE:
            if wrap & Context.WRAP_LINE:
                if self.lineIndex < (len(self.lines) - 1):
                    self.lineIndex += 1
                    self.zoneIndex = 0
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
                elif (wrap & Context.WRAP_TOP_BOTTOM) \
                     and (self.lineIndex != 0):
                    self.lineIndex = 0
                    self.zoneIndex = 0
                    self.wordIndex = 0
                    self.charIndex = 0
                    moved = True
        else:
            raise Exception("Invalid type: %d" % flatReviewType)

        if moved and (flatReviewType != Context.LINE):
            self.targetCharInfo = self.getCurrent(Context.CHAR)

        return moved

    def goAbove(self, flatReviewType=LINE, wrap=WRAP_ALL):
        """Moves this context's locus of interest to first char
        of the type that's closest to and above the current locus of
        interest.

        Arguments:
        - flatReviewType: LINE
        - wrap: if True, will cross top/bottom boundaries; if False, will
                stop on top/bottom boundaries.

        Returns: [string, startOffset, endOffset, x, y, width, height]
        """

        moved = False
        if flatReviewType == Context.CHAR:
            # We want to shoot for the closest character, which we've
            # saved away as self.targetCharInfo, which is the list
            # [string, x, y, width, height].
            #
            if not self.targetCharInfo:
                self.targetCharInfo = self.getCurrent(Context.CHAR)
            target = self.targetCharInfo

            [string, x, y, width, height] = target
            middleTargetX = x + (width / 2)

            moved = self.goPrevious(Context.LINE, wrap)
            if moved:
                while True:
                    [string, bx, by, bwidth, bheight] = \
                             self.getCurrent(Context.CHAR)
                    if (bx + width) >= middleTargetX:
                        break
                    elif not self.goNext(Context.CHAR, Context.WRAP_NONE):
                        break

            # Moving around might have reset the current targetCharInfo,
            # so we reset it to our saved value.
            #
            self.targetCharInfo = target
        elif flatReviewType == Context.LINE:
            return self.goPrevious(flatReviewType, wrap)
        else:
            raise Exception("Invalid type: %d" % flatReviewType)

        return moved

    def goBelow(self, flatReviewType=LINE, wrap=WRAP_ALL):
        """Moves this context's locus of interest to the first
        char of the type that's closest to and below the current
        locus of interest.

        Arguments:
        - flatReviewType: one of WORD, LINE
        - wrap: if True, will cross top/bottom boundaries; if False, will
                stop on top/bottom boundaries.

        Returns: [string, startOffset, endOffset, x, y, width, height]
        """

        moved = False
        if flatReviewType == Context.CHAR:
            # We want to shoot for the closest character, which we've
            # saved away as self.targetCharInfo, which is the list
            # [string, x, y, width, height].
            #
            if not self.targetCharInfo:
                self.targetCharInfo = self.getCurrent(Context.CHAR)
            target = self.targetCharInfo

            [string, x, y, width, height] = target
            middleTargetX = x + (width / 2)

            moved = self.goNext(Context.LINE, wrap)
            if moved:
                while True:
                    [string, bx, by, bwidth, bheight] = \
                             self.getCurrent(Context.CHAR)
                    if (bx + width) >= middleTargetX:
                        break
                    elif not self.goNext(Context.CHAR, Context.WRAP_NONE):
                        break

            # Moving around might have reset the current targetCharInfo,
            # so we reset it to our saved value.
            #
            self.targetCharInfo = target
        elif flatReviewType == Context.LINE:
            moved = self.goNext(flatReviewType, wrap)
        else:
            raise Exception("Invalid type: %d" % flatReviewType)

        return moved
EMBEDDED_OBJECT_RE = re.compile(r"[^\ufffc]+")
