
import os

from typing import List, Dict, Any

import logging



# Define a placeholder class for when GUI is not available

class PCInteractionTool(object):

    def __init__(self, user_permission_required: bool = True, session_id: str = None):

        self.name = "pc_interaction_tool"

        self.description = "Tool for interacting with the PC (GUI). Not available in this environment."

        logging.warning("PCInteractionTool is not available in this environment (e.g., non-GUI terminal).")



    def get_tools(self) -> List:

        return []



# Check if the DISPLAY environment variable is set, which indicates a GUI environment

if os.environ.get("DISPLAY"):

    try:

        from . import BaseTool

        from ...utils.tool_utils import authenticate_and_execute

        import pyautogui

        import time



        # The real implementation of the tool

        class PCInteractionTool(BaseTool):

            def __init__(self, user_permission_required: bool = True, session_id: str = None):

                super().__init__(user_permission_required, session_id)

                self.name = "pc_interaction_tool"

                self.description = "A tool to interact with the PC, including mouse and keyboard control."

                self.tools = {

                    "move_mouse_to": self.move_mouse_to,

                    "click": self.click,

                    "type_text": self.type_text,

                    "scroll": self.scroll,

                    "take_screenshot": self.take_screenshot

                }



            def get_tools(self) -> List:

                return [

                    {

                        "type": "function",

                        "function": {

                            "name": "move_mouse_to",

                            "description": "Moves the mouse cursor to a specified position on the screen.",

                            "parameters": {

                                "type": "object",

                                "properties": {

                                    "x": {"type": "integer", "description": "The x-coordinate to move to."},

                                    "y": {"type": "integer", "description": "The y-coordinate to move to."},

                                    "duration": {"type": "number", "description": "The duration of the mouse movement in seconds."}

                                },

                                "required": ["x", "y"]

                            }

                        }

                    },

                    {

                        "type": "function",

                        "function": {

                            "name": "click",

                            "description": "Performs a mouse click.",

                            "parameters": {

                                "type": "object",

                                "properties": {

                                    "button": {"type": "string", "description": "The mouse button to click ('left', 'right', 'middle').", "default": "left"}

                                },

                                "required": []

                            }

                        }

                    },

                    {

                        "type": "function",

                        "function": {

                            "name": "type_text",

                            "description": "Types the given text using the keyboard.",

                            "parameters": {

                                "type": "object",

                                "properties": {

                                    "text": {"type": "string", "description": "The text to type."}

                                },

                                "required": ["text"]

                            }

                        }

                    },

                    {

                        "type": "function",

                        "function": {

                            "name": "scroll",

                            "description": "Scrolls the mouse wheel.",

                            "parameters": {

                                "type": "object",

                                "properties": {

                                    "amount": {"type": "integer", "description": "The amount to scroll. Positive for up, negative for down."}

                                },

                                "required": ["amount"]

                            }

                        }

                    },

                    {

                        "type": "function",

                        "function": {

                            "name": "take_screenshot",

                            "description": "Takes a screenshot of the entire screen.",

                            "parameters": {

                                "type": "object",

                                "properties": {

                                    "filename": {"type": "string", "description": "The filename to save the screenshot as."}

                                },

                                "required": ["filename"]

                            }

                        }

                    }

                ]



            @authenticate_and_execute

            def move_mouse_to(self, x: int, y: int, duration: float = 0.5) -> str:

                """Moves the mouse to the specified coordinates."""

                pyautogui.moveTo(x, y, duration=duration)

                return f"Mouse moved to ({x}, {y})."



            @authenticate_and_execute

            def click(self, button: str = 'left') -> str:

                """Performs a mouse click."""

                pyautogui.click(button=button)

                return f"{button.capitalize()} click performed."



            @authenticate_and_execute

            def type_text(self, text: str) -> str:

                """Types the given text."""

                pyautogui.typewrite(text)

                return f"Typed text: {text}"



            @authenticate_and_execute

            def scroll(self, amount: int) -> str:

                """Scrolls the mouse wheel."""

                pyautogui.scroll(amount)

                return f"Scrolled by {amount} units."



            @authenticate_and_execute

            def take_screenshot(self, filename: str) -> str:

                """Takes a screenshot and saves it."""

                screenshot = pyautogui.screenshot()

                screenshot.save(filename)

                return f"Screenshot saved to {filename}."



    except ImportError:

        logging.error("PyAutoGUI or other dependencies for PCInteractionTool are not installed. The tool will be disabled.")

        # If imports fail, fall back to the dummy class

        # The class is already defined as a placeholder, so we just pass

        pass



