# DO NOT EDIT THIS FILE!
#
# This file is generated from the CDP specification. If you need to make
# changes, edit the generator and regenerate all of the modules.
#
# CDP domain: Input
from __future__ import annotations
from .util import event_class, T_JSON_DICT
from dataclasses import dataclass
import enum
import typing

@dataclass
class TouchPoint:
    #: X coordinate of the event relative to the main frame's viewport in CSS pixels.
    x: float

    #: Y coordinate of the event relative to the main frame's viewport in CSS pixels. 0 refers to
    #: the top of the viewport and Y increases as it proceeds towards the bottom of the viewport.
    y: float

    #: X radius of the touch area (default: 1.0).
    radius_x: typing.Optional[float] = None

    #: Y radius of the touch area (default: 1.0).
    radius_y: typing.Optional[float] = None

    #: Rotation angle (default: 0.0).
    rotation_angle: typing.Optional[float] = None

    #: Force (default: 1.0).
    force: typing.Optional[float] = None

    #: Identifier used to track touch sources between events, must be unique within an event.
    id_: typing.Optional[float] = None

    def to_json(self):
        json = dict()
        json['x'] = self.x
        json['y'] = self.y
        if self.radius_x is not None:
            json['radiusX'] = self.radius_x
        if self.radius_y is not None:
            json['radiusY'] = self.radius_y
        if self.rotation_angle is not None:
            json['rotationAngle'] = self.rotation_angle
        if self.force is not None:
            json['force'] = self.force
        if self.id_ is not None:
            json['id'] = self.id_
        return json

    @classmethod
    def from_json(cls, json):
        return cls(
            x=float(json['x']),
            y=float(json['y']),
            radius_x=float(json['radiusX']) if 'radiusX' in json else None,
            radius_y=float(json['radiusY']) if 'radiusY' in json else None,
            rotation_angle=float(json['rotationAngle']) if 'rotationAngle' in json else None,
            force=float(json['force']) if 'force' in json else None,
            id_=float(json['id']) if 'id' in json else None,
        )


class GestureSourceType(enum.Enum):
    DEFAULT = "default"
    TOUCH = "touch"
    MOUSE = "mouse"

    def to_json(self):
        return self.value

    @classmethod
    def from_json(cls, json):
        return cls(json)


class MouseButton(enum.Enum):
    NONE = "none"
    LEFT = "left"
    MIDDLE = "middle"
    RIGHT = "right"
    BACK = "back"
    FORWARD = "forward"

    def to_json(self):
        return self.value

    @classmethod
    def from_json(cls, json):
        return cls(json)


class TimeSinceEpoch(float):
    '''
    UTC time in seconds, counted from January 1, 1970.
    '''
    def to_json(self) -> float:
        return self

    @classmethod
    def from_json(cls, json: float) -> TimeSinceEpoch:
        return cls(json)

    def __repr__(self):
        return 'TimeSinceEpoch({})'.format(super().__repr__())


