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

"""Module for date and time presentation"""

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

import time
from typing import Optional, Dict, Callable, Any

from . import cthulhu  # Need access to cthulhuApp
from . import cmdnames
from . import input_event
from . import keybindings
from . import settings_manager

_settingsManager = None  # Removed - use cthulhu.cthulhuApp.settingsManager

class DateAndTimePresenter:
    """Provides commands to present the date and time."""

    def __init__(self) -> None:
        self._handlers: Dict[str, input_event.InputEventHandler] = self._setup_handlers()
        self._bindings: keybindings.KeyBindings = self._setup_bindings()

    def get_bindings(self) -> keybindings.KeyBindings:
        """Returns the date-and-time-presenter keybindings."""

        return self._bindings

    def get_handlers(self) -> Dict[str, input_event.InputEventHandler]:
        """Returns the date-and-time-presenter handlers."""

        return self._handlers

    def _setup_handlers(self) -> Dict[str, input_event.InputEventHandler]:
        """Sets up and returns the date-and-time-presenter input event handlers."""

        handlers = {}

        handlers["presentTimeHandler"] = \
            input_event.InputEventHandler(
                self.present_time,
                cmdnames.PRESENT_CURRENT_TIME)

        handlers["presentDateHandler"] = \
            input_event.InputEventHandler(
                self.present_date,
                cmdnames.PRESENT_CURRENT_DATE)

        return handlers

    def _setup_bindings(self) -> keybindings.KeyBindings:
        """Sets up and returns the date-and-time-presenter key bindings."""

        bindings = keybindings.KeyBindings()

        bindings.add(
            keybindings.KeyBinding(
                "t",
                keybindings.defaultModifierMask,
                keybindings.CTHULHU_MODIFIER_MASK,
                self._handlers.get("presentTimeHandler"),
                1))

        bindings.add(
            keybindings.KeyBinding(
                "t",
                keybindings.defaultModifierMask,
                keybindings.CTHULHU_MODIFIER_MASK,
                self._handlers.get("presentDateHandler"),
                2))

        return bindings

    def present_time(self, script: Any, event: Optional[Any] = None) -> bool:
        """Presents the current time."""

        format = cthulhu.cthulhuApp.settingsManager.getSetting('presentTimeFormat')
        script.presentMessage(time.strftime(format, time.localtime()))
        return True

    def present_date(self, script: Any, event: Optional[Any] = None) -> bool:
        """Presents the current date."""

        format = cthulhu.cthulhuApp.settingsManager.getSetting('presentDateFormat')
        script.presentMessage(time.strftime(format, time.localtime()))
        return True


_presenter: Optional[DateAndTimePresenter] = None

def getPresenter() -> DateAndTimePresenter:
    global _presenter
    if _presenter is None:
        _presenter = DateAndTimePresenter()
    return _presenter
