import pptx
import requests
from io import BytesIO
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE_TYPE
# def edit_slide_0(slide):
#     slide.shapes[1].text_frame.paragraphs[0].runs[-1].text="Test Proposal"

def edit_slide_1(slide):
    slide.shapes[0].text_frame.paragraphs[0].runs[-1].text="Proposal Title"

def edit_slide_3(slide):
    #Listing images
    slide.shapes[0].text_frame.paragraphs[0].runs[0].text="0 "
    #Main images
    slide.shapes[0].text_frame.paragraphs[1].runs[0].text="0 "
    #Organic Lifestyles
    slide.shapes[0].text_frame.paragraphs[2].runs[0].text="0 "
    #Infographics
    slide.shapes[0].text_frame.paragraphs[3].runs[0].text="0 "
    #Premium A+ Content with Video
    slide.shapes[0].text_frame.paragraphs[4].runs[0].text="0 "
    #Commercial video
    slide.shapes[0].text_frame.paragraphs[5].runs[0].text="0 "
    #Instructional video
    slide.shapes[0].text_frame.paragraphs[6].runs[0].text="0 "
    #Sponsored Ads Video
    slide.shapes[0].text_frame.paragraphs[7].runs[0].text="0 "
    

def edit_slide_5(slide):
    #Insights
    slide.shapes[6].text_frame.paragraphs[1].runs[0].text="This is a long text test paragraph. This is a long text test paragraph. This is a long text test paragraph."
    slide.shapes[6].text_frame.paragraphs[1].runs[1].text=""
    slide.shapes[6].text_frame.paragraphs[1].runs[2].text=""
    slide.shapes[6].text_frame.paragraphs[1].runs[3].text=""
    slide.shapes[6].text_frame.paragraphs[1].runs[4].text=""
    slide.shapes[6].text_frame.paragraphs[1].runs[5].text=""
    slide.shapes[6].text_frame.paragraphs[1].runs[6].text=""
    slide.shapes[6].text_frame.paragraphs[1].runs[7].text=""
    # Gender
    test_image_path = "src/static/ppt/test_tortoise.jpg"

    
    img_shape = slide.shapes[12]
    replace_image(img_shape,test_image_path)
    


def edit_slide_6(slide):
    #Insights
    #Age
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[10]

    replace_image(img_shape,test_image_path)

    
    #Marital Status
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[11]

    replace_image(img_shape,test_image_path)

    #Education
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[12]

    replace_image(img_shape,test_image_path)


def edit_slide_7(slide):
    #Review Insights
    slide.shapes[8].text_frame.paragraphs[3].runs[0].text="Competitor ASINs: ASINTEST"
    
def edit_slide_8(slide, new_img):
    #Word Cloud Positive
    #test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[14]

    replace_image(img_shape,new_img)

def edit_slide_9(slide, df):
    #Top Words Bigram, Trigram Positive 

    tables = [
        (0.91, 'TOP WORDS', 'Word', df[0]),
        (3.71, 'BIGRAMS', 'Bigram', df[1]),
        (6.51, 'TRIGRAMS', 'Trigram', df[2])
    ]

    for left, tittle, word_column, df in tables:

        rows, cols = 21, 2
        left = Inches(left)
        top = Inches(1.39)
        width = Inches(2.79)
        height = Inches(3.63)

        table = slide.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 = Inches(0.05)

    #Highlights
    #slide.shapes[10].text_frame.paragraphs[0].runs[1].text="Test highlight, test highlight, test test"
    
def edit_slide_10(slide, new_img):
    #Word Cloud Negative
    #test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[13]

    replace_image(img_shape,new_img)

def edit_slide_11(slide, df):
    #Top Words Bigram, Trigram Negative
    
    tables = [
        (0.91, 'TOP WORDS', 'Word', df[0]),
        (3.71, 'BIGRAMS', 'Bigram', df[1]),
        (6.51, 'TRIGRAMS', 'Trigram', df[2])
    ]

    for left, tittle, word_column, df in tables:
        rows, cols = 21, 2
        left = Inches(left)
        top = Inches(1.39)
        width = Inches(2.79)
        height = Inches(3.63)

        table = slide.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 = Inches(0.05)

    #Highlights
    #slide.shapes[10].text_frame.paragraphs[0].runs[1].text="Test highlight, test highlight, test test"

def edit_slide_12(slide, sentences):

    slide.shapes[12].text_frame.paragraphs[0].runs[0].text = sentences

def edit_slide_14(slide, urls):

    #Insights Benchmark Analysis
    
    #slide.shapes[7].text_frame.paragraphs[0].runs[0].text="Here's a picture of the most commonly searched TARGET PRODUCT, so we can compare them for improvements that will help us get a better overall approach and use a differentiating factor."


    #Commonly searched Target Product 1
    img_data = requests.get(urls[0]).content
    
    img_shape = slide.shapes[13]

    replace_image(img_shape, BytesIO(img_data))

    #Commonly searched Target Product 2
    img_data = requests.get(urls[1]).content
    
    img_shape = slide.shapes[14]

    replace_image(img_shape, BytesIO(img_data))

    img_data = requests.get(urls[2]).content
    
    img_shape = slide.shapes[15]

    replace_image(img_shape, BytesIO(img_data))


