import pandas as pd
import os

ROOT_PATH = '/Users/vegardjervell/Documents/7_semester/irrev_prosjekt/'
DATA_PATH = ROOT_PATH + 'data/'

def comment_to_doc():
    with open('text.txt', 'r') as file:
        lines = file.readlines()
        for i in range(len(lines)):
            lines[i] = lines[i].replace('_','\_')
            lines[i] = lines[i].strip().split(':')

    with open('latex.txt', 'w') as file:
        for line in lines:
            file.write('\param{'+line[0]+':}{'+line[1]+'\\\}\n')

def excel_to_table(filename, compname):
    data = pd.read_excel(DATA_PATH + filename)

    with open('tex_table_'+filename+'.txt', 'w') as file:
        file.write('\\toprule\n')
        linestr = '    \\backslashbox{'+compname+'}{T [\si{\celsius}]}'
        for T in data.columns:
            linestr += ' & '+str(T)
        linestr += ' \\\ \n'

        linestr += '\midrule \n'
        for i, row in data.iterrows():
            for r in row[0:1]:
                linestr += '    '+str(r)
            for r in row[1:]:
                if str(r) == 'nan':
                    linestr += ' '
                else:
                    linestr += ' & ' + str(r)
            linestr += ' \\\ \n'
        linestr += '\\bottomrule'

        file.write(linestr)

def files_to_table(folder):
    linestr = '\\toprule\n'
    firstfile = True
    for filename in os.listdir(DATA_PATH + folder):
        data = pd.read_excel(DATA_PATH + folder +'/'+ filename)
        if firstfile:
            linestr += '    \\backslashbox{Species}{x [-]}'
            for c in data['c']:
                linestr += ' & ' + str(c)
            linestr += '\\\ \n\\midrule \n'
            firstfile = False
        linestr += '    '+filename.split('_')[0]
        if filename.split('_')[-2] == 'hexane':
            linestr += '$^2$'
        if filename.split('_')[-2] == 'heptane':
            linestr += '$^1$'
        for st in data['ST']:
            linestr += ' & '+str(st)
        linestr += '\\\ \n'
    linestr += '\\bottomrule'

    with open('298_files.txt', 'w') as file:
        file.write(linestr)
