from ctypes import byref
from sdl2 import *

import entitymanager


class Spritesheet:
    """This class abstracts a sprite sheet.

    Attributes:
        entitymanager: Parent EntityManager instance.

        filename: Filename of the sprite sheet image.
        image: The filetype.ImageFile instance for the sprite sheet image.
        texture: The SDL_Texture for the sprite sheet image.
        imagewidth: Width of the sprite sheet in pixels.
        imageheight: Height of the sprite sheet in pixels.
    """

    def __init__(self, entitymanager: 'entitymanager.EntityManager', filename: str):
        """Spritesheet class initializer.

        Args:
            entitymanager: Link back to the parent EntityManager instance.
            filename: Filename of the sprite sheet image.
        """
        self.entitymanager = entitymanager

        self.filename = filename
        self.image = None
        self.texture = None
        self.imagewidth = 0
        self.imageheight = 0
        self.__resource = self.entitymanager.driftwood.resource

        self.__prepare_spritesheet()

    def __prepare_spritesheet(self) -> None:
        self.image = self.__resource.request_image(self.filename)
        self.texture = self.image.texture

        tw, th = c_int(), c_int()
        SDL_QueryTexture(self.texture, None, None, byref(tw), byref(th))
        self.imagewidth, self.imageheight = tw.value, th.value