def edit_slide_15(slide, proposal):
    '''
    main_images=["Example test text for Main image 1",
        "Example test text for Main image 2",
        "Example test text for Main image 3",
   ]
    
    #Suggested Image List (Main)

    for index,paragraph in enumerate(slide.shapes[0].text_frame.paragraphs):
        if index<=len(main_images)-1:
            paragraph.runs[0].text=f"Main Image #{index}: "
            paragraph.runs[1].text=main_images[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''
    '''

    slide.shapes[0].text_frame.paragraphs[0].runs[0].text = proposal
   

def edit_slide_16(slide, urls_img):

    #Reference Moodboard

    #Main Image 1
    test_image_path = requests.get(urls_img[0]).content
    
    img_shape = slide.shapes[0]

    replace_image(img_shape,BytesIO(test_image_path))
    '''
    #Main Image 2
    test_image_path = requests.get(urls_img[1]).content
    
    img_shape = slide.shapes[10]

    replace_image(img_shape,BytesIO(test_image_path))

    #Main Image 3
    test_image_path = requests.get(urls_img[2]).content
    
    img_shape = slide.shapes[11]

    replace_image(img_shape,BytesIO(test_image_path))

    #Main Image 4
    test_image_path = requests.get(urls_img[3]).content
    
    img_shape = slide.shapes[12]

    replace_image(img_shape,BytesIO(test_image_path))

    #Main Image 5
    test_image_path = requests.get(urls_img[4]).content
    
    img_shape = slide.shapes[13]

    replace_image(img_shape,BytesIO(test_image_path))
    '''
   
def edit_slide_17(prs, lifestyleProposal):
    '''
    lifestyle_images=["Example test text for lifestyle image 1",
        "Example test text for lifestyle image 2",
   ]
    
    #Suggested Image List (Lifestyle)

    for index,paragraph in enumerate(slide.shapes[0].text_frame.paragraphs):
       
        if index<=len(lifestyle_images)-1:

            paragraph.runs[0].text=f"Lifestyle #{index}: "
            paragraph.runs[1].text=lifestyle_images[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''
    '''
    proposals = lifestyleProposal.split("Infographic #5:")
    proposals[1] = "Infographic #5:" + proposals[1]
    prs.slides[16].shapes[0].text_frame.paragraphs[0].runs[0].text = proposals[0]
    prs.slides[17].shapes[0].text_frame.paragraphs[0].runs[0].text = proposals[1]

def edit_slide_18(slide, urls_img):

    #Reference Moodboard

    #Lifestyle Image 1
    test_image_path = requests.get(urls_img[0]).content
    
    img_shape = slide.shapes[7]

    replace_image(img_shape,BytesIO(test_image_path))
    '''
    #Lifestyle Image 2
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[8]

    replace_image(img_shape,test_image_path)

    #Lifestyle Image 3
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[9]

    replace_image(img_shape,test_image_path)

    #Lifestyle Image 4
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[10]

    replace_image(img_shape,test_image_path)
    '''
   

def edit_slide_16_17(prs, infographicProposal):

        proposals = infographicProposal.split("Infographic #3:")
        proposals[1] = "Infographic #3:" + proposals[1]
        prs.slides[18].shapes[0].text_frame.paragraphs[0].runs[0].text = proposals[0]
        prs.slides[19].shapes[0].text_frame.paragraphs[0].runs[0].text = proposals[1]

