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,
                                      MagnetConfigTest, OnOffCtrlTest, ResetCtrlTest]
                        },
                        {
                            "name": "Functions",
                            "tests": [StdzEnabledTest, StdzIFinalTest, StdzDirect, TrimEnabledTest, PerturbEnabledTest,
                                      CalbEnabledTest, DegausEnabledTest]
                        },
                        {
                            "name": "Magnet description",
                            "tests": [BarcodeTest, MADNameTest, ZPositionTest, PolyCoefTest]
                        }
                    ]
                },
                {
                    "name": "ACR clients",
                    "subsections": [
                        {
                            "name": "Alarm",
                            "tests": [STATSUMYTest]
                        },
                        {
                            "name": "Save/restore",
                            "tests": [BumplessRebootTest]
                        },
                        {
                            "name": "Archive",
                            "tests": [BDESArchivedPVsTest]
                        },
                        {
                            "name": "SCORE",
                            "tests": [MagnetScoreTest],
                        }
                    ]
                },
                {
                    "name": "EPICS",
                    "subsections": [
                        {
                            "name": "Display",
                            "tests": [MagnetDisplayTest]
                        },
                        {
                            "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, OppositeDirectionToStdzTest]
                        },
                        {
                            "name": "Trim",
                            "tests": [TrimTest]
                        },
                        {
                            "name": "Perturb",
                            "tests": [PerturbTest]
                        },
                        {
                            "name": "Calibrate",
                            "tests": [CalibrateTest]
                        },
                        {
                            "name": "Degauss",
                            "tests": [DegaussTest]
                        }
                    ]
                }
            ]
        }

    def test_iter(self):
        for section in self.structure['sections']:
            for subsection in section['subsections']:
                for test in subsection['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)
                    subsection_dict['tests'].append(result)

                section_dict['subsections'].append(subsection_dict)

            report_dict['sections'].append(section_dict)

        return report_dict
