

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

from SHARKNIR_gen_base import SHARKNIR_gen_base

class testOB_davide(SHINSApp):

    @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;

        ###########################
        ##### Repeat the test #####
        ###########################

        if self.paragraph("zero"):
            time.sleep(1)

        if self.paragraph("uno"):
            time.sleep(1)

        ##################
        # PAUSE / RESUME #
        ##################

        test_idl = ufun.test_idl_bridges()
        print(test_idl)
        
        #self.paused = True
        logger.warning("Resume if you agree to apply; Quit otherwise.")
        print("(R)esume, (Q)uit")

        
        import sys
        from select import select
        
        timeout = 2
               
        answer = None
        print("Enter something:"),
        self.paused = True
        
        
        while self.paused==True and self.aborted==False:

            rlist, _, _ = select([sys.stdin], [], [], timeout)
            if rlist:
                answer = sys.stdin.readline()
                print(answer)
            else:
                print("still waiting...")

            if answer:
                if answer.lower().startswith('r'):
                    self.paused = False
                    pass
                if answer.lower().startswith('q'):
                    self.paused = False
                    self.aborted = True
                    self.abort()
                    return
                else:
                    pass

        # reserved = [0,0]
        # zeros = [0,0,0,0]
        # modes = [0.05, 0]
        
        # logger.info("Upload a mode vector to internal DM")
        # self.status = obsController.RTCTTModeUpload(reserved+zeros+modes, absolute=True)
        # logger.info("Waiting setup to apply displacement... DONE")

        # ########################
        # # Save and apply flat #
        # ########################
        # if self.paragraph("Save and apply flat"):
            
        #     from util import master_listener_davide
            
        #     thread = master_listener_davide.PLOT_THREAD()
        #     thread.start()
        #     thread.RUN = True
        #     print("plot thread started")
            
        #     flat_name = ufun.save_flat(self.obsController, thread)
        #     logger.info("Save DM flat file... DONE")
        #     logger.info(flat_name)
            
        #     thread.CLOSE = True
        #     thread.RUN = False
        #     print("plot thread stopped")

        #     logger.info("Apply DM flat file...")
        #     rtc_s = {"TTDMFLATFILE": flat_name}
        #     status = self.obsController.RTCTTExecuteSetup(rtc_s)
        #     logger.info("Apply DM flat file... DONE")


            
        # if self.paragraph("due"):
        #     det_s={"DIT":"1", "NDIT":"2", "READOUT":"128x128"}
        #     gen_base.main(det_s=det_s)
        #     time.sleep(1)

        if self.paragraph("tre"):
            time.sleep(1)

        self.paused = True
        self.check_pause_or_abort()
        
        if self.paragraph("quattro"):
            time.sleep(1)

        #self.abort()
        
        if self.paragraph("cinque"):
            time.sleep(1)

        # for a in range(5):
        #     if self.paragraph("sei"):
        #         print(1+"a")
        #         time.sleep(1)

        if self.paragraph("sette"):
            time.sleep(1)

        if self.paragraph("otto"):
            time.sleep(1)

        # ################
        # # new gen base #
        # ################

        # logger.warning("Only Setup")
        # gen_base.main(ins_s=ins_s)

        # logger.warning("Only Detector")
        # gen_base.main(det_s=det_s)

        # logger.warning("Setup and detector")
        # gen_base.main(ins_s=ins_s, det_s=det_s)

        # logger.warning("Setup and detector, NO EXPOSE")
        # gen_base.main(ins_s=ins_s, det_s=det_s, expose=False)
        
