
    soj'                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ  G d
 d          ZdS )    N)CallbackQuery)ContextTypes)User)ContextDataKey)MessageSource)ReservedKeyboardKeys)Screen)get_semantic_button_stylec                   T   e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$dedededee         ded	ed
ee         dedededededee         de	defdZ
defdZd Zdej        defdZe	 	 	 d%dej        de	dedededd fd            ZdefdZdefdZdefdZdefd Zd! Zdefd"Zdedefd#ZdS )&KeyboardNTFtextinfoscreenprevious_screen_listurlinherit_authorized_usersauthorized_usersinbound_infofrom_deeplinkis_deeplink"only_authorized_users_can_interactswitch_inline_queryexclude_key_from_inbound_infomessage_sourcestylec                    || _         |pt          |          | _        ||ni | _        || _        ||ng | _        || _        || _        ||ng | _        |	| _	        |
| _
        || _        || _        d| _        ||ng | _        |r|                                ni | _        |                                 | _        t'          | j                  dk    rM| j        D ]}| j                            |d           | j        | j        z  | _        |                                  || _        | j
        r|                                  dS dS )a  
        Creates a keyboard object
        :param text: The text to be displayed on the keyboard
        :param info: The info embedded in the keyboard callback data
        :param screen: The screen to transition to
        :param previous_screen_list: The previous screens list
        :param url: The url to be displayed on the keyboard
        :param inherit_authorized_users: If the authorized users list should be inherited from the
        default value
        :param authorized_users: The authorized users list
        :param inbound_info: The inbound info to be added to the keyboard info
        :param from_deeplink: If the keyboard is being created from a deeplink
        :param is_deeplink: If the keyboard is a deeplink, generate the deeplink url
        :param only_authorized_users_can_interact: If only authorized users can interact with the
        keyboard
        :param switch_inline_query: The switch inline query
        :param exclude_key_from_inbound_info: The keys to be excluded from the inbound info
        :param message_source: The source of the message
        :param style: The style of the button (e.g. success, danger, primary)
        NFr   )r   r
   r   r   r   r   r   r   r   r   r   r   r   is_simulatedr   copyr   create_callback_datacallback_datalenpoprefresh_callback_datar   set_deeplink_url)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   keys                    /app/src/model/pojo/Keyboard.py__init__zKeyboard.__init__   s   L 	=7==
"&"2$$	$$8$D  " 	! .F% 0 <" 	 $1!,8Z/(; "'-J-V))\^ 	* 4@GL--///R"&";";"="=t !!A%%9 1 1!%%c40000)DI5DI&&(((, 	$!!#####	$ 	$    returnc                 .   | j                                         }| j        )t          | j        dd                   |t          j        <   t          j        |v r|t          j                 nd| j        8t          | j                  dk    r fd| j        D             |t          j        <   |	                                D ],\  }}t          |t                    rt          |          ||<   -| j        rFt          j        |vrg |t          j        <   |t          j        xx         d | j        D             z  cc<   t          j        |v r9t          t          |t          j                                     |t          j        <   t!          j        |d          S )z_
        Creates the callback data for the keyboard
        :return: The callback data
        N   r   c                 t    g | ]4}t          |d d                   k    t          |d d                   5S )r,   N)int).0r   temp_screen_codes     r'   
<listcomp>z1Keyboard.create_callback_data.<locals>.<listcomp>m   sN     F F Fvabbz??&666 F122J666r)   c                     g | ]	}|j         
S  )id)r/   users     r'   r1   z1Keyboard.create_callback_data.<locals>.<listcomp>|   s-     H H H H H Hr)   ,:
separators)r   r   r   r.   r   SCREENr   r!   PREVIOUS_SCREENitems
isinstanceboolr   AUTHORIZED_USERSlistsetjsondumps)r%   info_with_screenr&   valuer0   s       @r'   r   zKeyboard.create_callback_dataZ   s     9>>++;"<?ABB<P<P189 $*.>>> 1899 	 $0S9R5S5SVW5W5WF F F F"7F F F1AB +0022 	3 	3JC%&& 3(+E

 %   	#4<LLLJL !5!FG1BCCC H H$($9H H H CCC  04DDDFJ$%9%JKLLG G1BC z*zBBBBr)   c                 8    |                                  | _        dS )z=
        Refreshes the callback data of the keyboard
        N)r   r    )r%   s    r'   r#   zKeyboard.refresh_callback_data   s     "6688r)   contextc                    t          | j                  t          j        k     r| j        S t	          j        | j                                                                                  }t          j	        |t          j        | j        |           t          j        d|            t          j        t           j        |id          S )zq
        Get the callback data in the context if longer than allowed
        :param context: The context
        	inner_keyz8Keyboard data too long, saved in context with inner key r6   r9   )r!   r    cTG_KEYBOARD_DATA_MAX_LENhashlibmd5encode	hexdigestr   set_context_datar   KEYBOARD_DATAloggingdebugrC   rD   r   CONTEXT)r%   rH   rK   s      r'   $set_and_get_callback_data_in_contextz-Keyboard.set_and_get_callback_data_in_context   s     t!""Q%???%% K 2 9 9 ; ;<<FFHH	^143EQZ	
 	
 	
 	
 	\QZ\\]]]z/7CPZ[[[[r)   callback_queryinfo_strc                    ||t          d          |t          j        |          }nt          j        |j                  }t          j        |v rut          j        | t          j	        |t          j                           }t          j
        | t          j	        |t          j                            t          j        |          }	 t          j        |v r0t          t          |t          j                           z             }nd}n## t           t          f$ r t          j        }Y nw xY wt          j        |v rfd|t          j                 D             }ng }d}	t#          |	||||          S )a  
        Create a Keyboard object from a CallbackQuery object
        :param context: The context
        :param callback_query: CallbackQuery object
        :param message_source: Source of the message
        :param info_str: The info to be added to the keyboard info
        :param from_deeplink: If the keyboard is being created from a deeplink
        :return: Keyboard object
        Nz.Either callback_query or info must be providedrJ   c                 N    g | ]!}t          t          |          z             "S r3   )r	   str)r/   r   r   s     r'   r1   z<Keyboard.get_from_callback_query_or_info.<locals>.<listcomp>   s<     $ $ $ ~F344$ $ $r)    )r   r   r   r   r   )
