import pptx
import requests

from pptx import Presentation
from io import BytesIO

class ProposalPPT:

	def __init__(
		self,
		file_name,
		positive_wordcloud,
		benchmark_images,
		positive_insights_table,
		negative_wordcloud,
		negative_insights_table,
		main_image_content,
		main_image,
		lifestyle_content,
		lifestyle_image,
		infographic_content,
		infographic_image,
		a_plus_content,
		listing_video_content,
		premium_a_plus_content,
		premium_a_plus_video_content,
		sponsored_ads_content,
		brand_story_content
	):
		self.prs = Presentation("src/resources/templates/proposal_template.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.benchmark_images = benchmark_images
		self.main_image_content = main_image_content
		self.main_image = main_image
		self.lifestyle_content = lifestyle_content
		self.lifestyle_image = lifestyle_image
		self.infographic_content = infographic_content
		self.infographic_image = infographic_image
		self.a_plus_content = a_plus_content
		self.listing_video_content = listing_video_content
		self.premium_a_plus_content = premium_a_plus_content
		self.premium_a_plus_video_content = premium_a_plus_video_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()

		if self.negative_wordcloud:
			self.modify_negative_insights_wordcloud()
			self.modify_negative_insights_table()

		self.modify_benchmark_images()

		if self.main_image_content:
			self.modify_main_image_content()
			self.modify_main_image()
			self.modify_lifestyle_content()
			self.modify_lifestyle_image()
			self.modify_infographic_content()
			self.modify_infographic_image()

		if self.a_plus_content: 
			self.modify_a_plus_content()
			self.modify_premium_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_benchmark_images(self):

		url_images = {
			12: self.benchmark_images[0],
			13: self.benchmark_images[1],
			14: self.benchmark_images[2],
		}

		for shape, url in url_images.items():
			self.replace_image(
				self.prs.slides[9].shapes[shape], 
				BytesIO(requests.get(url).content)
			)

	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_main_image(self):

		self.replace_image(
			self.prs.slides[12].shapes[9], 
			BytesIO(requests.get(self.main_image).content)
		)

	def modify_lifestyle_content(self):

		if self.lifestyle_content.find("Lifestyle Image #5:") == -1:
			self.prs.slides[13].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[13].shapes[0].text_frame.paragraphs[0].runs[0].text = content[0]
			self.prs.slides[14].shapes[0].text_frame.paragraphs[0].runs[0].text = content[1]

	def modify_lifestyle_image(self):

		self.replace_image(
			self.prs.slides[15].shapes[0], 
			BytesIO(requests.get(self.lifestyle_image).content)
		)

	def modify_infographic_content(self):

		if self.infographic_content.find("Infographic #3:") == -1:
			self.prs.slides[16].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[16].shapes[0].text_frame.paragraphs[0].runs[0].text = content[0]
			self.prs.slides[17].shapes[0].text_frame.paragraphs[0].runs[0].text = content[1]

	def modify_infographic_image(self):
	
		self.replace_image(
			self.prs.slides[18].shapes[0], 
			BytesIO(requests.get(self.infographic_image).content)
		)

	def modify_a_plus_content(self):

		self.prs.slides[19].shapes[8].text_frame.paragraphs[0].runs[0].text = self.a_plus_content

	def modify_listing_video_content(self):

		self.prs.slides[21].shapes[7].text_frame.paragraphs[0].runs[0].text = self.listing_video_content

	def modify_premium_a_plus_content(self):

		self.prs.slides[23].shapes[7].text_frame.paragraphs[0].runs[0].text = self.premium_a_plus_content

	def modify_premium_a_plus_video_content(self):

		self.prs.slides[25].shapes[7].text_frame.paragraphs[0].runs[0].text = self.premium_a_plus_video_content

	def modify_sponsored_ads_video_content(self):

		self.prs.slides[27].shapes[7].text_frame.paragraphs[0].runs[0].text = self.sponsored_ads_content

	def modify_brand_story_content(self):

		self.prs.slides[29].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