#!/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

"""Thunderbird-specific utility overrides."""

__id__ = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2010 Joanmarie Diggs."
__license__   = "LGPL"

import cthulhu.debug as debug
from cthulhu.ax_object import AXObject
from cthulhu.ax_utilities import AXUtilities
from cthulhu.scripts.toolkits.Gecko.script_utilities import Utilities as GeckoUtilities


class Utilities(GeckoUtilities):

    def __init__(self, script):
        super().__init__(script)

    def getDocumentForObject(self, obj):
        document = super().getDocumentForObject(obj)
        if document:
            return document

        documentFrame = self._messageDocumentFrameFor(obj)
        if not documentFrame:
            return None

        tokens = ["THUNDERBIRD: Treating document frame as document:", documentFrame]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)
        return documentFrame

    def getTopLevelDocumentForObject(self, obj):
        document = super().getTopLevelDocumentForObject(obj)
        if document:
            return document

        documentFrame = self._messageDocumentFrameFor(obj)
        if not documentFrame:
            return None

        tokens = ["THUNDERBIRD: Treating document frame as top-level document:", documentFrame]
        debug.printTokens(debug.LEVEL_INFO, tokens, True)
        return documentFrame

    def _messageDocumentFrameFor(self, obj):
        if not obj:
            return None

        if AXUtilities.is_document_frame(obj) and self._isMessageDocumentFrame(obj):
            return obj

        documentFrame = AXObject.find_ancestor(obj, AXUtilities.is_document_frame)
        if documentFrame and self._isMessageDocumentFrame(documentFrame):
            return documentFrame

        return None

    def _isMessageDocumentFrame(self, obj):
        if not obj or not AXUtilities.is_document_frame(obj):
            return False

        uri = self.documentFrameURI(obj)
        if uri:
            for prefix in ("about:message", "mailbox:", "imap:", "imaps:",
                           "news:", "nntp:", "pop:", "pop3:",
                           "smtp:", "smtps:"):
                if uri.startswith(prefix):
                    return True

        name = (AXObject.get_name(obj) or "").lower()
        return "message" in name
