


# PPT calculator: 4767pptCalculator.csv

import pandas as pd
import numpy as np
import os

filename = r"C:\GlobalSino2\ICs\4767pptPreparationBridge.csv"
SpecialListA = r"C:\GlobalSino2\ICs\4767SpecialListA.csv"
SpecialListB = r"C:\GlobalSino2\ICs\4767SpecialListB.csv"

NewFileName = "Luke"

# Steps for defining JobNumber and JobSubNumber: Step 1> if the csv has it then take them from csv file;
# if the csv file does not have it, then take the names from the folder names.
# Read Pandas csv file with no header
df = pd.read_csv(filename, header=None)

for i in range(1, len(df)):
    print("Preparing pptx slides for: ", df.values[i][0])
    if df.values[i][0] == NewFileName:
        theFileDirectory = df.values[i][1]
        print("Working slides are for the directory: ", theFileDirectory)
        print("\n")
        dfPPTXconfig = pd.read_csv(theFileDirectory, header=None)
        for j in range(1, len(dfPPTXconfig)):
            print(dfPPTXconfig.values[j][0], "|and| ", dfPPTXconfig.values[j][1])
            if pd.isnull(dfPPTXconfig.values[j][1]) == False: 
                print(dfPPTXconfig.values[j][0], j, "is not none")
                # Check if the iterm is in the csv file or not ================
                if "NewFile" == str(dfPPTXconfig.values[j][0]):
                    NewFile = dfPPTXconfig.values[j][1]
                    if NewFile.lower() == "yes":
                        ImageFolder = str(dfPPTXconfig.values[j][2])
                if "ReportTitle" == str(dfPPTXconfig.values[j][0]):
                    ReportTitle = dfPPTXconfig.values[j][1]
                if "ImageFolder" == str(dfPPTXconfig.values[j][0]):
                    ImageFolder = dfPPTXconfig.values[j][1]
                if "Analyst" == str(dfPPTXconfig.values[j][0]):
                    Analyst = dfPPTXconfig.values[j][1]
                if "SlideLayout" == str(dfPPTXconfig.values[j][0]):
                    SlideLayout = dfPPTXconfig.values[j][1]
                if "JobNumber" == str(dfPPTXconfig.values[j][0]):
                    JobNumber = dfPPTXconfig.values[j][1]
                if "JobSubNumber" == str(dfPPTXconfig.values[j][0]):
                    JobSubNumber = dfPPTXconfig.values[j][1]
                if "JobDescription" == str(dfPPTXconfig.values[j][0]):
                    JobDescription = dfPPTXconfig.values[j][1]
                if "Rotation" == str(dfPPTXconfig.values[j][0]):
                    Rotation = int(dfPPTXconfig.values[j][1])              
                if "SpecialList" == str(dfPPTXconfig.values[j][0]):
                    SpecialList = dfPPTXconfig.values[j][1]
                if "ReportLinkA" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkA = dfPPTXconfig.values[j][1]
                if "ReportLinkB" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkB = dfPPTXconfig.values[j][1]
                if "ReportLinkC" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkC = dfPPTXconfig.values[j][1]
                if "ReportLinkD" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkD = dfPPTXconfig.values[j][1]
                if "ReportLinkE" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkE = dfPPTXconfig.values[j][1]
                if "ReportLinkF" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkF = dfPPTXconfig.values[j][1]
                if "ReportLinkG" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkG = dfPPTXconfig.values[j][1]
                if "ReportLinkH" == str(dfPPTXconfig.values[j][0]):
                    ReportLinkH = dfPPTXconfig.values[j][1]

# Get the immediate directories of the new files
listPathsA = [fA.path for fA in os.scandir(ImageFolder) if fA.is_dir()]
for iA in range(len(listPathsA)):
    # the subfolders
    print("listPathsA[iA] ", listPathsA[iA])
    from pathlib import Path
    FolderOfTheParentJob = Path(listPathsA[0]).name
    print("FolderOfTheParentJob", FolderOfTheParentJob)
    print("ImageFolder is ", ImageFolder)
    subFolderOfJobs = ""
    listPathsB = [fB.path for fB in os.scandir(listPathsA[iA]) if fB.is_dir()]
    for iB in range(len(listPathsB)):
        # the subfolders
        print("listPathsB[iB] ", listPathsB[iB])
        from pathlib import Path
        fileB = Path(listPathsB[iB])
        subFolderOfJobs = subFolderOfJobs + fileB.name + " "
        print("subFolderOfJobs is ", subFolderOfJobs)
print("FolderOfTheParentJob is", FolderOfTheParentJob)
print("subFolderOfJobs is ", subFolderOfJobs)

