from mgnt_auto_checkout_test import AutoCheckoutTest
from tests.mgnt_get_poly_from_epics import GetPolyFromEPICS


class CalbEnabledTest(AutoCheckoutTest):
    test_id = 'calb_enabled'
    description = 'Calibrate enabled'
    read_only = True
    deferred = False

    def __init__(self):
        super(CalbEnabledTest, self).__init__(
            pre_checkout_routines=[GetPolyFromEPICS]
        )

    def execute(self, magnet):
        epics_data = GetPolyFromEPICS().data[magnet['element']]
        calb_disable_pv = magnet['device'] + ':BARCODE'
        calb_disable = epics_data['CALBDISABLE']
        if calb_disable is None:
            self.data = '{} not found'.format(calb_disable_pv)
            self.status = False
            print 'FAIL'

        elif calb_disable:
            if magnet['psc_type'] == 'EPSC':
                self.data = 'PS config is {} but {} is {}'.format(magnet['ps_config'], calb_disable_pv, calb_disable)
                self.status = False
                print 'FAIL'

            else:
                self.data = 'Disabled'
                self.status = True
                print 'PASS'

        else:
            if magnet['psc_type'] != 'EPSC':
                self.data = 'PS config is {} but {} is {}'.format(magnet['ps_config'], calb_disable_pv, calb_disable)
                self.status = False
                print 'FAIL'

            else:
                self.data = 'Enabled'
                self.status = True
                print 'PASS'
