"""
Element 代理类类型存根

提供 VSCode/Pylance 类型提示支持。
"""
from typing import Optional, Union, List, Dict, Any, TYPE_CHECKING
from playwright.async_api import Locator, Page

if TYPE_CHECKING:
    from .relative import RelativeLocator


class Element:
    """元素代理类"""

    def __init__(
        self,
        locator: Locator,
        page: Page,
        selector: str = "",
    ) -> None: ...

    # ==================== 属性访问 ====================
    @property
    def locator(self) -> Locator: ...
    @property
    def page(self) -> Page: ...
    @property
    async def text(self) -> str: ...
    @property
    async def inner_text(self) -> str: ...
    @property
    async def value(self) -> str: ...
    @property
    async def html(self) -> str: ...
    @property
    async def attribute(self) -> dict: ...
    @property
    async def href(self) -> Optional[str]: ...
    @property
    async def src(self) -> Optional[str]: ...
    @property
    async def class_name(self) -> Optional[str]: ...
    @property
    async def id(self) -> Optional[str]: ...
    @property
    async def tag_name(self) -> str: ...

    # ==================== 操作方法 ====================
    async def click(
        self,
        force: bool = False,
        timeout: Optional[float] = None,
        no_wait_after: bool = False,
        position: Optional[dict] = None,
    ) -> "Element": ...
    async def dblclick(
        self,
        force: bool = False,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def fill(
        self,
        value: str,
        force: bool = False,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def type(
        self,
        text: str,
        delay: int = 50,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def clear(self, timeout: Optional[float] = None) -> "Element": ...
    async def check(
        self,
        force: bool = False,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def uncheck(
        self,
        force: bool = False,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def select_option(
        self,
        value: Optional[Union[str, list]] = None,
        label: Optional[Union[str, list]] = None,
        index: Optional[Union[int, list]] = None,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def hover(
        self,
        force: bool = False,
        position: Optional[dict] = None,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def focus(self, timeout: Optional[float] = None) -> "Element": ...
    async def blur(self) -> "Element": ...
    async def press(
        self,
        key: str,
        delay: Optional[int] = None,
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def scroll_into_view(
        self,
        timeout: Optional[float] = None,
    ) -> "Element": ...

    # ==================== 等待方法 ====================
    async def wait_for(
        self,
        state: str = "visible",
        timeout: Optional[float] = None,
    ) -> "Element": ...
    async def wait_until_visible(self, timeout: Optional[float] = None) -> "Element": ...
    async def wait_until_hidden(self, timeout: Optional[float] = None) -> "Element": ...
    async def wait_until_attached(self, timeout: Optional[float] = None) -> "Element": ...

    # ==================== 状态检查 ====================
    async def is_visible(self) -> bool: ...
    async def is_hidden(self) -> bool: ...
    async def is_enabled(self) -> bool: ...
    async def is_disabled(self) -> bool: ...
    async def is_editable(self) -> bool: ...
    async def is_checked(self) -> bool: ...
    async def count(self) -> int: ...

    # ==================== 相对定位 ====================
    @property
    def relative(self) -> "RelativeLocator": ...
    def parent(self, level: int = 1) -> "Element": ...
    def child(self, selector: Optional[str] = None, index: int = 0) -> "Element": ...
    def sibling(
        self,
        selector: Optional[str] = None,
        direction: str = "next"
    ) -> "Element": ...
    def next_sibling(self, selector: Optional[str] = None) -> "Element": ...
    def prev_sibling(self, selector: Optional[str] = None) -> "Element": ...
    def ancestor(self, selector: str) -> "Element": ...

    # ==================== 筛选方法 ====================
    def nth(self, index: int) -> "Element": ...
    def first(self) -> "Element": ...
    def last(self) -> "Element": ...
    def filter(
        self,
        has_text: Optional[str] = None,
        has_not_text: Optional[str] = None,
        has: Optional["Element"] = None,
    ) -> "Element": ...

    # ==================== 属性操作 ====================
    async def get_attribute(self, name: str) -> Optional[str]: ...
    async def set_attribute(self, name: str, value: str) -> "Element": ...
    async def remove_attribute(self, name: str) -> "Element": ...
    async def add_class(self, class_name: str) -> "Element": ...
    async def remove_class(self, class_name: str) -> "Element": ...

    # ==================== 表单操作 ====================
    async def submit(self) -> "Element": ...
    async def set_input_files(
        self,
        files: Union[str, list],
        timeout: Optional[float] = None,
    ) -> "Element": ...

    # ==================== 截图 ====================
    async def screenshot(
        self,
        path: Optional[str] = None,
        timeout: Optional[float] = None,
    ) -> bytes: ...

    # ==================== JavaScript 执行 ====================
    async def evaluate(self, expression: str) -> Any: ...
    async def evaluate_all(self, expression: str) -> Any: ...


class ElementList:
    """元素列表类"""

    def __init__(
        self,
        locator: Locator,
        page: Page,
        selector: str = "",
    ) -> None: ...

    async def count(self) -> int: ...
    async def texts(self) -> List[str]: ...
    async def attributes(self, name: str) -> List[Optional[str]]: ...
    def __getitem__(self, index: int) -> Element: ...
