
    
i'                     l    d Z ddlZddlZddlZddlmZmZmZmZ ddl	m
Z
 ddlmZ  G d d          ZdS )z
Click Element Primitive Operation

This module provides element clicking functionality with human-like mouse movement.
Uses Bezier curves and erratic movements to simulate natural human behavior.
    N)DictAnyTupleList)Page)ManagedPagec                       e Zd ZdZd Zddedededeee	f         fdZ
dded	ed
ededeee	f         f
dZdedededefdZ	 ddedededededeeeef                  fdZdS )ClickElementa  
    Click Element with Human-like Mouse Movement

    Implements realistic mouse movement using Bezier curves with random variations
    to simulate natural human behavior. The movement includes:
    - Random starting position
    - Curved path using cubic Bezier curves
    - Random variations/jitter along the path
    - Variable speed (slower at start/end, faster in middle)

    Usage:
        clicker = ClickElement()
        result = await clicker.click_selector(managed_page, selector, slowmo=1000)
    c                 $    t          d           dS )z%Initialize the click element handler.u    🖱️ ClickElement initializedN)print)selfs    @/home/byschii/byschiidev/penelope/lib/primitive/click_element.py__init__zClickElement.__init__"   s    011111    d   managed_pageselectorslowmoreturnc                   K   	 t          d| d           |j                            |           d{V }|st          d| d          |                                 d{V }|st          d| d          |                                 d{V }|st          d| d          |d         |d	         d
z  z   t          j        |d	          dz  |d	         dz            z   }|d         |d         d
z  z   t          j        |d          dz  |d         dz            z   }|                     ||||           d{V  |j        j	        
                    ||           d{V  ||_        ||_        t          j        |dz             d{V  d||j        j        d}	t          d|            |	S # t          $ r1}
d|t!          |
          d}t          d|
            |cY d}
~
S d}
~
ww xY w)au  
        Click an element on the page with human-like mouse movement.

        Args:
            page (Page): Playwright page to use
            selector (str): CSS selector of element to click
            slowmo (int): Base delay in milliseconds for movement timing (default: 100)

        Returns:
            Dict containing click result and element information
        u   👆 Clicking element ''Nz	Element 'z' not foundz' is not visiblez(Could not get bounding box for element 'xwidth   皙?yheight  T)successr   urlu   ✅ Element clicked: F)r   r   erroru   ❌ Element click failed: )r   pagequery_selector	Exception
is_visiblebounding_boxrandomuniform_move_mouse_human_likemouseclicklast_known_mouse_xlast_known_mouse_yasynciosleepr    str)r   r   r   r   elementr%   boxtarget_xtarget_yresulteerror_results               r   click_selectorzClickElement.click_selector&   s     2	 7H777888 )-<<XFFFFFFFFG C AH A A ABBB&1133333333J H FH F F FGGG  ,,........C X V8 V V VWWW 3x#g,"22V^S\MTWDWY\]dYehkYk5l5llH3x#h-!"33fnc(m^VYEY[^_g[hkn[n6o6ooH --lHhPVWWWWWWWWW #)//(CCCCCCCCC /7L+.6L+ -.........  $#(, F 4(44555M 	  	  	  $Q L
 2q22333	 s   F2F7 7
G2&G-'G2-G2r   r   c                   K   	 t          d| d| d           |                     ||||           d{V  |j        j                            ||           d{V  ||_        ||_        t          j        |dz             d{V  d||d|j        j	        d}t          d	| d| d           |S # t          $ r4}d
||dt          |          d}t          d|            |cY d}~S d}~ww xY w)a  
        Click at specific (x, y) position on the page with human-like mouse movement.

        Args:
            page (Page): Playwright page to use
            x (float): X coordinate to click
            y (float): Y coordinate to click
            slowmo (int): Base delay in milliseconds for movement timing (default: 100)

        Returns:
            Dict containing click result and position information
        u   👆 Clicking position (z, )Nr   T)r   r   )r   positionr    u   ✅ Position clicked: (F)r   r;   r!   u   ❌ Position click failed: )r   r)   r"   r*   r+   r,   r-   r.   r/   r    r$   r0   )r   r   r   r   r   r5   r6   r7   s           r   click_positionzClickElement.click_positionf   s      	 6Q66!666777 --lAq&IIIIIIIII #)//1555555555 /0L+./L+ -.........  "#!,,#(, F 5A55555666M 	  	  	  "#!,,Q L
 333444	 s   B/B4 4
