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

# Other templates
from SHARKNIR_gen_Preset import SHARKNIR_gen_Preset
from SHARKNIR_gen_tec_PupilApoAlignment import SHARKNIR_gen_tec_PupilApoAlignment
from SHARKNIR_gen_tec_PupilLyotAlignment import SHARKNIR_gen_tec_PupilLyotAlignment
from SHARKNIR_gen_tec_PSFAlignment_lab import SHARKNIR_gen_tec_PSFAlignment_lab 
from SHARKNIR_gen_base import SHARKNIR_gen_base

class SHARKNIR_gen_acq(SHINSApp):

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

        logger.info(__name__)
        logger.info('Telescope preset template begin ... ')

        self.status = uvars.EXIT_SUCCESS
        obsController = self.obsController

        gen_preset = SHARKNIR_gen_Preset()
        gen_tec_PupilApoAlignment = SHARKNIR_gen_tec_PupilApoAlignment()
        gen_tec_PupilLyotAlignment = SHARKNIR_gen_tec_PupilLyotAlignment()
        gen_tec_PSFAlignment = SHARKNIR_gen_tec_PSFAlignment()
        self.gen_base = SHARKNIR_gen_base();        gen_base = self.gen_base
        
        ####################
        # TELESCOPE PRESET #
        ####################

        logger.debug("Enumerating telescope setup")
        for element, value in tel_s.items():
            logger.debug("Element : % 15s  -  Value % 15s    " % (element, value))

        logger.info("Preset telescope ... ")
        gen_tec_Preset.main(tel_s)
        logger.info("Preset telescope ... DONE ")

        #########################
        # Choose alignment type #
        #########################

        align_ref_det = {
            "NCOADDS": 1,
            "NGROUPS": 1,
            "NDROPS": 0,
            "READOUT": "Center",
            "NDIT": 1,
        }

        align_ref_ins = {
            "CORO_SLIT_W": "WINDOW",
            "PUPIL_LENS_DEP": "IN"
        }

        #########################
        # CHECK!!! LAB TEST!!!! #
        #########################

        LAB_TEST = False
        if not LAB_TEST:
            
            ####################
            # Choose alignment #
            ####################

            if ins_s["CORO_SLIT_W"] in ["SP1_FPM", "SP2a_FPM", "SP2b_FPM"]:

                DOME_CLOSED = True
                if not DOME_CLOSED:
                    #################
                    # Apo Alignment #
                    #################
                    
                    align_ref_ins["CAL_FILTER_DEP"] = "OUT"
                    align_ref_ins["CAL_FF_LAMP"] = "OFF"
                    align_ref_ins["CAL_MIRROR_DEP"] = "OUT"
                    align_ref_ins["LYOT_GRISM_W"] = "HOLE"
                    
                    ins_a = ins_s.copy()
                    ins_a.update(align_ref_ins)
                    
                    logger.info("Recalling gen_tec_PupilApoAlignment")
                    self.status, pupilfile, self.other = gen_tec_PupilApoAlignment.main(ins_a, align_ref_det)
                    logger.debug(self.status, pupilfile)
            
            elif ins_s["CORO_SLIT_W"] in ["FQPM", "GAUSS"]:
            
                #################################
                # Lyot Alignment FOR CI and LSS #
                #################################
            
                align_ref_det["NREADS"] = 1
                align_ref_ins["CAL_FIBER_DEP"] = "NO_FIBERS"
                align_ref_ins["CAL_FIBER_FOCUS_LAMP"] = "OFF"
                align_ref_ins["APODIZER_W"] = "HOLE"
                align_ref_ins["SCI_FILT_W2"] = "HOLE"
            
                ins_a = ins_s.copy()
                ins_a.update(align_ref_ins)
            
                logger.info("Recalling gen_tec_PupilLyotAlignment")
                self.status, pupilfile, self.other = gen_tec_PupilLyotAlignment.main(ins_a, align_ref_det_s)
                logger.debug(self.status, pupilfile)
            
            else:
            
                #########################
                # Lyot Alignment for DI #
                #########################
            
                align_ref_det["NREADS"] = 1
                align_ref_ins["CAL_FIBER_DEP"] = "NO_FIBERS"
                align_ref_ins["CAL_FIBER_FOCUS_LAMP"] = "OFF"
                align_ref_ins["APODIZER_W"] = "HOLE"
                align_ref_ins["SCI_FILT_W2"] = "HOLE"
            
                align_ref_ins["GAUSS_LYOT_STOP"] = "GAUSS"
            
                ins_a = ins_s.copy()
                ins_a.update(align_ref_ins)
            
                logger.info("Recalling gen_tec_PupilLyotAlignment")
                self.status, pupilfile, self.other = gen_tec_PupilLyotAlignment.main(ins_a, align_ref_det_s)
                logger.debug(self.status, pupilfile)

        # #################
        # # PSF Alignment #
        # #################

        # psf_ref_det = {
        #     "READOUT": "128x128",
        # }
        
        # psf_ref_ins = {
        #     "LYOT_GRISM_W" : "HOLE",
        #     "CAL_FILTER_DEP": "OUT",
        #     "CAL_FIBER_FOCUS_LAMP": "OFF", # sara' off a LBT
        #     "PUPIL_LENS_DEP": "OUT"
        # }

        # det_p = det_s.copy()
        # det_p.update(psf_ref_det)
        # ins_p = ins_s.copy()
        # ins_p.update(psf_ref_ins)
        
        # logger.info("Recalling gen_tec_PSFAlignment_lab")
        # self.status, psffile, self.other = gen_tec_PSFAlignment.main(ins_p, det_p)
        # logger.debug("gen_tec_PSFAlignment exit with status {} and file {}".format(self.status, psffile))


        # ###################################
        # # START AND WAIT INSTRUMENT SETUP #
        # ###################################

        # gen_base.main(ins_s=ins_s, **kwargs)
