from telethon.utils import is_audio, is_gif, is_image, is_video

from ..helpers import var
from .base import Filter


class _FilterVar(var.Var):
    def __set_name__(self, owner, val):
        if not self.value:
            self.value = Filter(
                lambda event: event.file is not None
                and (self.prefix + val + self.suffix) == event.file.ext
            )


class File:
    is_video: Filter = Filter(lambda event: is_video(event.media))
    is_image: Filter = Filter(lambda event: is_image(event.media))
    is_gif: Filter = Filter(lambda event: is_gif(event.document))
    is_audio: Filter = Filter(lambda event: is_audio(event.media))

    # generated by extensions
    # Image extensions
    png: Filter = _FilterVar()
    jpeg: Filter = _FilterVar()
    webp: Filter = _FilterVar()
    gif: Filter = _FilterVar()
    bmp: Filter = _FilterVar()
    tga: Filter = _FilterVar()
    tiff: Filter = _FilterVar()
    psd: Filter = _FilterVar()

    # Video extensions
    mp4: Filter = _FilterVar()
    mov: Filter = _FilterVar()
    avi: Filter = _FilterVar()

    # Audio extensions
    mp2: Filter = _FilterVar()  # a.k.a. ISO/IEC 13818-2
    mp3: Filter = _FilterVar()
    m4a: Filter = _FilterVar()
    aac: Filter = _FilterVar()
    ogg: Filter = _FilterVar()
    flac: Filter = _FilterVar()

    # Win executable extension
    exe: Filter = _FilterVar()
