from __future__ import print_function

import epics

from helpers.mgnt_utilities import is_string
from .mgnt_auto_checkout_test import AutoCheckoutTest


class ResetCtrlTest(AutoCheckoutTest):
    test_id = 'reset_ctrl'
    description = 'Reset control'
    read_only = True
    deferred = False

    def __init__(self):
        super(ResetCtrlTest, self).__init__(
            pre_checkout_routines=[]
        )

    def execute(self, magnet):
        reset_ctrl_pv = magnet['device'] + ':RESETCTRL'
        reset_ctrl = epics.caget(reset_ctrl_pv, as_string=True)
        if reset_ctrl is None:
            self.data = '{} not found'.format(reset_ctrl_pv)
            self.status = False
            print('FAIL')
            return

        if magnet['psc_type'] == 'mcor' or is_string(magnet):
            true_reset_ctrl = 'Disabled'
        else:
            true_reset_ctrl = 'Enabled'

        if reset_ctrl != true_reset_ctrl:
            self.data = '{} is {} instead of {}'.format(reset_ctrl_pv, reset_ctrl, true_reset_ctrl)
            self.status = False
            print('FAIL')

        else:
            self.data = reset_ctrl
            self.status = True
            print('PASS')