def dispatch_key_event(
        type_: str,
        modifiers: typing.Optional[int] = None,
        timestamp: typing.Optional[TimeSinceEpoch] = None,
        text: typing.Optional[str] = None,
        unmodified_text: typing.Optional[str] = None,
        key_identifier: typing.Optional[str] = None,
        code: typing.Optional[str] = None,
        key: typing.Optional[str] = None,
        windows_virtual_key_code: typing.Optional[int] = None,
        native_virtual_key_code: typing.Optional[int] = None,
        auto_repeat: typing.Optional[bool] = None,
        is_keypad: typing.Optional[bool] = None,
        is_system_key: typing.Optional[bool] = None,
        location: typing.Optional[int] = None,
        commands: typing.Optional[typing.List[str]] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Dispatches a key event to the page.

    :param type_: Type of the key event.
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param timestamp: *(Optional)* Time at which the event occurred.
    :param text: *(Optional)* Text as generated by processing a virtual key code with a keyboard layout. Not needed for for ```keyUp```` and ````rawKeyDown```` events (default: "")
    :param unmodified_text: *(Optional)* Text that would have been generated by the keyboard if no modifiers were pressed (except for shift). Useful for shortcut (accelerator) key handling (default: "").
    :param key_identifier: *(Optional)* Unique key identifier (e.g., 'U+0041') (default: "").
    :param code: *(Optional)* Unique DOM defined string value for each physical key (e.g., 'KeyA') (default: "").
    :param key: *(Optional)* Unique DOM defined string value describing the meaning of the key in the context of active modifiers, keyboard layout, etc (e.g., 'AltGr') (default: "").
    :param windows_virtual_key_code: *(Optional)* Windows virtual key code (default: 0).
    :param native_virtual_key_code: *(Optional)* Native virtual key code (default: 0).
    :param auto_repeat: *(Optional)* Whether the event was generated from auto repeat (default: false).
    :param is_keypad: *(Optional)* Whether the event was generated from the keypad (default: false).
    :param is_system_key: *(Optional)* Whether the event was a system key event (default: false).
    :param location: *(Optional)* Whether the event was from the left or right side of the keyboard. 1=Left, 2=Right (default: 0).
    :param commands: **(EXPERIMENTAL)** *(Optional)* Editing commands to send with the key event (e.g., 'selectAll') (default: []). These are related to but not equal the command names used in ````document.execCommand``` and NSStandardKeyBindingResponding. See https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/editing/commands/editor_command_names.h for valid command names.
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    if modifiers is not None:
        params['modifiers'] = modifiers
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    if text is not None:
        params['text'] = text
    if unmodified_text is not None:
        params['unmodifiedText'] = unmodified_text
    if key_identifier is not None:
        params['keyIdentifier'] = key_identifier
    if code is not None:
        params['code'] = code
    if key is not None:
        params['key'] = key
    if windows_virtual_key_code is not None:
        params['windowsVirtualKeyCode'] = windows_virtual_key_code
    if native_virtual_key_code is not None:
        params['nativeVirtualKeyCode'] = native_virtual_key_code
    if auto_repeat is not None:
        params['autoRepeat'] = auto_repeat
    if is_keypad is not None:
        params['isKeypad'] = is_keypad
    if is_system_key is not None:
        params['isSystemKey'] = is_system_key
    if location is not None:
        params['location'] = location
    if commands is not None:
        params['commands'] = [i for i in commands]
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.dispatchKeyEvent',
        'params': params,
    }
    json = yield cmd_dict


