from shinsapp import SHINSApp
from util.shinsLogger import logger
from util import variables as uvars
from util import exception
from util import functions as ufun
import time

from SHARKNIR_gen_base import SHARKNIR_gen_base

class SHARKNIR_cal_ADC(SHINSApp):
    """
    The task of this template is to apply a fixed rotation of a given
    angle to the ADC for a given number of steps.

    A third parameter will indicate the type of rotation to be applied:
    co-rotatin or contro-rotating

    """

    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):

        logger.info(__name__)
        obsController = self.obsController
        self.status = uvars.EXIT_SUCCESS

        self.gen_base = SHARKNIR_gen_base()
        gen_base = self.gen_base

        fitsDir = uvars.fitsDir + obsController.getCurrentDataDirectory() +'/'
        
        ##########
        # ADC ON #
        ##########
        if(self.paragraph("Turning ON the ADC")):
            
            logger.info("Turning ON the ADCs...")
            ins_s_adc = {"ADC_Mode":"ON"}
            obsController.setupInstrument(ins_s_adc)
            logger.info("Turning ON the ADCs... DONE")
            
        ################
        # LOOP ACTIONS #
        ################

        val = float(ins_s["AMOUNT"])
        steps = int(ins_s["STEPS"])
        adc_offset = ins_s["ADC_Offset"]
        
        time.sleep(20)

        pos_array = [val*s for s in range(-steps//2 + 1 , steps//2 + 1)]

        for pos in pos_array:
            if(self.paragraph("Setting {} at {} deg".format(adc_offset, pos))):
                

                if(adc_offset == "COROT_Offset"):

                    ############################################
                    # APPLYING CO-ROTATION ROTATION TO THE ADC #
                    ############################################
                    logger.info("Waiting for rotation ...")
                    obsController.setAdcCoRotationOffset(pos)
                    logger.info("Waiting for rotation ... DONE")

                elif(adc_offset == "COUNTER_Offset"):

                    ############################################
                    # APPLYING CO-ROTATION ROTATION TO THE ADC #
                    ############################################
                    logger.info("Waiting for rotation ...")
                    obsController.setAdcCounterRotationOffset(pos)
                    logger.info("Waiting for rotation ... DONE")

                else:
                    pass


            time.sleep(10)
                
            ##################
            # EXPOSING SASHA #
            ##################

            gen_base.main(det_s=det_s)

        # End of loop
            
        #####################
        # Reset offset to 0 #
        #####################            
        if(self.paragraph("Reset {} at 0 deg".format(adc_offset))):
            
            time.sleep(10)

            if(adc_offset == "COROT_Offset"):

                ############################################
                # APPLYING CO-ROTATION ROTATION TO THE ADC #
                ############################################
                logger.info("Waiting for rotation ...")
                obsController.setAdcCoRotationOffset(0)
                logger.info("Waiting for rotation ... DONE")

            elif(adc_offset == "COUNTER_Offset"):

                ############################################
                # APPLYING CO-ROTATION ROTATION TO THE ADC #
                ############################################
                logger.info("Waiting for rotation ...")
                obsController.setAdcCounterRotationOffset(0)
                logger.info("Waiting for rotation ... DONE")

            else:
                pass
        
        ###########
        # ADC OFF #
        ###########
        if(self.paragraph("Turning OFF the ADC")):

            logger.info("Turning OFF the ADCs...")
            ins_s_adc = {"ADC_Mode":"OFF"}
            obsController.setupInstrument(ins_s_adc)
            logger.info("Turning OFF the ADCs... DONE")
