# SPDX-License-Identifier: GPL-3.0-only
# (C) Copyright CERN 2021. All rights not expressly granted are reserved. 

import glob
import os
import random
import shutil
import unittest
import pandas
import PIL
import numpy as np

from availsim4core import main

class test_fancy_exporter(unittest.TestCase):
    param_scenario = [
        ["./availsim4core/test/E2E/input/fancy_exporter/simulation.xlsx",
         "./availsim4core/test/E2E/input/fancy_exporter/system.xlsx",
         "./availsim4core/test/E2E/input/fancy_exporter/theoretical_output.xlsx"],
    ]

    def test_runner(self):
        # randomize the order of the test execution to detect hidden dependencies
        random.shuffle(self.param_scenario)
        for simulation_file, system_file, theoretical_result_file in self.param_scenario:
            with self.subTest():
                self._runner_E2E(simulation_file,
                                 system_file,
                                 theoretical_result_file)

    def _runner_E2E(self,
                    simulation_file,
                    system_file,
                    theoretical_result_file):

        output_folder = "./availsim4core/test/E2E/output/fancy_exporter/"

        # Clean folder
        if os.path.exists(output_folder):
            shutil.rmtree(output_folder)

        # Run the main process
        main.start(simulation_file, system_file, None, output_folder)

        result_simulation_file_list = glob.glob(output_folder + "/*.xlsx")

        for sheet_name in ['RESULTS_COMPONENT_TREE_EXTENDED','RESULTS_COMPONENT_TREE_SIMPLE',
                           'RESULTS_CONNECTIVITY_MATRIX', 'RESULTS_COMPONENT_LISTING',
                           'RESULTS_CRITICAL_FAILURE_PATHS']:
            actual_output = pandas.read_excel(result_simulation_file_list[0], sheet_name=sheet_name, engine='openpyxl')
            theoretical_output = pandas.read_excel(theoretical_result_file, sheet_name=sheet_name, engine='openpyxl')

            self.assertTrue(actual_output.equals(theoretical_output))

        # compare the RESULTS_GRAPH
        ## TODO:
        ## equality TEST not passing the CI, the picture have slightly different layout, I don't know why...
        #
        #theoretical_png = np.array(PIL.Image.open("./availsim4core/test/E2E/input/fancy_exporter/theoretical_tree.png"))
        #actual_png = np.array(PIL.Image.open(result_simulation_file_list[0].split('.xlsx')[0]+'.png'))
        #self.assertTrue(np.array_equal(theoretical_png, actual_png))
        #
        #theoretical_linux_png = np.array(PIL.Image.open("./availsim4core/test/E2E/input/fancy_exporter/theoretical_tree_linux.png"))
        #actual_linux_png = np.array(PIL.Image.open(result_simulation_file_list[0].split('.xlsx')[0]+'_linux.png'))
        #self.assertTrue(np.array_equal(theoretical_linux_png, actual_linux_png))


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