
    i!                        d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ  e       Z e       Zg dZg dZd Z	 	 dd
ee
   deee
gef      deee
ee   ee   gef      dee   dee   deeeef      defdZ	 ddeegef   deee
   eeeef      gef   dedededee   dedeeeef      deeeef      defdZy	)    )CallableOptionalDictAny)Console)Prompt)	MediaItemMediaManager)TVShowManager)redmagentayellowcyangreenbluewhite)SlugSub_itaFirst_air_dateSeasons_countUrlImagePath_idc                    |j                   sy|j                   st        j                  d       y|j                   d   }ddt        d   ii}d}|j                  j                         D ]v  }|j                         t        v r|dv r(|dk(  rdd	i|d
<   +|dk(  rddi|d<   8|dk(  s>ddi|d<   Fdt        |t        t              z     i||j                         <   |dz  }x | j                          | j                  |       t        |j                         D ]  \  }}dt        |      i}|j                  j                         D ]?  }|j                         t        v rt        t        ||            ||j                         <   A | j                  |        | j                  dt        |j                               }	| j                          |	|	j!                         dv rt        j                  d       y	 t#        |	      }
d|
cxk  rt        |j                         k  rn n|j%                  |
      S t        j                  d       y# t&        $ r t        j                  d       Y yw xY w)aO  
    Display a selection of titles and prompt the user to choose one.

    Parameters:
        table_show_manager: Manager for console table display.
        media_search_manager: Manager holding the list of media items.

    Returns:
        MediaItem: The selected media item, or None if no selection is made or an error occurs.
    Nz
[red]No media items available.r   Indexcolor   )idtypenamescorer   r   Typer    r   Namer!   r   ScoreT)force_int_inputmax_int_input)qquitz#
[red]Selezione annullata o uscita.z!
[red]Indice errato o non valido.z0
[red]Input non numerico ricevuto dalla tabella.)
media_listconsoleprintavailable_colors__dict__keys
capitalizecolumn_to_hidelenclear
add_column	enumeratestrgetattradd_tv_showrunlowerintget
ValueError)table_show_managermedia_search_managerfirst_media_itemcolumn_infocolor_indexkeyimedia
media_dictlast_command_strselected_indexs              j/home/nidran/Documenti/script/streaming-community/StreamingCommunity/services/_base/site_search_manager.pyget_select_titlerI      sn     ****89+66q9W&6q&9:;KK((--/ >>~-11f}'.&9F#'.	&:F#(/'8G$ .56F{UXYiUjGj6k,lK()1K!$ !!+.2==> 35s1v&
#,,113 	DC~~>1+.wuc/B+CJs~~'(	D 	&&z23 *--dRUVjVuVuRv-w#3#9#9#;}#L<=-.E%9%D%D!EE'++N;; MM>? IJs   :<I 7I I.-I.Nselect_titledownload_film_funcdownload_series_funcr>   r=   
selectionsreturnc                    | st         j                  d       yt        | j                        j	                         dk(  st        | j                        j	                         dk(  srt        | j                        j	                         dk(  sLt        | j                        j	                         dk(  s&t        | j                        j	                         dk(  ro|st         j                  d       yd	}d	}|r"|j                  d
      }|j                  d      } || ||       |r|j                          |r|j                          yt        | j                        j	                         dk(  s&t        | j                        j	                         dk(  r3|st         j                  d       y ||        |r|j                          yt         j                  d| j                          y)a7  
    Handles the search result and initiates the download for either a film or series.
    
    Parameters:
        select_title (MediaItem): The selected media item. Can be None if selection fails.
        download_film_func (callable, optional): Function to download a film
        download_series_func (callable, optional): Function to download a series
        media_search_manager (MediaManager, optional): Manager to clear after processing
        table_show_manager (TVShowManager, optional): Manager to clear after processing
        selections (dict, optional): Dictionary containing selection inputs that bypass manual input
                                    e.g., {'season': season_selection, 'episode': episode_selection}
    
    Returns:
        bool: True if processing was successful, False otherwise
    z1[yellow]No title selected or selection cancelled.Ftvserieovaonashowz;[red]Error: download_series_func not provided for TV seriesNseasonepisodeTfilmmoviez5[red]Error: download_film_func not provided for filmsz[red]Unknown media type: )r*   r+   r5   r   r9   r;   r2   )rJ   rK   rL   r>   r=   rM   season_selectionepisode_selections           rH   base_process_search_resultr[   a   s   $ IJ <##%-\5F5F1G1M1M1OSZ1Z^abnbsbs^t^z^z^|  AF  _F  JM  NZ  N_  N_  J`  Jf  Jf  Jh  lq  Jq  ux  yE  yJ  yJ  uK  uQ  uQ  uS  W]  u]#MMWX )~~h7 *y 9\+;=NO   &&($$& 
\		%	%	'6	1S9J9J5K5Q5Q5SW^5^!MMQR<( $$& 	1,2C2C1DEF    title_search_funcprocess_result_func	site_namestring_to_searchget_onlyDatabasedirect_itemc	                 0   |rt        di |}	 ||	|      }
|
S d}||j                         }n&t        j                  d|       j                         } | |      }|sy|r|S |dkD  rt	        ||      }	 ||	|      }
|
S t
        j                  d|        y)a  
    Generalized search function for streaming sites.
    
    Parameters:
        title_search_func (callable): Function that performs the actual search and returns number of results
        process_result_func (callable): Function that processes the selected result
        media_search_manager (MediaManager): Manager for media search results
        table_show_manager (TVShowManager): Manager for displaying results
        site_name (str): Name of the site being searched
        string_to_search (str, optional): String to search for. Can be passed from run.py.
        get_onlyDatabase (bool, optional): If True, return only the database search manager object.
        direct_item (dict, optional): Direct item to process (bypasses search).
        selections (dict, optional): Dictionary containing selection inputs that bypass manual input
                                     for series (season/episode).
    
    Returns:
        MediaManager if get_onlyDatabase=True, bool otherwise
    Nz+
[purple]Insert a word to search in [green]Fr   z5
[red]Nothing matching was found for[white]: [purple] )r	   stripmsgaskrI   r*   r+   )r]   r^   r>   r=   r_   r`   ra   rb   rM   rJ   resultactual_search_querylen_databases                rH   base_searchrk      s    ,  /;/$\:> #.446!gg(TU^T_&`aggi %%89L  ## a'(:<PQ$\:>NObNcder\   )NNNNN)NFNN)typingr   r   r   r   rich.consoler   rich.promptr   !StreamingCommunity.services._baser	   r
   StreamingCommunity.utilsr   r*   rf   r,   r0   rI   r5   boolr[   r:   rk   rd   r\   rH   <module>rr      s@   1 0 !  F 2 )hQ bGT _c UY=Xi-@ =V^_girhsuxhx_yVz =  Zb  ck  mv  x@  AD  xE  GO  PS  GT  lU  WZ  lZ  c[  Z\ ="<0=MUVcMd= zB  CG  HK  MP  HP  CQ  zR=	=B rv48SE3J#7 4hX`ajXkmuvz{~  AD  |D  wE  nF  XG  IM  XM  ON 4  fr 4  HU 44&.sm4NR4iqrvwz|w  sA  jB4 W_  `d  eh  jm  em  `n  Wo44r\   