
    soj                     j    d dl Z d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ  G d de          ZdS )    N)ImageImageFilter)	GameBoard)	Character)generate_temp_file_pathdownload_temp_filec                   x     e Zd Z	 	 	 	 ddededededef
 fd	Zd
 Zd Zd Z	de
fdZdede
fdZde
fdZ xZS )WhosWhoN   r   	character
image_pathlevellatest_blurred_imagerevealed_letters_countc                 l   t                                                       || _        || _        || _        || _        || _        | j        X	 t          j        | j                  5  	 ddd           n# 1 swxY w Y   n8# t          $ r | 
                                 Y nw xY w| 
                                 | j        X	 t          j        | j                  5  	 ddd           n# 1 swxY w Y   n$# t          $ r |                                  Y nw xY w| j        |                                  dS dS )a&  
        Constructor
        :param character: The character
        :param image_path: The downloaded image path
        :param level: The level
        :param latest_blurred_image: The latest blurred image tg media id
        :param revealed_letters_count: The revealed letters count
        N)super__init__r   r   r   r   r   r   openFileNotFoundError_safe_download_imageset_blurred_image)selfr   r   r   r   r   	__class__s         &/app/src/model/game/whoswho/WhosWho.pyr   zWhosWho.__init__   s     	"$
$8!&<# ?&,Z00                $ , , ,))+++++, %%''' $0)Z 9::                $ ) ) )&&((((() $,""$$$$$ -,sl   B  &A4(B  4A88B  ;A8<B   B! B! C3 C'C3 'C++C3 .C+/C3 3DDc                    	 t          | j        j        d          | _        dS # t          j        j        t          j        j        f$ rE}t          j	        d| j        j
         d|            t          d| j        j
                   d}~wt          $ rE}t          j	        d| j        j
         d|            t          d| j        j
                   d}~ww xY w)z
        Safely attempts to download the character image.
        Raises ValueError if the download fails (e.g., 404 Not Found)
        jpgzDead image link for z: zImage unavailable for NzFailed to process image for zImage processing failed for )r   r   anime_image_urlr   urlliberror	HTTPErrorURLErrorloggingwarningname
ValueError	Exception)r   es     r   r   zWhosWho._safe_download_image8   s    
	S01OQVWWDOOO&(=> 	M 	M 	MOM4>3FMM!MMNNNKdn6IKKLLL 	S 	S 	SOU4>;NUURSUUVVVQDN<OQQRRR	Ss#   # %CA BCA CCc                 N   t          j        | j                  5 }|                    d          }|                    t          j        | j        dz
  dz                      }t          d          }|	                    |           || _
        ddd           dS # 1 swxY w Y   dS )z=
        Set the blurred image
        :return: None
        RGB   
   r   N)r   r   r   convertfilterr   GaussianBlurr   r   saver   )r   image	save_paths      r   r   zWhosWho.set_blurred_imageF   s     Z(( 	2EMM%((ELL!94:>R:O!P!PQQE/66IJJy!!!(1D%	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   A3BB!Bc                 h    | j         dk    r&| xj         dz  c_         |                                  dS dS )z8
        Reduce the level
        :return: None
        r   r*   N)r   r   r   s    r   reduce_levelzWhosWho.reduce_levelS   s=    
 :>>JJ!OJJ""$$$$$ >    returnc                     | j         dk    S )zI
        Check if the level can be reduced
        :return: bool
        r*   )r   r3   s    r   can_reduce_levelzWhosWho.can_reduce_level\   s    
 zA~r5   answerc                 r    |duo3|                                 | j        j                                         k    S )zh
        Check if the answer is correct
        :param answer: The answer
        :return: bool
        N)lowerr   r$   )r   r9   s     r   
is_correctzWhosWho.is_correctc   s2     T!Sfllnn8K8Q8Q8S8S&SSr5   c                 F    | j         t          | j        j                  k    S )zO
        Check if all letters have been revealed
        :return: bool
        )r   lenr   r$   r3   s    r   have_revealed_all_lettersz!WhosWho.have_revealed_all_lettersk   s    
 *c$.2E.F.FFFr5   )Nr   Nr   )__name__
__module____qualname__r   strintr   r   r   r4   boolr8   r<   r?   __classcell__)r   s   @r   r
   r
   
   s        $(&'+% +%+% +% 	+%
 "+% !$+% +% +% +% +% +%ZS S S2 2 2% % %$    T T T T T TG4 G G G G G G G Gr5   r
   )r"   urllib.errorr   PILr   r   src.model.game.GameBoardr   src.model.wiki.Characterr   src.utils.download_utilsr   r   r
    r5   r   <module>rM      s         " " " " " " " " . . . . . . . . . . . . P P P P P P P PfG fG fG fG fGi fG fG fG fG fGr5   