from modules.svgcreator import SvgCreator
import pandas as pd

class SvgCreatorModel:
    arr_errors = []
    def __init__(self, svg_path, excel_path, output_path):
        self.svg_path = svg_path
        self.excel_path = excel_path
        self.output_path = output_path
        
    
    def extract_excel(self):
        df = pd.read_excel(self.excel_path)
        # Iterate over rows and columns to print each cell
        for index, row in df.iterrows():
            svg = row["motiv"]
            tx1 = row["tx1"]
            num = str(row["num"]).split('.')[0]
            tx2 = row["tx2"]
            tx3 = row["tx3"]
            tx4 = row["tx4"]
            
            # Check for NaN values and replace them with None
            svg = svg if pd.notna(row["motiv"]) else None
            tx1 = tx1 if pd.notna(row["tx1"]) else None
            num = num if pd.notna(row["num"]) else None
            tx2 = tx2 if pd.notna(row["tx2"]) else None
            tx3 = tx3 if pd.notna(row["tx3"]) else None
            tx4 = tx4 if pd.notna(row["tx4"]) else None
            
            if(svg == None):
                continue
            self.create_svg(svg, tx1, tx2, tx3, tx4, num)
        
        self.write_error_excel()
        self.arr_errors = []

    
    def write_error_excel(self):
        df = pd.DataFrame(self.arr_errors)
        df.to_excel(self.output_path+"/errors.xlsx")

    
    def create_svg(self, svg, tx1, tx2, tx3, tx4, num):
        obj = SvgCreator(self.svg_path+"/"+svg+".svg")
        if obj.svg_path == 0:
            print("Pfad existiert nicht")
            print(self.svg_path+"/"+svg+".svg")
            tuple_error = [svg, "Pfad existiert nicht"]
            self.arr_errors.append(tuple_error)

            
        allow_exp = True
        try:
            if not None == tx1:
                tx1 = tx1.split("(")[0]
                if tx1[-1] == " ":
                    tx1 = tx1[:-1]

                obj.update_text("tx1", tx1)
            if not None == tx2:
                tx2 = tx2.split("(")[0]
                if tx2[-1] == " ":
                    tx2 = tx2[:-1]

                obj.update_text("tx2", tx2)
            if not None == tx3:
                tx3 = tx3.split("(")[0]
                if tx3[-1] == " ":
                    tx3 = tx3[:-1]

                obj.update_text("tx3", tx3)
            if not None == tx4:
                tx4 = tx4.split("(")[0]
                if tx4[-1] == " ":
                    tx4 = tx4[:-1]

                obj.update_text("tx4", tx4)
            if not None == num:
                num = num.split("(")[0]
                if num[-1] == " ":
                    num = num[:-1]
                          
                obj.update_text("num", num)
        except Exception as e:
            tuple_error = [svg, e]
            self.arr_errors.append(tuple_error)
            allow_exp = False

        if allow_exp:
            try:
                obj.export_svg(self.output_path+"/", svg+" "+tx1, 300)
            except Exception as e:
                tuple_error = [svg, "Export fehlgeschlagen"]
                self.arr_errors.append(tuple_error)
                allow_exp = False
            