def edit_slide_19(slide):
    infographics=["Example test text for Infographic 3",
        "Example test text for Infographic 4", ]
    
    #Suggested Image List (Infographic)

    for index,paragraph in enumerate(slide.shapes[0].text_frame.paragraphs):
        
        if index<=len(infographics)-1:
            paragraph.runs[3].text=infographics[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_20(slide):

    #Infographics

    #Infographics Image 1
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[9]

    replace_image(img_shape,test_image_path)

    #Infographics Image 2
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[10]

    replace_image(img_shape,test_image_path)

    #Infographics Image 3
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[11]

    replace_image(img_shape,test_image_path)

    #Infographics Image 4
    test_image_path = "src/static/ppt/test_tortoise.jpg"
    
    img_shape = slide.shapes[12]

    replace_image(img_shape,test_image_path)


def edit_slide_22(slide):
    module_list_a_plus=["Example test text for Module 1",
        "Example test text for Module 2", ]
    
    #Suggested Module List for A+

    for index,paragraph in enumerate(slide.shapes[8].text_frame.paragraphs):
        
        if index<=len(module_list_a_plus)-1:
            paragraph.runs[1].text=module_list_a_plus[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_23(slide):
    module_list_a_plus=["Example test text for Module 3(Video)",]
    
    #Suggested Module List for A+

    for index,paragraph in enumerate(slide.shapes[8].text_frame.paragraphs):
        
        if index<=len(module_list_a_plus)-1:
            paragraph.runs[1].text=module_list_a_plus[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_24(slide):
    module_list_a_plus=["Example test text for Module 4",
                        "Example test text for Module 5",]
    
    #Suggested Module List for A+

    for index,paragraph in enumerate(slide.shapes[8].text_frame.paragraphs):
        
        if index<=len(module_list_a_plus)-1:
            paragraph.runs[1].text=module_list_a_plus[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_25(slide):
    module_list_a_plus=["Example test text for Module 6",
                        "Example test text for Module 7",]
    
    #Suggested Module List for A+

    for index,paragraph in enumerate(slide.shapes[8].text_frame.paragraphs):
        
        if index<=len(module_list_a_plus)-1:
            paragraph.runs[1].text=module_list_a_plus[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_27(slide):
    comm_video_scenes=["Example test text for Scene 1",
                        "Example test text for Scene 2",
                        "Example test text for Scene 3",]
    
    #Commercial Video

    for index,paragraph in enumerate(slide.shapes[7].text_frame.paragraphs):
        
        if index<=len(comm_video_scenes)-1:
            paragraph.runs[1].text=comm_video_scenes[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_28(slide):
    comm_video_scenes=["Example test text for Scene 4",
                        "Example test text for Scene 5",
                        "Example test text for Scene 6",]
    
    #Commercial Video

    for index,paragraph in enumerate(slide.shapes[7].text_frame.paragraphs):
        
        if index<=len(comm_video_scenes)-1:
            paragraph.runs[1].text=comm_video_scenes[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''

def edit_slide_29(slide):
    comm_video_scenes=["Example test text for Scene 7",
                       ]
    
    #Commercial Video

    for index,paragraph in enumerate(slide.shapes[7].text_frame.paragraphs):
        
        if index<=len(comm_video_scenes)-1:
            paragraph.runs[1].text=comm_video_scenes[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''
def edit_slide_30(slide):
    inst_video_scenes=["Example test text for Scene 1",
                       "Example test text for Scene 2",
                       "Example test text for Scene 3",
                       "Example test text for Scene 4",
                       ]
    
    #Instructional Video

    for index,paragraph in enumerate(slide.shapes[7].text_frame.paragraphs):
        
        if index<=len(inst_video_scenes)-1:
            paragraph.runs[1].text=inst_video_scenes[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''
def edit_slide_31(slide):
    ad_video_1=["Example test text for Scene 1",
                       "Example test text for Scene 2",
                       "Example test text for Scene 3",
                       "Example test text for Scene 4",
                       "Example test text for Scene 5",
                       "Example test text for Scene 6",
                       ]
    
    #Sponsored Ads Video 1

    for index,paragraph in enumerate(slide.shapes[7].text_frame.paragraphs):
        
        if index<=len(ad_video_1)-1:
            paragraph.runs[1].text=ad_video_1[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''
def edit_slide_32(slide):
    ad_video_2=["Example test text for Scene 1",
                       "Example test text for Scene 2",
                       "Example test text for Scene 3",
                       "Example test text for Scene 4",
                       "Example test text for Scene 5",
                       "Example test text for Scene 6",
                       ]
    
    #Sponsored Ads Video 2

    for index,paragraph in enumerate(slide.shapes[7].text_frame.paragraphs):
        
        if index<=len(ad_video_2)-1:
            paragraph.runs[1].text=ad_video_2[index]
        else:
            for run in paragraph.runs[::-1]:
                run.text = ''


def replace_image(img_shape,image_path):
    # Create a new image part from the new image file
    new_pptx_img = pptx.parts.image.Image.from_file(image_path)

    # Get the part and rId from the shape we need to change
    slide_part, rId = img_shape.part, img_shape._element.blip_rId

    # Get the image part
    image_part = slide_part.related_part(rId)

    # Overwrite the old blob info with the new blob info
    image_part.blob = new_pptx_img._blob



#Function to help find order of objects in slides
def find_objects(slide):
    objects_desc = []
    for shape in slide.shapes:
        if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
            objects_desc.append("Picture: {}".format(shape.image.filename))
        elif shape.shape_type == MSO_SHAPE_TYPE.CHART:
            objects_desc.append("Chart")
        elif shape.shape_type == MSO_SHAPE_TYPE.TABLE:
            objects_desc.append("Table")
        elif shape.shape_type == MSO_SHAPE_TYPE.TEXT_BOX or shape.shape_type == MSO_SHAPE_TYPE.PLACEHOLDER:
            text = shape.text_frame.text if shape.text_frame else "Empty Text Box"
            objects_desc.append("Text Box: '{}'".format(text))
        else:
            objects_desc.append("Shape Type: {}".format(shape.shape_type))
    
    print("...".join(objects_desc)) 