import pygame

sf=pygame.display.get_surface()
WIDTH = 800
HEIGHT = 800

class Images():
    all = []
    surface = None
    def __init__(self,_filename, _pos=(0,0)):
        self.image = pygame.image.load(_filename)
        self.name = _filename
        self._pos = _pos
        self._showtime = None
        self._hidetime = None
        self._visible = False
        Images.all.append(self)

    def draw(self):
        if len(Images.all)>0:
            for item in Images.all:
                if item.visible:
                    sf.blit(item.image,item._pos)

                if item._showtime and not item.visible:
                    if pygame.time.get_ticks() > item._showtime:
                        item.visible = True
                        print(f'show! no={item.name}')
                if item._hidetime and item.visible:
                    if pygame.time.get_ticks() > item._hidetime:
                        item.visible = False
                        item._showtime = None
                        item._hidetime = None
                        print(f'hide! no={item.name}')

    def align(self,halign='center',valign='center'):
        sx,sy = self.image.get_size()
        if 'center' in halign:
            x = WIDTH //2 - sx //2
        elif 'left' in halign:
            x = 0
        elif 'right' in halign:
            x = WIDTH - sx

        if 'center' in valign:
            y = HEIGHT //2 - sy //2
        elif 'up' in valign:
            y = 0
        elif 'down' in valign:
            y = HEIGHT - sy
        self._pos = (x,y)

    def opacity(self,value=255):
        self.image.set_alpha(value)

    @property
    def visible(self):
        return self._visible

    @visible.setter
    def visible(self,_yes):
        self._visible = _yes

    @classmethod
    def show(cls,no,_len=None,_delay=0):
        if no<len(cls.all):
            item = cls.all[no]
            if _len and not item._hidetime and not item._showtime:
                item._showtime = pygame.time.get_ticks()+_delay*1000
                item._hidetime = item._showtime + _len*1000
                print('showtime=',item._showtime)
                print('hidetime=',item._hidetime)
            elif _delay>0 and not item._showtime:
                item._showtime = pygame.time.get_ticks()+_delay*1000
                item._hidetime = None
            elif _len is None and _delay==0 and not item.visible:
                item._showtime = None
                item._hidetime = None
                item.visible = True
        else:
            raise ValueError('Image not found!')

    def hide(self,_delay=0):
        if self.visible and _delay:
            self._showtime = pygame.time.get_ticks()
            self._hidetime = self._showtime + _delay*1000
        else:
            self.visible = False
