
    i                         d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ  e       Zdededee   dee   d	ed
dfdZddededededee   d
dfdZy)    )CallableAnyOptional)Console)Prompt)manage_selectionvalidate_selectiondisplay_episodes_listdisplay_seasons_listscrape_serieseasons_countseason_selectionepisode_selectiondownload_episode_callbackreturnNc                    |dk(  rt         j                  d       y|t        | j                        }d}n|}d}t         j                  d|        | j                  j                  }|D cg c]  }|j
                   }	}|rt        |      n|	rt        |	      n|}
t        ||
      }g }|D cg c]  }|j
                   }	}|D ]j  }d|cxk  rt        |      k  r%n n"|j                  ||dz
     j
                         <||	v r|j                  |       Rt         j                  d| d	       l |s4t         j                  d
t        |       d|	        |rt        ||	      }ny|D ]_  }| j                  j                  |      }|st         j                  d| d|	        <t        |      dkD  xs |dk(  } ||||s|nd       a yc c}w c c}w )a  
    Process season selection and trigger episode downloads.
    
    Parameters:
        - scrape_serie: Scraper object with series information
        - seasons_count (int): Total number of seasons
        - season_selection (str, optional): Pre-defined season selection
        - episode_selection (str, optional): Pre-defined episode selection
        - download_episode_callback (Callable): Function to call for downloading episodes
    r   z%[red]No seasons found for this seriesNTFz0
[cyan]Using provided season selection: [yellow]   z[yellow]Warning: Selection z4 is neither a valid index nor a valid season number.z5[red]No valid seasons selected. Available indices: 1-, Available numbers: z[red]Season z not found! Available seasons: *)season_numberdownload_allr   )consoleprintr   seasons_managerseasonsnumberlenmaxr   appendr	   get_season_by_number)r   r   r   r   r   index_season_selectedis_manual_inputseasons_listsavailable_numbers	max_countlist_selectionlist_season_selectvalr   seasonr   s                    j/home/nidran/Documenti/script/streaming-community/StreamingCommunity/services/_base/tv_download_manager.pyprocess_season_selectionr,      s    =>  4\5Q5Q R 0IJZI[\]  //77L+78a88 &5L!Uf3?P;QlyI%&;YGN +78a88 
s (s<((%%l3q5&9&@&@A %%%%c*MM7u<pqr
s McR^N_M``u  wH  vI  J  	K!3NDU!V , 
--BB=QMML7VWhVijk -.2R6Ks6R 	"'%7C/	

= 9 9s   ,G/Gr!   download_video_callbackr   c                    |j                  |       }t        |      }|dk(  rt        j                  d|         y|r>t	        d|dz         D ]  } || |      \  }}	|	s n t        j                  d|  d       y|t        |      }
n|}
t        j                  d|        t        |d      D cg c]3  \  }}t        |t              r|j                  d      nt        |d|      5 }}}t        t        |      t        |D cg c]  }t        |t        t        f      s| c}xs dg            }	 t        |
|      }g }|D ]  }d|cxk  rt        |      k  rn n|j                  |       ,d
}t        |d      D ]M  \  }}t        |t              r|j                  d      nt        |dd      }||k(  s:|j                  |       d	} n |rt        j                  d| d        |rn=t        j                  dt        |       d|        |yt!        j"                  d      }
|D ]  } |||dz
     | |      \  }}	|	s y yc c}}w c c}w )a  
    Handle downloading episodes for a specific season.
    
    Parameters:
        - index_season_selected (int): Season number
        - scrape_serie: Scraper object with series information
        - download_video_callback (Callable): Function to call for downloading individual videos
        - download_all (bool): Whether to download all episodes
        - episode_selection (str, optional): Pre-defined episode selection
    r   z"[red]No episodes found for season Nr   z*
[red]End downloaded [yellow]season: [red].z1
[cyan]Using provided episode selection: [yellow]r   TFz#[yellow]Warning: Episode selection z5 is neither a valid index nor a valid episode number.z6[red]No valid episodes selected. Available indices: 1-r   z+[red]Enter valid episode numbers or indices)getEpisodeSeasonsr   r   r   ranger
   	enumerate
isinstancedictgetgetattrr   intfloatr   r   r   ask)r!   r   r-   r   r   episodesepisodes_count	i_episodepathstoppedlast_commandidxepavailable_episode_numbersnm_countr'   list_episode_selectr)   foundep_nums                        r+   process_episode_downloadrH   ]   s    --.CDH]N:;P:QRSq.1"45 	I34I9UMD'		 	CDYCZZ[\] $0:L,LMMNO`Nabc
 %Xq1%
R !+2t 4BFF8'"hPS:TT%
! %
 c(mS5N)nR\]^adfk`lRm!)n)ustru%vw-lGDN #%% H ,s8},'..s3 "E#,Xq#9 "R5?D5I!1wWY[ceiOj!S=/66s;$(E!" !(KC5  QF  'G  H#H& #MMRSVW_S`Raav  xQ  wR  S  T ,!::&STLA F - 	I3HYq[4IK`bklMD'		W%
 *os   28II!I)FN)typingr   r   r   rich.consoler   rich.promptr   4StreamingCommunity.services._base.tv_display_managerr   r	   r
   r   r   r7   strr,   boolrH        r+   <module>rQ      s    + * !  S  S )G
3 G
s G
V^_bVc G
  yA  BE  yF G
  ck G
  pt G
TTC Ts Tem T  ~B T  _g  hk  _l T  x| TrP   