o
    sgF                     @   s   d Z ddlZddlZ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
mZmZ ddlmZmZ ddlmZmZ ddlT dd	lmZ dd
lmZ ddlmZ ddlmZ G dd dZdS )+Class to contain the slide creation process    N)Image)RGBColor)MSO_AUTO_SHAPE_TYPE	MSO_SHAPE)
MSO_ANCHORPP_ALIGN)InchesPt)*)SlideResearch)ClientConfig)DownloadImage)GoogleSearchc                   @   sv  e Zd ZdZedZedZdZdZedde	j
edddd	Zd
eddde	j
dZededed edddedddeddddZddeddZedededdZdZdZdZddiZdeddddZddedddeddddZded ddfdefd d!Zddd"d#Zed$d% Zd&d' Zd(d) Zd*d+ Z d,d- Z!d.d/ Z"d0d1 Z#ddd2d3Z$d4d5 Z%d6d7 Z&ddd8d9Z'd:d; Z(d<d= Z)d>d? Z*d@dA Z+dBdC Z,dddDdEZ-dFdG Z.dHdI Z/dJdK Z0dLdM Z1e2	dedNdOZ3e2edddddde	j4fdPdQZ5edRdS Z6edTe7e fdUdVZ8edWdX Z9edYdZ Z:e2d[d\ Z;edfd^ed_e<fd`daZ=e2dbdc Z>dS )gSlideCreatorr   g      +@ffffff?            ?      )height	font_size	alignment
font_color&   r   r   r   r   g      ?g?皙?   )min_chevron_lengthchevron_heightchevron_spacingtext_spacing_from_chevronfont_size_headerfont_size_textfont_color_headerfont_color_text            ?)max_rowsmax_columnsmin_spacing      ?)r   spacing
max_height Nr   )r   r   )r#   r$   r%   r&   Txcmresearchc              
   C   s   || _ || _|| _|| _|| _i d| jd| jd| jd| jd| j	d| j
d| jd| jd	| jd
| jd| j
d| jd| jd| jd| jd| jd| j| j| j| j| j| j| j| j| jd| _dS )zinitialize the classsummary_textbullet_text	icon_texttakeawayicon_circlesolution_diagramtime_line_graph20_token_textgraphgraph_with_texticonographyfull_page_iconographyinfographic	flowchartcircular_process_flowtimelinecentered_timeline)top_timelinethirds_matrix	landscapegridsubtitle_contentgantt_chartvenn_diagramoverlapping_elementsN)projectslideprs_slide_objectclient_configr2   r3   r4   r5   r6   r7   r=   r;   r<   r>   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   item_to_function_mapping)selfrL   rM   conduct_research_flagclientprs_slide_objr2    rU   @/var/www/html/XCapMarket/services/ppt_generator/slide_creator.py__init__U   sb   	
zSlideCreator.__init__c                 C   s   |du rt | j| jtdd}| }|d }	n|}	||d  }|d }||d  }|d }|jtj|||}
| 	|
 |
j
}| j|jd |	| jd | jd | jd	 d
 |S )z
        Add a summary text to the slide
        ppt_slide_object: pptx slide object
        top: int in em for PPT
        left: int in em for PPT
        width: int in em for PPT
        height: int in em for PPT
        text: str
        N+Keep the summary text to 20 tokens or less.rM   primary_dataparsing_classprompt_additioncontentr'   r   r   r   r   r   )r   rM   r2   token20_text_formatshapes	add_shaper   	RECTANGLEset_object_fill_and_line
text_frameadd_text
paragraphssummary_text_properties)rQ   ppt_slide_objecttopleftwidthr   _textslide_researchoutput_datar3   summary_text_objrd   rU   rU   rV   r3      s6   


zSlideCreator.summary_textc                 C   s@   g }t |tr|dS t |tr|D ]
}|| | q|S )zUnpack the bullet text
)
isinstancestrsplitlistextendunpack_bullet_text)clsr4   return_texttextrU   rU   rV   rv      s   


