import sys
import pptx
import requests

sys.path.insert(0, './src/helpers')

from pptx import Presentation
from io import BytesIO

from processWords import NgramAnalysis
from processImage import CreateDocumentPPT

class ProposalPPT:

	def __init__(
		self,
		file_name,
		positive_wordcloud,
		positive_insights_table,
		negative_wordcloud,
		negative_insights_table,
		main_image_content,
		lifestyle_content,
		infographic_content,
		a_plus_content,
		listing_video_content,
		premium_a_plus_content,
		sponsored_ads_content,
		brand_story_content
	):
		self.prs = Presentation("src/resources/templates/proposal_template_test.pptx")
		self.file_name = file_name
		self.positive_wordcloud = positive_wordcloud
		self.positive_insights_table = positive_insights_table
		self.negative_wordcloud = negative_wordcloud
		self.negative_insights_table = negative_insights_table
		self.main_image_content = main_image_content
		self.lifestyle_content = lifestyle_content
		self.infographic_content = infographic_content
		self.a_plus_content = a_plus_content
		self.listing_video_content = listing_video_content
		self.premium_a_plus_content = premium_a_plus_content
		self.sponsored_ads_content = sponsored_ads_content
		self.brand_story_content = brand_story_content

	def generate_ppt(self):

		self.modify_positive_insights_wordcloud()
		self.modify_positive_insights_table()
		self.modify_negative_insights_wordcloud()
		self.modify_negative_insights_table()

		if self.main_image_content:
			self.modify_main_image_content()
			self.modify_lifestyle_content()
			self.modify_infographic_content()

		if self.a_plus_content: 
			self.modify_a_plus_content()
			self.modify_premium_a_plus_video_content()

		if self.listing_video_content: 
			self.modify_listing_video_content()

		if self.sponsored_ads_content: 
			self.modify_sponsored_ads_video_content()

		if self.brand_story_content:
			self.modify_brand_story_content()

		self.delete_empty_slides()

		self.prs.save(f"storage/proposals/{self.file_name}.pptx")

	def modify_tittle(self):

		self.prs.slides[1].shapes[0].text_frame.paragraphs[0].runs[0].text

	def modify_positive_insights_wordcloud(self):

		self.replace_image(self.prs.slides[5].shapes[14], self.positive_wordcloud) 

	def modify_positive_insights_table(self):

		self.replace_table(self.prs.slides[6], self.positive_insights_table)

	def modify_negative_insights_wordcloud(self):

		self.replace_image(self.prs.slides[7].shapes[13], self.negative_wordcloud)

	def modify_negative_insights_table(self):

		self.replace_table(self.prs.slides[8], self.negative_insights_table)

	def modify_main_image_content(self):

		if self.main_image_content.find("Main Image #6:") == -1:
			self.prs.slides[10].shapes[0].text_frame.paragraphs[0].runs[0].text = self.main_image_content  
		else:
			content = self.main_image_content.split("Main Image #6:")
			content[1] = "Main Image #6:" + content[1]
			self.prs.slides[10].shapes[0].text_frame.paragraphs[0].runs[0].text = content[0]
			self.prs.slides[11].shapes[0].text_frame.paragraphs[0].runs[0].text = content[1]

	def modify_lifestyle_content(self):

		if self.lifestyle_content.find("Lifestyle Image #5:") == -1:
			self.prs.slides[12].shapes[0].text_frame.paragraphs[0].runs[0].text = self.lifestyle_content
		else:
			content = self.lifestyle_content.split("Lifestyle Image #5:")
			content[1] = "Lifestyle Image #5:" + content[1]
			self.prs.slides[12].shapes[0].text_frame.paragraphs[0].runs[0].text = content[0]
			self.prs.slides[13].shapes[0].text_frame.paragraphs[0].runs[0].text = content[1]

	def modify_infographic_content(self):

		if self.infographic_content.find("Infographic #3:") == -1:
			self.prs.slides[14].shapes[0].text_frame.paragraphs[0].runs[0].text = self.infographic_content  
		else:
			content = self.infographic_content.split("Infographic #3:")
			content[1] = "Infographic #3:" + content[1]
			self.prs.slides[14].shapes[0].text_frame.paragraphs[0].runs[0].text = content[0]
			self.prs.slides[15].shapes[0].text_frame.paragraphs[0].runs[0].text = content[1]

	def modify_a_plus_content(self):

		self.prs.slides[16].shapes[8].text_frame.paragraphs[0].runs[0].text = self.a_plus_content

	def modify_listing_video_content(self):

		self.prs.slides[18].shapes[7].text_frame.paragraphs[0].runs[0].text = self.listing_video_content

	def modify_premium_a_plus_video_content(self):

		self.prs.slides[20].shapes[7].text_frame.paragraphs[0].runs[0].text = self.premium_a_plus_content

	def modify_sponsored_ads_video_content(self):

		self.prs.slides[22].shapes[7].text_frame.paragraphs[0].runs[0].text = self.sponsored_ads_content

	def modify_brand_story_content(self):

		self.prs.slides[24].shapes[7].text_frame.paragraphs[0].runs[0].text = self.brand_story_content

	def delete_empty_slides(self):

		xml_slides	= self.prs.slides._sldIdLst 
		slides = list(xml_slides)

		for index, slide in enumerate(self.prs.slides):
			for shape in slide.shapes:
				if shape.shape_type == 17 and shape.text.find('*') != -1:
					xml_slides.remove(slides[index])
					break

	def replace_table(self, location, data_frames):
		
		tables = [
			(0.91, 'TOP WORDS', 'Word', data_frames[0]),
			(3.71, 'BIGRAMS', 'Bigram', data_frames[1]),
			(6.51, 'TRIGRAMS', 'Trigram', data_frames[2])
		]

		for left, tittle, word_column, df in tables:
			rows, cols = 21, 2
			left = pptx.util.Inches(left)
			top = pptx.util.Inches(1.39)
			width = pptx.util.Inches(2.79)
			height = pptx.util.Inches(3.63)

			table = location.shapes.add_table(rows, cols, left, top, width, height).table

			table.cell(0, 0).merge(table.cell(0, 1))
			table.cell(0, 0).text = tittle
			table.cell(1, 0).text = 'Word'
			table.cell(1, 1).text = 'Frequency'

			for i, (word, freq) in enumerate(zip(df[word_column], df['Frequency']), start=1):
				table.cell(i, 0).text = str(word)
				table.cell(i, 1).text = str(freq)
				if i == 20: break

			for i in range(0, 21):
				for y in range(0, 2):
					table.cell(i, y).text_frame.paragraphs[0].font.size = pptx.util.Inches(0.05)

	def replace_image(self, location, image):

	    new_pptx_img = pptx.parts.image.Image.from_file(image)
	    slide_part, rId = location.part, location._element.blip_rId
	    image_part = slide_part.related_part(rId)
	    image_part.blob = new_pptx_img._blob


words = ['as', 'b', 'c', 'b', 'b', 'as', 'g']
	
processWords = NgramAnalysis(words)
bestMonograms = processWords.extract_common_words_across_all_texts()
bestBigrams = processWords.extract_common_bigrams_across_all_texts()
bestTrigrams = processWords.extract_common_trigrams_across_all_texts()

processImage = CreateDocumentPPT()
positiveWordCloud = processImage.add_wordcloud_to_slide(bestMonograms)

proposal_ppt = ProposalPPT(
	'prueba', positiveWordCloud, (bestMonograms, bestBigrams, bestTrigrams), positiveWordCloud, 
	(bestMonograms, bestBigrams, bestTrigrams), '1', '2', '3', '4', '5', '6', None, None
)
proposal_ppt.generate_ppt()