def insert_text(
        text: str
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    This method emulates inserting text that doesn't come from a key press,
    for example an emoji keyboard or an IME.

    **EXPERIMENTAL**

    :param text: The text to insert.
    '''
    params: T_JSON_DICT = dict()
    params['text'] = text
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.insertText',
        'params': params,
    }
    json = yield cmd_dict


def dispatch_mouse_event(
        type_: str,
        x: float,
        y: float,
        modifiers: typing.Optional[int] = None,
        timestamp: typing.Optional[TimeSinceEpoch] = None,
        button: typing.Optional[MouseButton] = None,
        buttons: typing.Optional[int] = None,
        click_count: typing.Optional[int] = None,
        delta_x: typing.Optional[float] = None,
        delta_y: typing.Optional[float] = None,
        pointer_type: typing.Optional[str] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Dispatches a mouse event to the page.

    :param type_: Type of the mouse event.
    :param x: X coordinate of the event relative to the main frame's viewport in CSS pixels.
    :param y: Y coordinate of the event relative to the main frame's viewport in CSS pixels. 0 refers to the top of the viewport and Y increases as it proceeds towards the bottom of the viewport.
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param timestamp: *(Optional)* Time at which the event occurred.
    :param button: *(Optional)* Mouse button (default: "none").
    :param buttons: *(Optional)* A number indicating which buttons are pressed on the mouse when a mouse event is triggered. Left=1, Right=2, Middle=4, Back=8, Forward=16, None=0.
    :param click_count: *(Optional)* Number of times the mouse button was clicked (default: 0).
    :param delta_x: *(Optional)* X delta in CSS pixels for mouse wheel event (default: 0).
    :param delta_y: *(Optional)* Y delta in CSS pixels for mouse wheel event (default: 0).
    :param pointer_type: *(Optional)* Pointer type (default: "mouse").
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    params['x'] = x
    params['y'] = y
    if modifiers is not None:
        params['modifiers'] = modifiers
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    if button is not None:
        params['button'] = button.to_json()
    if buttons is not None:
        params['buttons'] = buttons
    if click_count is not None:
        params['clickCount'] = click_count
    if delta_x is not None:
        params['deltaX'] = delta_x
    if delta_y is not None:
        params['deltaY'] = delta_y
    if pointer_type is not None:
        params['pointerType'] = pointer_type
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.dispatchMouseEvent',
        'params': params,
    }
    json = yield cmd_dict


def dispatch_touch_event(
        type_: str,
        touch_points: typing.List[TouchPoint],
        modifiers: typing.Optional[int] = None,
        timestamp: typing.Optional[TimeSinceEpoch] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Dispatches a touch event to the page.

    :param type_: Type of the touch event. TouchEnd and TouchCancel must not contain any touch points, while TouchStart and TouchMove must contains at least one.
    :param touch_points: Active touch points on the touch device. One event per any changed point (compared to previous touch event in a sequence) is generated, emulating pressing/moving/releasing points one by one.
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param timestamp: *(Optional)* Time at which the event occurred.
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    params['touchPoints'] = [i.to_json() for i in touch_points]
    if modifiers is not None:
        params['modifiers'] = modifiers
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.dispatchTouchEvent',
        'params': params,
    }
    json = yield cmd_dict


def emulate_touch_from_mouse_event(
        type_: str,
        x: int,
        y: int,
        button: MouseButton,
        timestamp: typing.Optional[TimeSinceEpoch] = None,
        delta_x: typing.Optional[float] = None,
        delta_y: typing.Optional[float] = None,
        modifiers: typing.Optional[int] = None,
        click_count: typing.Optional[int] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Emulates touch event from the mouse event parameters.

    **EXPERIMENTAL**

    :param type_: Type of the mouse event.
    :param x: X coordinate of the mouse pointer in DIP.
    :param y: Y coordinate of the mouse pointer in DIP.
    :param button: Mouse button. Only "none", "left", "right" are supported.
    :param timestamp: *(Optional)* Time at which the event occurred (default: current time).
    :param delta_x: *(Optional)* X delta in DIP for mouse wheel event (default: 0).
    :param delta_y: *(Optional)* Y delta in DIP for mouse wheel event (default: 0).
    :param modifiers: *(Optional)* Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
    :param click_count: *(Optional)* Number of times the mouse button was clicked (default: 0).
    '''
    params: T_JSON_DICT = dict()
    params['type'] = type_
    params['x'] = x
    params['y'] = y
    params['button'] = button.to_json()
    if timestamp is not None:
        params['timestamp'] = timestamp.to_json()
    if delta_x is not None:
        params['deltaX'] = delta_x
    if delta_y is not None:
        params['deltaY'] = delta_y
    if modifiers is not None:
        params['modifiers'] = modifiers
    if click_count is not None:
        params['clickCount'] = click_count
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.emulateTouchFromMouseEvent',
        'params': params,
    }
    json = yield cmd_dict


def set_ignore_input_events(
        ignore: bool
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Ignores input events (useful while auditing page).

    :param ignore: Ignores input events processing when set to true.
    '''
    params: T_JSON_DICT = dict()
    params['ignore'] = ignore
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.setIgnoreInputEvents',
        'params': params,
    }
    json = yield cmd_dict


def synthesize_pinch_gesture(
        x: float,
        y: float,
        scale_factor: float,
        relative_speed: typing.Optional[int] = None,
        gesture_source_type: typing.Optional[GestureSourceType] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Synthesizes a pinch gesture over a time period by issuing appropriate touch events.

    **EXPERIMENTAL**

    :param x: X coordinate of the start of the gesture in CSS pixels.
    :param y: Y coordinate of the start of the gesture in CSS pixels.
    :param scale_factor: Relative scale factor after zooming (>1.0 zooms in, <1.0 zooms out).
    :param relative_speed: *(Optional)* Relative pointer speed in pixels per second (default: 800).
    :param gesture_source_type: *(Optional)* Which type of input events to be generated (default: 'default', which queries the platform for the preferred input type).
    '''
    params: T_JSON_DICT = dict()
    params['x'] = x
    params['y'] = y
    params['scaleFactor'] = scale_factor
    if relative_speed is not None:
        params['relativeSpeed'] = relative_speed
    if gesture_source_type is not None:
        params['gestureSourceType'] = gesture_source_type.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.synthesizePinchGesture',
        'params': params,
    }
    json = yield cmd_dict