zSlideCreator.unpack_bullet_textc                 C   s   t | j| jtdd}| }|d }t|tr|d}t|tr/t|d t	r/dd |D }d|v r7|d }| 
|}|jtj||||}	| |	 |	j}
tj|
_tj|
_d	|
_t|D ]\}}|dkrl|
jd }n|
 }| j||d
d q^|S )a  
        Add a summary text to the slide
        ppt_slide_object: pptx slide object
        top: int in em for PPT
        left: int in em for PPT
        width: int in em for PPT
        height: int in em for PPT
        text: str
        bullets: list of str
        rX   rY   r]   rp   r   c                 S   s   g | ]}|d  qS )ry   rU   .0irU   rU   rV   
<listcomp>   s    z,SlideCreator.bullet_text.<locals>.<listcomp>ry   Tr   )r   )r   rM   r2   r4   r_   rq   rr   rs   rt   dictrv   r`   ra   r   rb   rc   rd   r   TOPvertical_anchorr   LEFTr   	word_wrap	enumeraterf   add_paragraphre   )rQ   rh   ri   rj   rk   r   rm   rn   bullet_textsbullet_text_objbullet_text_framer|   bulletbullet_prU   rU   rV   r4      s<   




zSlideCreator.bullet_textc           
      C   s   t | j| jtdd}| }|d }|| | jd  }|jtj	|||| jd }	| j
|	tdddtdddd | j|	jjd || jd | jd | jd	 d
 |S )zadd takeaway to the sliderX   rY   r]   r   r   
fill_color
line_colorr   r   r   r   r   r   )r   rM   r2   r^   r_   takeaway_propertiesr`   ra   r   rb   rc   r   re   rd   rf   )
rQ   rh   ri   rj   rk   r   rm   rn   r6   takeaway_objrU   rU   rV   r6      s.   
zSlideCreator.takeawayc                 C   s   t | j| jtdd}| }|d }td}	|jtj	||||	}
| 
|
 | j|
jjd || jd tjd |||	 td ||	 td fS )	zadd subtitle to the sliderX   rY   r]   r   r   r   r   r   g      ?)r   rM   r2   r^   r_   r	   r`   ra   r   rb   rc   re   rd   rf   subtitle_propertiesr   r   )rQ   rh   ri   rj   rk   r   rm   rn   rH   takeaway_obj_heightr   rU   rU   rV   rH     s.   

	zSlideCreator.subtitle_contentc              	   C   s>  t | j| jtdd}| }| ||||||d  ||k r |}|| jd 8 }|}dtj t	|d  }||d  }||d  }t
|d D ]V\}	}
||d t|	|   }||d t|	|   }|jtj||| jd | jd }| j|tdddtddddd	 | j|
d
 ddd }|jj|||| jd}qF|S )a9  
        Add a summary text to the slide
        ppt_slide_object: pptx slide object
        top: int in em for PPT
        left: int in em for PPT
        width: int in em for PPT
        height: int in em for PPT
        text: str
        summary_text_icons: dict of {'text': str, 'icons': list of str}
        z+The icons amount should be between 4 to 10.rY   r]   r'      iconsr-   r   )r   r   
line_widthicon_search_queryTsearch_querysquare_ratio   rk   )r   rM   r2   r7   r_   r3   icon_circle_sizemathpilenr   sincosr`   ra   r   OVALrc   r   search_and_download_imageadd_picture)rQ   rh   ri   rj   rk   r   rm   rn   radsr|   iconxyicon_objicon_locpic_objrU   rU   rV   r7   A  sZ   

	zSlideCreator.icon_circlec                 C   s   t | j| jtdd}| }|d }|d }	|jj jdd|	  7  _t }
