# made by eris. for [ultroid].
# uses @ hazmat_suit_bot to mask.

""" 
✘ Commands Available -

• `{i}mask <reply>` *opt
**USE**:
   Puts a Mask on replied file 🎭
  *Reply to any file or any user.
   
**CMD**- {i}mask , {i}mask -s rotate 90 , {i}mask black -s , etc.
"""

import random
import os
from telethon import events
from asyncio import sleep, TimeoutError
from telethon.errors.rpcerrorlist import YouBlockedUserError
from telethon.tl.functions.account import UpdateNotifySettingsRequest

from . import *


@ultroid_cmd(pattern="mask ?(.*)")
async def hazmat(event):
    if event.fwd_from:
        return 
    here = event.chat_id
    reply = await event.get_reply_message()
    args = s_dl = dl = " "
    argss = event.pattern_match.group(1) 
    eris = await eor(event, "`getting Mask ready` 🎭")
    slangs = ["Get a profile picture you fucking donkey!", "You don't have a profile pic, Guess you'll die..."]
    slang = random.choice(slangs)
    if "-s" in argss:
        args = argss.replace("-s", " ")
    else: args == argss
    chat = await ultroid_bot.get_entity("hazmat_suit_bot")
    async with event.client.conversation(chat.username, timeout=15) as conv:
        try: 
            response = conv.wait_event(
                events.NewMessage(incoming=True, from_users=chat.id)
            )
            if not reply:
                await ultroid_bot.send_message(
                    chat, f"/hazmat {args}"
                )
            elif reply and (reply.photo or reply.sticker or reply.gif):
                if reply.file.ext == (".tgs"):
                    await eris.edit("`can't mask animated sticker` 🤧")
                    return
                elif reply.gif:
                    p = await reply.forward_to(chat)
                    await p.reply("/hazmat")
                    await sleep(2)
                else:
                    p = await reply.forward_to(chat)
                    if args != " ":
                        await sleep(3)
                        await p.reply(f"/hazmat {args}")
            elif reply and (not reply.media):
                pp = await ultroid_bot.get_profile_photos(reply.sender_id)
                if len(pp) > 0:
                    dl = await ultroid_bot.download_media(pp[0])
                    file = await ultroid_bot.send_file(chat.id, dl)
                    if args != " ":
                        await sleep(3)
                        await file.reply(f"/hazmat {args}")
                else:
                    await eris.edit(f"`{slang}`")
                    return
            else:
                await eod(eris, "`Invalid media, Can't mask` 🤷🏻‍♀️")
                return
            response = await response
        except YouBlockedUserError:
            await eris.edit(f"`Sar, plz Unblock` @{chat.username}") 
            return
        except asyncio.TimeoutError:
            await eris.edit("`Bot didn't respond in time`")
            return
        mask = response.message
        if mask.media:
            if "-s" in argss:
                s_dl = await mask.download_media()
                await ultroid_bot.send_file(
                    here,
                    await ultroid_bot.upload_file(
                        s_dl,
                        file_name="anew.webp",
                    ),
                    reply_to=event.reply_to_msg_id,
                    caption="Ultroid @e3ris",
                )
            else:
                await ultroid_bot.send_file(
                    here, mask, reply_to=event.reply_to_msg_id,
                )
            await eris.delete()
        else:
            await eris.edit(f"`{mask.message}`")
        if os.path.isfile(dl):
            os.remove(dl)
        if os.path.isfile(s_dl):
            os.remove(s_dl)
        await ultroid_bot.send_read_acknowledge(chat)


HELP.update({f"{__name__.split('.')[1]}": f"{__doc__.format(i=HNDLR)}"})