ValueErrorrC   loadsdatar   rV   r   get_context_datar   rS   remove_context_datar;   r	   r\   KeyErrorUNKNOWNr<   r   )
rH   r   rX   rY   r   r   info_str_from_contextr   r   r   s
    `        r'   get_from_callback_query_or_infoz(Keyboard.get_from_callback_query_or_info   s   $ !h&6MNNNH--DDN$788D  '4//$($9,3;<% % %! $5t<P<X7Y   :344D	$#*d22T:N:U5V1W1W WXXH% 	$ 	$ 	$^FFF	$  /477$ $ $ $"#7#GH$ $ $  
 $& !5')
 
 
 	
s   	A D
 
D*)D*r&   c                 6    | j                             |          S )k
        Get a value from the keyboard info
        :param key: The key
        :return: The value
        r   getr%   r&   s     r'   rj   zKeyboard.get   s     y}}S!!!r)   c                 8    | j                             |d          S )rh   Nri   rk   s     r'   get_or_nonezKeyboard.get_or_none   s     y}}S$'''r)   c                 P    t          | j                            |                    S )zp
        Get an int value from the keyboard info
        :param key: The key
        :return: The value
        )r.   r   rj   rk   s     r'   get_intzKeyboard.get_int   s      49==%%&&&r)   c                 P    t          | j                            |                    S )zp
        Get a bool value from the keyboard info
        :param key: The key
        :return: The value
        )r?   r   rj   rk   s     r'   get_boolzKeyboard.get_bool   s      DIMM#&&'''r)   c                 <    ddl m}  || j                  | _        dS )z7
        Set the deeplink url for the keyboard
        r   )get_deeplinkN)src.service.message_servicers   r    r   )r%   rs   s     r'   r$   zKeyboard.set_deeplink_url  s.    
 	=<<<<<< 233r)   c                 f    || _         t          |dd                   | j        t          j        <   dS )zB
        Set the screen
        :param screen: The screen
        r,   N)r   r.   r   r   r;   )r%   r   s     r'   
set_screenzKeyboard.set_screen  s/     14VABBZ	&-...r)   c                     || j         v S )zu
        Check if the keyboard info has a key
        :param key: The key
        :return: If the key exists
        )r   rk   s     r'   has_keyzKeyboard.has_key  s     dir)   )NNNNTNNFFTNNNN)NNF)__name__
__module____qualname__r\   dictr	   rA   r?   r   r   r(   r   r#   r   DEFAULT_TYPErW   staticmethodr   rf   rj   rm   ro   rq   r$   rv   rx   r3   r)   r'   r   r      s        -1)-'+!#!37#'37(,!F$ F$F$ F$ 	F$
 #6lF$ F$ #'F$ t*F$ F$ F$ F$ -1F$ !F$ (,CyF$ &F$  !F$ F$ F$ F$P+Cc +C +C +C +CZ9 9 9\L<U \Z] \ \ \ \"  )-#?
 ?
*?
%?
 &?
 	?

 ?
 
?
 ?
 ?
 \?
B"s " " " "(s ( ( ( ('3 ' ' ' '(C ( ( ( (4 4 4A A A A A 3  4            r)   r   )rN   rC   rT   src.tg_compatr   src.tg_compat.extr   	constantsrL   src.model.Userr   src.model.enums.ContextDataKeyr   src.model.enums.MessageSourcer   $src.model.enums.ReservedKeyboardKeysr   src.model.enums.Screenr	   src.utils.button_style_utilsr
   r   r3   r)   r'   <module>r      s       ' ' ' ' ' ' * * * * * *           9 9 9 9 9 9 7 7 7 7 7 7 E E E E E E ) ) ) ) ) ) B B B B B BL  L  L  L  L  L  L  L  L  L r)   