t	
|
|d}|D ]I}t }zt	
||d d	\}}W n tjjy^ } z
t| W Y d
}~q2d
}~ww |jj|||||k rk|n|d}||j7 }| j}td}q2|S )zfadd iconography to the slide
        iconograpth is a list [search_query, design_instruction]
        z"This is going to be a single imagerY   r   design_instructionsrp   zDesign instructions for image: imagesimageUrlzimages/iconography.pngNr   r   )r   rM   r2   r=   r_   notes_slidenotes_text_framery   r   asynciorunsearch_googler   	get_imagerequests
exceptionsInvalidSchemaprintr`   r   r   slide_max_widthr	   )rQ   rh   ri   rj   rk   r   rm   rn   r   design_instructiongoogle_search
images_url	image_urldownload_imageimgimg_locerU   rU   rV   r=     sD   


zSlideCreator.iconographyc                 C   s>   t d}|| }|| }|| }|| }| ||||| |S )z&add full page iconography to the slider   )r	   r=   )rQ   rh   ri   rj   rk   r   standard_marginrU   rU   rV   r>     s   z"SlideCreator.full_page_iconographyc              	   C   s  |du rt }t| j| j|d}| }td}	|jtj	||||	}
| j
|
tdddtdddd |d|d }| j|
jjd || jd | jd	 tjd
 |jtj	|||	d  |||	d  }| j
|tdddd | j|jjd |d d t|d  tddddtjd
 t }t|dd}d}| j}td}|D ]"}t }t||d dd }|jj||||d ||7 } |S |S )ztadd graph to the slide
        graph is a list [graph_type, data, data_source, search_query, takeaway_text]
        N)rM   rZ   r[   r   r   r   takeaway_texttitler   r   r   r      r   
graph_typez
Data: datar   r   z	bar chartr   r   images/graph.pngr   )r   )r;   r   rM   r2   r_   r	   r`   ra   r   rb   rc   r   getre   rd   rf   graph_propertiesr   r   jsondumpsr   r   r   r   r   r   r   r   )rQ   rh   ri   rj   rk   r   parser_classrm   
graph_datagraph_header_heightgraph_headergraph_header_text	graph_objr   r   
img_heightr   r   r   rU   rU   rV   r;     st   
	



zSlideCreator.graphc                 C   s$   t d}| |||||| t |S )zadd graph with text to the slide
        graph_with_text is a list [graph_type, data, data_source, search_query, text]
        r   )r	   r;   r<   )rQ   rh   ri   rj   rk   r   text_heightrU   rU   rV   r<     s   	zSlideCreator.graph_with_textc                 C   sx  t | j| jtdd}| }|d }dd |D }dtj t| }	t||t	d }
||d  |
d  }||d  |
d  }|j
tj|||
|
}| j|tdddd	 ||
d  }||
d  }t|D ]\}}|
d
 }||
d t||	   |d  }||
d t||	   |d  }| j|d d dd\}}|j
j||||d}tj|_||	 tjk r|d n| d t	d }|}|| }t	d}t	d}|j
tj||||}| j|tdddd | j|jjd |d | jd | jd tjd |t	d }|| }t	d}t	d}|j
tj||||}| j|tddddd | j|jjd |d | jd | jd tjd qe|S )zt
        add circular process flow to the slide
        circular_process_flow: list of [icon, header, text]
        +The icons amount should be between 5 to 10.rY   r   c                 S   &   g | ]}|d  d |d |d gqS icon_contentr   r   ry   rU   rz   rU   rU   rV   r}   D      z6SlideCreator.circular_process_flow.<locals>.<listcomp>r   r   r   )r       iconTr   r   g      ?r)   r   r   r%   r#   r   )r   r   r&   r$   )r   rM   r2   r@   r_   r   r   r   minr	   r`   ra   r   r   rc   r   r   r   r   r   r   r   auto_shape_typerb   re   rd   rf   process_flow_propertiesr   CENTERr   )rQ   rh   ri   rj   rk   r   rm   rn   rA   r   circle_size
circle_objr|   process	icon_sizer   r   
icon_imager   left_offsettext_top	text_left
text_widthr   text_objrU   rU   rV   rA   4  s   ""



