"""
Browser Wait Module - Wait for a duration or until an element appears
"""
from typing import Any, Dict
from ...base import BaseModule
from ...registry import register_module
from ...schema import compose, presets


@register_module(
    module_id='browser.wait',
    version='1.0.0',
    category='browser',
    tags=['browser', 'wait', 'delay', 'selector'],
    label='Wait',
    label_key='modules.browser.wait.label',
    description='Wait for a duration or until an element appears',
    description_key='modules.browser.wait.description',
    icon='Clock',
    color='#95A5A6',

    # Connection types
    input_types=['page'],
    output_types=['page'],

    # Connection rules
    can_receive_from=['browser.*', 'flow.*'],
    can_connect_to=['browser.*', 'element.*', 'page.*', 'screenshot.*', 'flow.*'],

    # Schema-driven params
    params_schema=compose(
        presets.DURATION_S(default=1),
        presets.SELECTOR(required=False, placeholder='.element-to-wait-for'),
        presets.TIMEOUT_MS(default=30000),
    ),
    output_schema={
        'status': {'type': 'string'},
        'selector': {'type': 'string', 'optional': True},
        'duration': {'type': 'number', 'optional': True}
    },
    examples=[
        {
            'name': 'Wait 2 seconds',
            'params': {'duration': 2}
        },
        {
            'name': 'Wait for element',
            'params': {'selector': '#loading-complete'}
        }
    ],
    author='Flyto2 Team',
    license='MIT'
)
class BrowserWaitModule(BaseModule):
    """Wait Module"""

    module_name = "Wait"
    module_description = "Wait for a duration or element to appear"
    required_permission = "browser.interact"

    def validate_params(self):
        self.duration = self.params.get('duration', 1)
        self.selector = self.params.get('selector')
        self.timeout = self.params.get('timeout', 30000)

    async def execute(self) -> Any:
        import asyncio

        browser = self.context.get('browser')

        if self.selector:
            # Wait for element to appear
            if not browser:
                raise RuntimeError("Browser not launched. Please run browser.launch first")
            await browser.wait(self.selector, timeout_ms=self.timeout)
            return {"status": "success", "selector": self.selector}
        elif self.timeout and not self.selector and 'duration' not in self.params:
            # If only timeout is provided (used as simple delay in ms)
            await asyncio.sleep(self.timeout / 1000)
            return {"status": "success", "duration": self.timeout / 1000}
        else:
            # Wait for specified duration in seconds
            await asyncio.sleep(self.duration)
            return {"status": "success", "duration": self.duration}


