from io import StringIO, BytesIO
import unittest

import numpy as np
import pandas as pd
from backend import his_io, moodle_io


class TestMoodleio(unittest.TestCase):

    def check_csv_table(self, table):
        pass

    def test(self):
        exercise_columns = ['F 1 /4_0', 'F 2 /6_0', 'F 3 /4_0', 'F 4 /4_0',
       'F 5 /4_0', 'F 6 /7_0', 'F 7 /10_0', 'F 8 /5_0', 'F 9 /4_0',
       'F 10 /9_0', 'F 11 /8_0', 'F 12 /8_0']
        
        with open('RocketScience-Bewertungen.csv', 'r') as f:
            self.table_str = StringIO(f.read())

        with self.subTest('read'):
            points_df_moodle = moodle_io.read_csv(self.table_str)
            self.assertIsNotNone(points_df_moodle)

        
        with self.subTest('check_table'):
            self.assertEqual(list(points_df_moodle.columns), 
                             ['E-Mail-Adresse', 'Status', 'Begonnen', 'Beendet', 'Dauer', 'Bewertung/73_0'] + exercise_columns)
            self.assertEqual(points_df_moodle.shape[0], 6)
            self.assertEqual(points_df_moodle.index.names, ['Nachname', 'Vorname'])
            self.assertEqual(list(points_df_moodle.index), [('Hendrix','Jimi'), ('Lukather','Steve') , ('Mayer','John'), ('Page','Jimmy'), ('Townshend','Pete'), ('Van Halen','Eddie')])
            self.assertEqual(list(points_df_moodle[exercise_columns].dtypes), ['float64'] * len(list(points_df_moodle[exercise_columns].dtypes)))

        with self.subTest('parse_header'):
            max_points, exercises = moodle_io.parse_header(points_df_moodle)
            self.assertEqual(max_points, 73)
            self.assertEqual(exercises, exercise_columns)

        with self.subTest('merge_tables'):
            table_bytes = None
            with open('5160-RocketScience-WiSe_2023.xlsx', 'rb') as f:
                table_bytes = BytesIO(f.read())

            students_table = None
            students_table, _ = his_io.read_excel(table_bytes)
            self.assertIsNotNone(students_table)

            for col in exercise_columns:
                students_table[col] = pd.Series(np.nan, index=students_table.index, dtype='float64')

            students_table, merged_count = moodle_io.merge_points(students_table, points_df_moodle)
            self.assertEqual(merged_count, 6)
            self.assertEqual(students_table.index.name, 'Matrikelnummer')

            self.assertEqual(list(students_table.loc[3434343][exercise_columns]), [3.0, 6.0, 2.0, 4.0, 2.5, 4.0, 1.0, 4.0, 0.0, 4.0, 7.0, 2.0])
            self.assertEqual(list(students_table.loc[3535353][exercise_columns]), [3.0, 6.0, 2.0, 2.0, 1.5, 2.0, 3.0, 1.0, 0.0, 2.0, 2.0, 2.0])
            self.assertEqual(list(students_table.loc[3131313][exercise_columns]), [3.0, 6.0, 3.0, 4.0, 3.0, 5.0, 6.0, 5.0, 4.0, 7.0, 8.0, 8.0])
            self.assertEqual(list(students_table.loc[2323232][exercise_columns]), [3.0, 4.0, 1.0, 1.0, 2.0, 0.5, 0.0, 5.0, 0.0, 8.0, 4.0, 2.0])
            self.assertEqual(list(students_table.loc[3737373][exercise_columns]), [2.0, 5.0, 4.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0, 2.5, 2.0, 1.0])
            self.assertEqual(list(students_table.loc[3939393][exercise_columns]), [2.0, 5.0, 4.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0, 2.5, 2.0, 1.0])



if __name__ == '__main__':
    unittest.main()