z"SlideCreator.circular_process_flowc                 C   s<  |du rt }t| j| j|dd}| }|d }	dd |	D }	d}
| jd }| jd	 }| jd
 }| jd }|t|	d |  t|	 }|t|	d |  t|	 }|}||k rmd}
|}|}||| d  }||| d  }||k rutd|	D ]}|j	t
j||||}|
dkrdnd|_| j|tdddtdddd | j|jjd |d | jd | jd tjd |
dkr|| | }|}|||  }|d }n|}|| | }|d }|||  }|j	t
j||||}| | tj|j_| j|jjd |d | jd | jd tjd |
dkr||| 7 }qw||| 7 }qw|S )zgadd flowchart to the slide
        flowchart: list of [icon, header limited to 5 tokens, text]
        Nr   rY   r   c                 S   r   r   rU   rz   rU   rU   rV   r}     r   z*SlideCreator.flowchart.<locals>.<listcomp>
horizontalr   r    r!   r"   r   verticalr   z(The flowchart is too large for the slideZ   r   r   r%   r#   r   gQ?r&   r$   )r@   r   rM   r2   r_   flowchart_propertiesr   
ValueErrorr`   ra   r   CHEVRONrotationrc   r   re   rd   rf   r   r   rb   r   r   r   r   )rQ   rh   ri   rj   rk   r   r[   rm   rn   flowchart_dataflowchart_orientationr   r    r!   r"   vertical_chevron_lengthhorizontal_chevron_lengthchevron_widthleft_iflowchevronr   r   r   r   r   rU   rU   rV   r@     s   



	





zSlideCreator.flowchartc                 C      | j |||||td |S )zh
        add timeline to the slide
        timeline list of [icon, timeline_process_name, text]
        r[   r@   rB   rQ   rh   ri   rj   rk   r   rU   rU   rV   rB     s   zSlideCreator.timelinec                 C   s4   ||d  | j d d  }| j|||||td |S )zz
        add centered timeline to the slide
        centered_timeline list of [icon, timeline_process_name, text]
        r   r    r   )r   r@   rB   r   rU   rU   rV   rC     s
   zSlideCreator.centered_timelinec                 C   r   )zadd top timeline to the slider   r   r   rU   rU   rV   rD   (  s   zSlideCreator.top_timelinec              	   C   s   t | j| jtdd}| }|d }t|}| |||}	|	d }
|	d }|	d }g }|D ]*}|| j|d d d	g|| j d |d
 d
g|| j d d |d d| j	gg q-| 
|||||
|| |S )z
        add landscape to the slide
        landscape list of [icon, header, text]
        cell_structures list of [height, item, type of item]
        z+The icons amount should be between 3 to 12.rY   r]   r   r   r'   r   r   r   r   ry   )r   rM   r2   rF   r_   r   define_matrixappendr   default_font_size_bodycreate_matrix)rQ   rh   ri   rj   rk   r   rm   rn   landscape_len
matrix_defcolsmatrix_heightmatrix_widthcell_structurescellrU   rU   rV   rF   0  sR   	

zSlideCreator.landscapec              	   C   s   t | j| jtdd}| }|d }t|}| |||}	|	d }
|	d }|	d }g }|D ]!}||d |d d d	g|d |d
 d
g|d |d dgg q-| |||||
|| |S )zc
        add thirds matrix to the slide
        thirds_matrix list of [icon, header, text]
        z*The icons amount should be between 3 to 9.rY   r]   r   r   r'   r   r   r   r   ry   )	r   rM   r2   rE   r_   r   r   r   r  )rQ   rh   ri   rj   rk   r   rm   rn   
matrix_lenr  r  r  r  r  r  rU   rU   rV   rE   o  sD   

zSlideCreator.thirds_matrixc              	   C   s   |du rt | j| jtdd}| }|d }n|}t|}	| |||	}
|
d }|
d }|
d }g }|D ]}||d |d	 d	g|d |d
 d
g|d |d dgg q4| ||||||| |S )zW
        add grid to the slide
        grid list of [icon, text, search_query]
        Nz*The grid amount should be between 4 to 12.rY   r]   r   r   r'   r(   r   ry   r   image)	r   rM   r2   rG   r_   r   r   r   r  )rQ   rh   ri   rj   rk   r   grid_detailsrm   rn   grid_lenr  r  r  r  r  r  rU   rU   rV   rG     sN   

zSlideCreator.gridc                 C   s  t | j| jtdd}| }|d }t|}td}	t||	|d   | }
t| j	t|
d d }td}t
|D ]L}| j|d d	 d
 dd\}}|jj||||d}|jtj|| | ||| | |
}| | | j|jjd |d d | jtjd ||
|	 7 }q:|S )zK
        add icon and text
        icon_text: list of [icon, text]
        z)Keep the response between 6 to 10 bulletsrY   r]   g?r   r(   r'   r   r   r   Tr   r   r   r4   r   )r   rM   r2   r5   r_   r   r	   intr   r   r   r   r`   r   ra   r   rb   rc   re   rd   rf   default_font_sizer   r   )rQ   rh   ri   rj   rk   r   rm   rn   	icons_lenheight_spacingheight_for_each_bulleticon_heightwidth_spacingr   r   r   r   r   rU   rU   rV   r5     sN   