def synthesize_scroll_gesture(
        x: float,
        y: float,
        x_distance: typing.Optional[float] = None,
        y_distance: typing.Optional[float] = None,
        x_overscroll: typing.Optional[float] = None,
        y_overscroll: typing.Optional[float] = None,
        prevent_fling: typing.Optional[bool] = None,
        speed: typing.Optional[int] = None,
        gesture_source_type: typing.Optional[GestureSourceType] = None,
        repeat_count: typing.Optional[int] = None,
        repeat_delay_ms: typing.Optional[int] = None,
        interaction_marker_name: typing.Optional[str] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Synthesizes a scroll gesture over a time period by issuing appropriate touch events.

    **EXPERIMENTAL**

    :param x: X coordinate of the start of the gesture in CSS pixels.
    :param y: Y coordinate of the start of the gesture in CSS pixels.
    :param x_distance: *(Optional)* The distance to scroll along the X axis (positive to scroll left).
    :param y_distance: *(Optional)* The distance to scroll along the Y axis (positive to scroll up).
    :param x_overscroll: *(Optional)* The number of additional pixels to scroll back along the X axis, in addition to the given distance.
    :param y_overscroll: *(Optional)* The number of additional pixels to scroll back along the Y axis, in addition to the given distance.
    :param prevent_fling: *(Optional)* Prevent fling (default: true).
    :param speed: *(Optional)* Swipe speed in pixels per second (default: 800).
    :param gesture_source_type: *(Optional)* Which type of input events to be generated (default: 'default', which queries the platform for the preferred input type).
    :param repeat_count: *(Optional)* The number of times to repeat the gesture (default: 0).
    :param repeat_delay_ms: *(Optional)* The number of milliseconds delay between each repeat. (default: 250).
    :param interaction_marker_name: *(Optional)* The name of the interaction markers to generate, if not empty (default: "").
    '''
    params: T_JSON_DICT = dict()
    params['x'] = x
    params['y'] = y
    if x_distance is not None:
        params['xDistance'] = x_distance
    if y_distance is not None:
        params['yDistance'] = y_distance
    if x_overscroll is not None:
        params['xOverscroll'] = x_overscroll
    if y_overscroll is not None:
        params['yOverscroll'] = y_overscroll
    if prevent_fling is not None:
        params['preventFling'] = prevent_fling
    if speed is not None:
        params['speed'] = speed
    if gesture_source_type is not None:
        params['gestureSourceType'] = gesture_source_type.to_json()
    if repeat_count is not None:
        params['repeatCount'] = repeat_count
    if repeat_delay_ms is not None:
        params['repeatDelayMs'] = repeat_delay_ms
    if interaction_marker_name is not None:
        params['interactionMarkerName'] = interaction_marker_name
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.synthesizeScrollGesture',
        'params': params,
    }
    json = yield cmd_dict


def synthesize_tap_gesture(
        x: float,
        y: float,
        duration: typing.Optional[int] = None,
        tap_count: typing.Optional[int] = None,
        gesture_source_type: typing.Optional[GestureSourceType] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Synthesizes a tap gesture over a time period by issuing appropriate touch events.

    **EXPERIMENTAL**

    :param x: X coordinate of the start of the gesture in CSS pixels.
    :param y: Y coordinate of the start of the gesture in CSS pixels.
    :param duration: *(Optional)* Duration between touchdown and touchup events in ms (default: 50).
    :param tap_count: *(Optional)* Number of times to perform the tap (e.g. 2 for double tap, default: 1).
    :param gesture_source_type: *(Optional)* Which type of input events to be generated (default: 'default', which queries the platform for the preferred input type).
    '''
    params: T_JSON_DICT = dict()
    params['x'] = x
    params['y'] = y
    if duration is not None:
        params['duration'] = duration
    if tap_count is not None:
        params['tapCount'] = tap_count
    if gesture_source_type is not None:
        params['gestureSourceType'] = gesture_source_type.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Input.synthesizeTapGesture',
        'params': params,
    }
    json = yield cmd_dict
