import csv
from os import path
from typing import List
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
from data import *
from algorithms.Util import Util

from data.InputRow import InputRow
from data.OutputRow import OutputRow
from data.CsvHelper import CsvHelper
from itertools import groupby
from algorithms.ResultsViewModel import ResultsViewModel
from htmlgenerator.ResultsGenerator import ResultsGenerator



"""
Given the absolute path to the CSV file with input images, 
returns the absolute path to the file which contains the output images
"""
def find_results_csv_from_input_csv(inputfilename:str):
    results_file=os.path.dirname(inputfilename)+".results\\result_image.csv"
    if (os.path.exists(results_file)==False):
        return None
    return results_file

def generate_html_filepath_from_input(inputfilename:str):
    folder_output=os.path.join(os.path.dirname(__file__),"out")
    dir_name=os.path.basename(os.path.dirname(inputfilename))
    return os.path.join(folder_output,f"{dir_name}-result.html")

def generate_report_for_input_csv(input_images_csvfile:str):
    input_image_folder=os.path.dirname(input_images_csvfile)
    results_csv_file=find_results_csv_from_input_csv(inputfilename=input_images_csvfile)
    if (results_csv_file == None):
        print(f"\tNo results csv found for {Util.display_leaf_folders_from_path(path=input_image_folder,count=3)}")        
        return

    input_rows=CsvHelper.read_input_rows_from_csv(filename=input_images_csvfile)
    result_rows=CsvHelper.read_result_rows_from_csv(filename=results_csv_file)

    view_model=ResultsViewModel(inputrows=input_rows, outputrows=result_rows, inputfolder=os.path.dirname(input_images_csvfile), resultsfolder=os.path.dirname(results_csv_file))
    html_file=generate_html_filepath_from_input(inputfilename=input_images_csvfile)

    report_generator = ResultsGenerator(viewmodel=view_model,filename=html_file)
    report_generator.generate_report()

    # #The code below exemplifies the structural validity
    pass

def main():
    print("This script will search for input CSV files generated by Salt-Pepper script")
    output_folder=os.path.join(os.path.dirname(__file__),"Out/")    
    input_csv_files=glob.glob(output_folder+"/**/*input*.csv",recursive=True)
    print("Found %d files" % (len(input_csv_files)))
    if (len(input_csv_files) == 0):
        print("No CSV files found with input images. Quitting")
        return

    for file_index in range(0,len(input_csv_files)):
        input_csv_file=input_csv_files[file_index]
        print(f"\t{Util.display_leaf_folders_from_path(path=input_csv_file, count=3)}")

    print("Going to generate friendly report")
    for file_index in range(0,len(input_csv_files)):
        input_csv_file=input_csv_files[file_index]
        print(f"\tProcessing file...{Util.display_leaf_folders_from_path(path=input_csv_file, count=3)}")
        generate_report_for_input_csv(input_images_csvfile=input_csv_file)
        print("----------------------------")

    pass

if __name__ == "__main__":
    main()