zSlideCreator.icon_textc                 C   R   |  |}|jtj||||}| | | j|jjd t	
|| jtjd |S )zadd gantt chart to the slider   r   format_input_to_listr`   ra   r   rb   rc   re   rd   rf   r   r   r  r   r   )rQ   rh   ri   rj   rk   r   rI   gantt_chart_objrU   rU   rV   rI        


zSlideCreator.gantt_chartc                 C   r  )zadd venn diagram to the slider   r   r  )rQ   rh   ri   rj   rk   r   rJ   venn_diagram_objrU   rU   rV   rJ   )  r  zSlideCreator.venn_diagramc                 C   r  )z%add overlapping elements to the slider   r   r  )rQ   rh   ri   rj   rk   r   rK   overlapping_elements_objrU   rU   rV   rK   ;  s   


z!SlideCreator.overlapping_elementsc                 C   sN   |r| j   || j j_n| j   |r|| jj_|| j_dS | jj   dS )z?Set the fill and line color of the object. If none, set to noneN)fillsolid
fore_colorrgb
backgroundlinecolorrk   )_objectr   r   r   rU   rU   rV   rc   P  s   


z%SlideCreator.set_object_fill_and_linec                 C   sB   |du r|}|| _ || _t|| j_|| jj_t|| _|| _dS )zadd text to the paragraphN)	ry   levelr
   fontsizer!  r  space_afterr   )
_paragraphry   r   r   r#  r&  r   rU   rU   rV   re   c  s   


zSlideCreator.add_textc              
   C   sH  |D ]}|d }|d dkr/|j tj||||}	| |	 | j|	jjd |d dtj	d nn|d dkrV|j tj||||}	| |	 | j|	jjd |d dtj	d nG|d d	kr|| j
|d d
 ddd }
|j j|
||d  |d  ||d n!|d dkrt|d tur|d g|d< | ||||||d  ||7 }q|S )a  
        add a single cell to the slide
        top = top of the cell
        left = left of the cell
        width = width of the cell
        height = height of the cell
        cell_contents = list of [height, content, content_type (text, icon, image, etc)]
        r   r   ry   r   r   r   r   r   r   r   Tr   r   r
  )r`   ra   r   rb   rc   re   rd   rf   r   r   r   r   typert   multi_image)rw   rh   ri   rj   rk   r   cell_contentscell_contentcell_heightcell_objr   rU   rU   rV   add_single_cellx  sR   





