# --------------------------------------------------------------------------------------
# Class to generate a virtual display
# --------------------------------------------------------------------------------------

from pyvirtualdisplay import Display
from accelarobotkeywords.config.displaysettings import DisplaySettings
import pyscreenshot as ImageGrab
from robot.api import logger


class VirtualDisplay:
    _display = None

    @staticmethod
    def create_display():
        """Creates a virtual display"""
        width = DisplaySettings.get_width()
        height = DisplaySettings.get_height()
        depth = DisplaySettings.get_depth()
        logger.info("Creating virtual display %s,%s,%s" % (width, height, depth))
        display = Display(backend='xvfb', use_xauth=False, visible=False, color_depth=depth, size=(width, height))
        display.start()
        logger.info("Virtual display created")
        VirtualDisplay._display = display

    @staticmethod
    def stop_display():
        """Stops the virtual display"""
        if VirtualDisplay._display is not None:
            VirtualDisplay._display.stop()
            logger.info("Virtual display was stopped")
        else:
            logger.info("No virtual display to stop")

    @staticmethod
    def capture_screenshot(path):
        """Captures a screenshot to specified path"""
        img = ImageGrab.grab()
        img.save(path)
        logger.info("Screenshot captured to file: %s" % path)


