import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from tests import *


class AutoCheckoutStructure:
    def __init__(self):
        self.structure = {
            "sections": [
                {
                    "name": "Network",
                    "subsections": [
                        # {
                        #     "name": "Triggers",
                        #     "tests": [TriggerPVTest, TriggerDisplayTest]
                        # },
                        {
                            "name": "DHCP",
                            "tests": [DHCPConfTest]
                        },
                        {
                            "name": "Ethernet",
                            "tests": [EthernetPingTest, TSPingTest, ACSWPingTest]
                        },
                        {
                            "name": "Serial",
                            "tests": [TSScreenIOCsTest]
                        },
                        {
                            "name": "TS port configuration",
                            "tests": [TSPortTest, TSPortLabelTest]
                        },
                        # {
                        #     "name": "Nodename",
                        #     "tests": [NsLookupTest, NodePingTest]
                        # },
                        {
                            "name": "ACSW",
                            "tests": [ACSWPlugName, ACSWPowerTest]
                        }
                    ]
                },
                {
                    "name": "Database comparison (Oracle vs. EPICS)",
                    "subsections": [
                        {
                            "name": "Polynomial database",
                            "tests": [PSCurrentRangeTest, CurrentRangeTest, BFieldRangeTest, PSTypeTest, PSDeviceTest,
                                      IntlkSumyOutTest, MagnetConfigTest, OnOffCtrlTest, ResetCtrlTest]
                        },
                        {
                            "name": "Functions",
                            "tests": [StdzEnabledTest, StdzDirect, TrimEnabledTest, PerturbEnabledTest,
                                      CalbEnabledTest, DegausEnabledTest]
                        },
                        {
                            "name": "Magnet description",
                            "tests": [BarcodeTest, MADNameTest, ZPositionTest, PolyCoefTest]
                        }
                    ]
                },
                {
                    "name": "ACR clients",
                    "subsections": [
                        {
                            "name": "Alarm",
                            "tests": [STATMSGTest]
                        },
                        {
                            "name": "Save/restore",
                            "tests": [BumplessRebootTest]
                        },
                        {
                            "name": "Archive",
                            "tests": [ArchivedPVsTest]
                        },
                        {
                            "name": "SCORE",
                            "tests": [MagnetScoreTest],
                        }
                    ]
                },
                {
                    "name": "EPICS",
                    "subsections": [
                        {
                            "name": "Display",
                            "tests": [MagnetDisplayTest, MagnetPyDMDisplayTest]
                        },
                        {
                            "name": "CCI",
                            "tests": [MagnetScreenIOCsTest, MagnetIOCDataTest, IOCHeartbeatTest]
                        },
                        {
                            "name": "Wildcard control",
                            "tests": [AllTypeSelectTest, AllStarSelectTest]
                        },
                        {
                            "name": "Model PVs",
                            "tests": [EDESTest, EACTTest, ECONTest, BLEMTest]
                        }
                    ]
                },
                {
                    "name": "Functionality",
                    "subsections": [
                        {
                            "name": "In service",
                            "tests": [InServiceTest]
                        },
                        {
                            "name": "Turn PS off",
                            "tests": [DirectPowerOffTest, CtrlPowerOffTest]
                        },
                        {
                            "name": "Turn PS on",
                            "tests": [DirectPowerOnTest, CtrlPowerOnTest]
                        },
                        {
                            "name": "Standardize",
                            "tests": [StandardizeTest]
                        },
                        {
                            "name": "Trim",
                            "tests": [TrimTest]
                        },
                        {
                            "name": "Perturb",
                            "tests": [PerturbTest]
                        },
                        {
                            "name": "Calibrate",
                            "tests": [CalibrateTest]
                        },
                        {
                            "name": "Degauss",
                            "tests": [DegaussTest]
                        }
                    ]
                }
            ]
        }

    def test_iter(self, test_id, skip_test_id):
        deferred_tests = []
        for section in self.structure['sections']:
            for subsection in section['subsections']:
                for test in subsection['tests']:
                    if (not skip_test_id or test.test_id not in skip_test_id) and (
                            not test_id or test.test_id in test_id):
                        if test.deferred:
                            deferred_tests.append(test)
                        else:
                            yield test()

        for test in deferred_tests:
            yield test()

    def generate_report_dict(self, checkout_data):
        report_dict = {
            'element': checkout_data.get_element(),
            'device': checkout_data.get_device(),
            'sections': []
        }
        for section in self.structure['sections']:
            section_dict = {
                'name': section['name'],
                'subsections': []
            }
            for subsection in section['subsections']:
                subsection_dict = {
                    'name': subsection['name'],
                    'tests': []
                }
                for test in subsection['tests']:
                    result = checkout_data.get_latest_result(test.test_id)
                    if result is None:
                        print('Warning: {} was not run for {}'.format(test.__name__, checkout_data.get_element()))
                    else:
                        subsection_dict['tests'].append(result)

                section_dict['subsections'].append(subsection_dict)

            report_dict['sections'].append(section_dict)

        return report_dict