zSlideCreator.add_single_cellr)  c                 C   s"  |}t |dkr
|S || jd | jd   t |kr5t|t | | jd }|}	d}
||d  |	d  }nt| jd | jd }|d | jd  }	d}
|D ]A}|dr\| |d }n| |d }|jj||||	|k ro|	n|d	}||j| jd  7 }|
r||kr||	| jd  7 }|}qM|S )
z add multiple images to the slider   r   r.   r/   Fr   httpr   r   )	r   r   r   
startswithr   r   r`   r   r   )rw   rh   ri   rj   rk   r   r)  top_iimage_heightimage_width
add_column	image_strr
  	image_objrU   rU   rV   r)    s:   
zSlideCreator.multi_imagec              	   C   sv   t |D ]4\}}	|| }
|| }||
|| jd    }|||| jd    }|	du r.td| ||||||	 qdS )a>  
        Create the matrix
        Input:
        ppt_slide_object: pptx slide object
        top: int in em for PPT
        left: int in em for PPT
        cell_structures: list of [height, item, type of item]
        cols: int
        matrix_height: int in em for PPT
        matrix_width: int in em for PPT
        r,   Nzcell_structures is required)r   landscape_propertiesr   r.  )rw   rh   ri   rj   r  r  r  r  r|   cell_structurerowcolr1  r   rU   rU   rV   r    s(   zSlideCreator.create_matrixc           	      C   s   || j d | j d  krtd| dt|}| r&t|}t|}n|dk r/d}|}nt|}t|| }||d | j d   | }||d | j d   | }||||fS )z
        Define the matrix for the landscape
        Input: work area width, height, and the length of the landscape
        Output: number of rows, columns, matrix height, matrix width
        r*   r+   zLandscape of length: z is too large for the slider(   r   r,   )r7  r   r   sqrt
is_integerr  floorceil)	rw   rk   r   r  square_rootrowsr  r  r  rU   rU   rV   r     s0   



zSlideCreator.define_matrixc                 C   s   t | tr| S t | trt|  S t | trLg }| D ].}t |tr,|t|  qt |tr7|| qt |trB|| qtd| d|S td)z format the input value to a listzItem: z is not a list or a dictz#Input value is not a list or a dict)rq   rr   r~   rt   valuesr   r   )input_valueinput_value_unpackeditemrU   rU   rV   r  ;  s    





z!SlideCreator.format_input_to_listFr   r   c                 C   s   t  }t||d}g }|D ]9}| |d \}}|s!||g}|rAt|}|j\}	}
d|	|
   k r9dk rAn n||f  S |sI||f  S q|d |d fS )z
        search and download the image
        Input:
        search_query: str
        square_ratio: bool to only accept square images

        r   r   g?g?r   r   )r   r   r   r   r   r   openr%  )rw   r   r   r   r   first_imager   r   r   rk   r   rU   rU   rV   r   [  s    	

z&SlideCreator.search_and_download_imagec              
   C   sZ   t  }zt|| d\}}||fW S  tjjy, } zt| W Y d}~dS d}~ww )zdownload the imager   N)r   r   r   r   r   r   r   r   )urlr   r   r   r   rU   rU   rV   r   {  s   

zSlideCreator.download_image)N)NNN)F)?__name__
__module____qualname____doc__r	   r   r   r  r   r   r   r   r   rg   r   r7  r   r2   secondary_researchsecondary_research_textr   r   r   r   get_client_configrr   rW   r3   classmethodrv   r4   r6   rH   r7   r=   r>   r;   r<   rA   r@   rB   rC   rD   rF   rE   rG   r5   rI   rJ   rK   staticmethodrc   r   re   r.  rt   r)  r  r   r  boolr   r   rU   rU   rU   rV   r      s    








12
9 #K6
L
ht?
2:<

=/
.
%
r   )rK  r   r   r   osr   PILr   pptx.dml.colorr   pptx.enum.shapesr   r   pptx.enum.textr   r   	pptx.utilr	   r
   7services.ppt_generator.data_classes.slide_layout_models%services.ppt_generator.slide_researchr   utils.client_checkr   utils.download_imager   utils.search_googler   r   rU   rU   rU   rV   <module>   s"    