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

import cthulhu.script_utilities as script_utilities
from cthulhu.ax_object import AXObject
from cthulhu.ax_value import AXValue
from cthulhu.ax_utilities import AXUtilities


class Utilities(script_utilities.Utilities):

    def __init__(self, script):
        """Creates an instance of the Utilities class.

        Arguments:
        - script: the script with which this instance is associated.
        """

        script_utilities.Utilities.__init__(self, script)

    def _formatDuration(self, s):
        seconds = '%02d' % (s%60)
        minutes = '%02d' % (s/60)
        hours = '%02d' % (s/3600)
        duration = [minutes, seconds]
        if hours != '00':
            duration.insert(0, hours)

        return ':'.join(duration)

    def isSeekSlider(self, obj):
        return AXObject.find_ancestor(obj, AXUtilities.is_tool_bar) is not None

    def textForValue(self, obj):
        if not self.isSeekSlider(obj):
            return script_utilities.Utilities.textForValue(self, obj)

        if not AXObject.supports_value(obj):
            return script_utilities.Utilities.textForValue(self, obj)

        return self._formatDuration(int(AXValue.get_current_value(obj)) / 1000)