C2>)C-'C2-C2r3   r4   c                   K   |j         }|j        }|j        j                            ||d           d{V  t          j        t          j        dd                     d{V  | 	                    ||||          }t          |          }t          |          D ]\  }	\  }
}t          j        dd          }t          j        dd          }|
|z   }||z   }|j        j                            ||d           d{V  |	|z  }|dk     s|d	k    r|d
z  |z  t          j        dd          z  }n|d
z  |z  t          j        dd          z  }t          j        |           d{V  dS )a  
        Move mouse from random starting position to target with human-like curved path.

        Uses cubic Bezier curves with random control points to create natural-looking
        curved movements. Adds jitter and variable speed for realism.

        Args:
            page (Page): Playwright page
            target_x (float): Target X coordinate
            target_y (float): Target Y coordinate
            slowmo (int): Base delay for timing
           )stepsNg{Gz?g?g      g      ?r   g?r   g      @      ?      ?)r,   r-   r"   r*   mover.   r/   r'   r(   _generate_bezier_pathlen	enumerate)r   r   r3   r4   r   start_xstart_ypath_pointstotal_pointsir   r   jitter_xjitter_yfinal_xfinal_yprogressdelays                     r   r)   z#ClickElement._move_mouse_human_like   s      11 %**7G1*EEEEEEEEEmFN466777777777 00'8XVV ;''";// 	' 	'IAv1~dC00H~dC00H(lG(lG #)..wq.IIIIIIIII <'H#~~C4v~c37O7OO 4v~c37O7OO-&&&&&&&&&&)	' 	'r      rF   rG   
num_pointsc                    t          j        ||z
  dz  ||z
  dz  z             }||z   dz  }||z   dz  }t          j        dd          }	||	z  }
||z
  }||z
  }t          j        ||          t           j        dz  z   }||dz  z   t          j        |          |
z  t          j        dd          z  z   }||dz  z   t          j        |          |
z  t          j        dd          z  z   }||dz  z   t          j        |          |
z  t          j        dd	          z  z   }||dz  z   t          j        |          |
z  t          j        dd	          z  z   }g }t          |          D ]}||d
z
  z  }d
|z
  dz  |z  dd
|z
  dz  z  |z  |z  z   dd
|z
  z  |dz  z  |z  z   |dz  |z  z   }d
|z
  dz  |z  dd
|z
  dz  z  |z  |z  z   dd
|z
  z  |dz  z  |z  z   |dz  |z  z   }|	                    ||f           |S )a  
        Generate a curved path using cubic Bezier curve with random control points.

        Args:
            start_x (float): Starting X coordinate
            start_y (float): Starting Y coordinate
            target_x (float): Target X coordinate
            target_y (float): Target Y coordinate
            num_points (int): Number of points along the curve (default: 30)

        Returns:
            List of (x, y) tuples representing the path
        r   g333333?gffffff?gQ?r@   rA   gq=
ףp?g      g      r>      )
mathsqrtr'   r(   atan2picossinrangeappend)r   rF   rG   r3   r4   rR   distancemid_xmid_ycurve_intensityoffsetdxdyanglecp1_xcp1_ycp2_xcp2_ypointsrJ   tr   r   s                          r   rC   z"ClickElement._generate_bezier_path   s   " 9h01478JQ7NNOO 8#q(8#q( !.t44O+ 
2r""TWq[0 "t)#dhuoo&>PSUXAYAY&YY"t)#dhuoo&>PSUXAYAY&YY "t)#dhuoo&>PTVZA[A[&[["t)#dhuoo&>PTVZA[A[&[[ z"" 	" 	"AZ!^$A
 Q
W$QUQJ"U*+QUad"U*+ 1x   Q
W$QUQJ"U*+QUad"U*+ 1x   MM1a&!!!!r   N)r   )rQ   )__name__
__module____qualname____doc__r   r   r0   intr   r   r8   floatr<   r)   r   r   rC    r   r   r
   r
      sW        2 2 2>  >  >  > UX > cghkmphpcq >  >  >  > @-  -  -  - 5 - Z] - hlmprumuhv -  -  -  - `/' /'PU /'af /'ps /' /' /' /'f 13C CU CU C(-C9>C*-C7;E%,<O7PC C C C C Cr   r
   )rn   r.   r'   rU   typingr   r   r   r   patchright.async_apir   lib.page_managementr   r
   rq   r   r   <module>ru      s        ) ) ) ) ) ) ) ) ) ) ) ) & & & & & & + + + + + +x x x x x x x x x xr   