# Recode By @mrismanaziz
# @SharingUserbot
# RecodeV2 By @PacarFerdilla
# @KingUserbotSupport

import importlib
import logging
import os
import sys
from pathlib import Path

from userbot import CMD_HELP, LOGS, bot  # pylint:disable=E0602
from userbot.events import register

DELETE_TIMEOUT = 5


def load_module(shortname):
    if shortname.startswith("__"):
        pass
    elif shortname.endswith("_"):
        path = Path(f"userbot/modules/{shortname}.py")
        name = "userbot.modules.{}".format(shortname)
        spec = importlib.util.spec_from_file_location(name, path)
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)
        LOGS.info("Sukses Menambahkan Plugin " + shortname)
    else:

        path = Path(f"userbot/modules/{shortname}.py")
        name = "userbot.modules.{}".format(shortname)
        spec = importlib.util.spec_from_file_location(name, path)
        mod = importlib.util.module_from_spec(spec)
        mod.bot = bot
        mod.LOGS = LOGS
        mod.CMD_HELP = CMD_HELP
        mod.logger = logging.getLogger(shortname)
        spec.loader.exec_module(mod)
        # for imports
        sys.modules["userbot.modules." + shortname] = mod
        LOGS.info("Sukses Menambahkan Plugin " + shortname)


@register(outgoing=True, pattern=r"^\.install$")
async def _(event):
    if event.fwd_from:
        return
    if event.reply_to_msg_id:
        try:
            await event.edit("`Installing Plugin...`")
            downloaded_file_name = (
                await event.client.download_media(  # pylint:disable=E0602
                    await event.get_reply_message(),
                    "userbot/modules/",  # pylint:disable=E0602
                )
            )
            if "(" not in downloaded_file_name:
                path1 = Path(downloaded_file_name)
                shortname = path1.stem
                load_module(shortname.replace(".py", ""))
                await event.edit(
                    "**Plugin** `{}` **Sukses di install**".format(
                        os.path.basename(downloaded_file_name)
                    )
                )
            else:
                os.remove(downloaded_file_name)
                await event.edit("**Error!** Plugin ini sudah terinstall di userbot.")
        except Exception as e:  # pylint:disable=C0103,W0703
            await event.edit(str(e))
            os.remove(downloaded_file_name)


CMD_HELP.update(
    {
        "install": "✘ Pʟᴜɢɪɴ : Install Plugin\
        \n\n⚡𝘾𝙈𝘿⚡ : `.install` <Reply ke File Plugins>\
        \n↳ : Untuk Menginstall Plugins Userbot Secara Instan.\
    "
    }
)
