from PyQt5.QtGui import QMovie
import logging
from ui.test_test import Test_Test
import time
from PyQt5.QtCore import  QByteArray
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class Test_Pipe_Cutter(Test_Test):
    """
    A class to manage and operate a pipe cutter machine in a test environment.
    """

    def __init__(self, config, components, recipe=None, run_once=False,reset_on_start=False, enable_override=False, bench_name="generic", step=None,):
        super().__init__(components=components, recipe=recipe, step=step, run_once=run_once,reset_on_start=reset_on_start, enable_override=enable_override,)
        self.config = config
        self.bench_name = bench_name
        self.step=step
        self.current_cut_length = 0
        self.get_connection = None

        gif_path = "config/instruction_images/st-ten-10/pipe.gif"
        self.movie = QMovie(gif_path,QByteArray(), self)
        self.pipe_gif.setMovie(self.movie)
        self.movie.start()


        self.pipe_gif.setMovie(self.movie)

    def start(self, recipe=None, step=None, pieces=None):
        self.get_connection = self.components["pipe_cutter"].out.connect(self.get)
        self.components["pipe_cutter"].resume()


        show = super().start(recipe=recipe, step=step, pieces=pieces)
        if show is False:
            return show

        if step is not None :
            self.start_cutting()
            self.stop_cutting()
        return show

    def start_cutting(self,recipe=None,step=None):
        """
        Start the pipe cutting process and activate the pipe cutter component.
        """
        self.current_status = self.components["pipe_cutter"]._get()
        #print(f"current status is :{str(self.current_status)}")
        #print(self.step.spec["lenght"])
        self.lenght=self.step.spec["lenght"]
        self.diameter=self.step.spec["diameter"]
        if self.current_status == 102 : # ready for operation
            try:
                #self.reset_machine()
                self.components["pipe_cutter"].write_total_length(self.lenght)
                self.components["pipe_cutter"].write_od_of_pipe(self.diameter)
                time.sleep(1)
                self.components["pipe_cutter"].write_bit_with_delay(600,0,100)
            except Exception as e:
                logger.error(f"Failed to start the pipe cutting process: {e}")
        else:
            try:
                self.components["pipe_cutter"].write_bit_with_delay(600, 1, 100)  # resetting
            except Exception as e:
                logger.error(f"Failed to reset the pipe cutting process: {e}")

    def stop_cutting(self):
        """
        Stop the pipe cutting process and deactivate the pipe cutter component.
        """
        try:
                self.components["pipe_cutter"].write_bit_with_delay(600, 2, 100)
                #self.reset_machine()
        except Exception as e:
            logger.error(f"Failed to stop the pipe cutting process: {e}")

    def stop(self):

        self.components["pipe_cutter"].pause()
        self.disconnect(self.get_connection)
        super().stop()

    def reset_machine(self):
        """
        Reset the pipe cutter machine to its initial state.
        """
        try:
           self.components["pipe_cutter"].write_bit_with_delay(600, 1, 100)
        except Exception as e:
            logger.error(f"Failed to reset the pipe cutter machine: {e}")


