#!/usr/bin/env python3
#
# Copyright (c) 2025 Stormux
#
# 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.

"""Hello World plugin for Cthulhu."""

import logging
from cthulhu.plugin import Plugin, cthulhu_hookimpl

logger = logging.getLogger(__name__)

class HelloWorld(Plugin):
    """Hello World plugin."""

    def __init__(self, *args, **kwargs):
        """Initialize the plugin."""
        super().__init__(*args, **kwargs)
        print("Plugin hello world initialized.")
        logger.info("HelloWorld plugin initialized")

    @cthulhu_hookimpl
    def activate(self, plugin=None):
        """Activate the plugin."""
        # Skip if this activation call isn't for us
        if plugin is not None and plugin is not self:
            return

        try:
            logger.info("Activating Hello World plugin")

            # Register our keyboard shortcut
            self.registerGestureByString(
                self.speakTest, 
                "hello world", 
                "kb:cthulhu+z",
                learnModeEnabled=True
            )
        except Exception as e:
            logger.error(f"Error activating Hello World plugin: {e}")

    @cthulhu_hookimpl
    def deactivate(self, plugin=None):
        """Deactivate the plugin."""
        # Skip if this deactivation call isn't for us
        if plugin is not None and plugin is not self:
            return

        try:
            logger.info("Deactivating Hello World plugin")
        except Exception as e:
            logger.error(f"Error deactivating Hello World plugin: {e}")

    def speakTest(self, script=None, inputEvent=None):
        """Speak a test message."""
        try:
            if self.app:
                self.app.getDynamicApiManager().getAPI('CthulhuState').activeScript.presentMessage(
                    'hello world', 
                    resetStyles=False
                )

            return True
        except Exception as e:
            logger.error(f"Error in speakTest: {e}")
            return False