# take the names from the folder names
for i in range(1, len(df)):
    print("Preparing pptx slides for: ", df.values[i][0])
    if df.values[i][0] == NewFileName:
        theFileDirectory = df.values[i][1]
        print("Working slides are for the directory: ", theFileDirectory)
        print("\n")
        dfPPTXconfig = pd.read_csv(theFileDirectory, header=None)
        for j in range(1, len(dfPPTXconfig)):
            print(dfPPTXconfig.values[j][0], "|and| ", dfPPTXconfig.values[j][1])
            if pd.isnull(dfPPTXconfig.values[j][1]) == True: 
                print(dfPPTXconfig.values[j][0], j, "is not none")
                if "JobNumber" == str(dfPPTXconfig.values[j][0]):
                    JobNumber = FolderOfTheParentJob
                if "JobSubNumber" == str(dfPPTXconfig.values[j][0]):
                    JobSubNumber = subFolderOfJobs
                if "Analyst" == str(dfPPTXconfig.values[j][0]):
                    Analyst = ""
                if "SpecialList" == str(dfPPTXconfig.values[j][0]):
                    SpecialList = ""
    
print("\nParameters for powerpoint preparation are:")
print("NewFile is '",NewFile, "(existing)'")
print("ReportLinkA is '",ReportLinkA, "(existing)'")
print("ImageFolder is '",ImageFolder, "(existing)'")
print("JobNumber is ", JobNumber)
print("JobSubNumber is ", JobSubNumber)
print("The Rotation is ", Rotation)
print("SpecialList is", SpecialList) 
print("\n")

# Prepare the first PowerPintSlide
import page4767ppt2 as firstPPTslide
firstPPTslide.firstSlide(JobNumber, JobSubNumber, JobDescription, Analyst)

# List all folders which has files ending with .jpg
fileFolderList = []
for root, dirs, files in os.walk(ImageFolder):
    for file in files:
        filePath = os.path.join(root, file)
        if filePath.endswith(".jpg") == True:
            if root not in fileFolderList:
                fileFolderList.append(root)
print("fileFolderList is ", fileFolderList)

dfSpecialListA = pd.read_csv(SpecialListA, header=None)
print(dfSpecialListA)

print("len(dfSpecialListA) is", len(dfSpecialListA))

TheSpecialListNeededForReport = []
for iSpecialListA in range(1, len(dfSpecialListA)):
    print("Preparing a list of images for ppt preparation")
    print("dfSpecialListA.values[iSpecialListA][0] is", dfSpecialListA.values[iSpecialListA][0])
    TheSpecialListNeededForReport.append(dfSpecialListA.values[iSpecialListA][0])
print("TheSpecialListNeededForReport is", TheSpecialListNeededForReport)
    
dfSpecialListB = pd.read_csv(SpecialListB, header=None)
print(dfSpecialListB)
if SpecialList.lower() == "yes":
    for iSpecialListB in range(1, len(dfSpecialListB)):
        print("dfSpecialListB.values[iSpecialListB][0] is", dfSpecialListA.values[iSpecialListB][0])
        TheSpecialListNeededForReport.append(dfSpecialListB.values[iSpecialListB][0])
print("TheSpecialListNeededForReport is", TheSpecialListNeededForReport)

print("\n")

for ifileFolderList in range(0, len(fileFolderList)):
    print("fileFolderList is", fileFolderList)
    actualFileListInTheCurrentFolder = []    
    # =================
    for iImagesForReport, eachImagesForReport in enumerate(os.listdir(fileFolderList[ifileFolderList])):
        if eachImagesForReport.endswith(".jpg") == True:
            print(eachImagesForReport)
            actualFileListInTheCurrentFolder.append(eachImagesForReport)    
    FileListToSendToPPTpreparation = []
    # The lines below may be not needed (the original idea is to check the match between the need list and the actual files).
    for jImage in range(0, len(actualFileListInTheCurrentFolder)):
        print("File name, actualFileListInTheCurrentFolder[jImage], is", actualFileListInTheCurrentFolder[jImage].split(".")[0])
        if actualFileListInTheCurrentFolder[jImage].split(".")[0] in TheSpecialListNeededForReport:
            FileListToSendToPPTpreparation.append(actualFileListInTheCurrentFolder[jImage].split(".")[0])
    print("The folders (fileFolderList[ifileFolderList]), containing data images, are ", fileFolderList[ifileFolderList])
    print("FingerPrint actualFileListInTheCurrentFolder", actualFileListInTheCurrentFolder)
    print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    import page4767ppt3
    page4767ppt3.SlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, fileFolderList[ifileFolderList], Rotation)

    
