# if you change credits, you get anal cancer and get murdered by russians in 3 days.
"""
Support chatbox for pmpermit.
Used by incoming messages with trigger as /start
Will not work for already approved people.
"""
import asyncio
import io 
import telethon.sync
from telethon.tl.functions.users import GetFullUserRequest
import userbot.plugins.sql_helper.pmpermit_sql as pmpermit_sql
from telethon import events, errors, functions, types
from userbot import ALIVE_NAME
from userbot.utils import admin_cmd


DEFAULTUSER = str(ALIVE_NAME) if ALIVE_NAME else "Set ALIVE_NAME in heroku vars"
PREV_REPLY_MESSAGE = {}


@command(pattern=r"\/start", incoming=True)
async def _(event):
    chat_id = event.from_id
    userid = event.sender_id
    if not pmpermit_sql.is_approved(chat_id):
        chat = await event.get_chat()
        if event.fwd_from:
            return
        if event.is_private:
         
         PM = ("`Hai. Anda mengakses menu untuk memilih tujuan dan tindakan yang ingin diajukan kepada Boss saya,`"
               f"{DEFAULTUSER}.\n"
               "__Ayo lakukan ini dengan mudah dan bijak! Silahkan pilih angka berisi alasan__\n"
               "**Ketik salah satu angka tindakan apa yang akan anda lakukan:**\n\n"
               "`1`. Untuk chat dengan Boss saya.\n"
               "`2`. Hanya mau spam saja.\n"

               "`3`. Untuk bertanya sesuatu.\n"
               "`4`. Untuk meminta sesuatu.\n")
         ONE = ("`Oke permintaanmu diterima! Boss saya akan segera kembali untuk membalas chat anda!` \n\n")
         TWO = ("`Jangan lakukan hal itu! Atau kontak anda akan saya block!`")
         FOUR = ("`Baiklah! Saya paham apa yang anda inginkan! Tunggulah sampai Boss saya memberi anda akses!`")
         FIVE = ("`Okelah. Harap anda menjaga sopan santun kepada Boss saya. Jika dia ingin membantu, dia pasti akan merespon anda.`\n**Jangan lakukan hal yang mengganggu jika anda tidak ingin dilaporkan dan diblok.**")
         LWARN = ("**Ini perintah terakhir. Jangan asal kirim pesan atau anda akan saya laporkan dan blok. Harap mengerti. Boss saya pasti akan merespon anda nanti.**\n__Gunakan__ /start __untuk kembali ke menu pilihan.__")
     
        async with borg.conversation(chat) as conv:
         await borg.send_message(chat, PM)
         chat_id = event.from_id
         response = await conv.get_response(chat)
         y = response.text
         if y == "1":
             await borg.send_message(chat, ONE)
             response = await conv.get_response(chat)
             await event.delete()
             if not response.text == "/start":
                 await response.delete()
                 await borg.send_message(chat, LWARN)
                 response = await conv.get_response(chat)
                 await event.delete()
                 await response.delete()
                 response = await conv.get_response(chat)
                 if not response.text == "/start":
                     await borg.send_message(chat, TWO)
                     await asyncio.sleep(3)
                     await event.client(functions.contacts.BlockRequest(chat_id))
         elif y == "2":
             await borg.send_message(chat, LWARN)
             response = await conv.get_response(chat)
             if not response.text == "/start":
                 await borg.send_message(chat, TWO)
                 await asyncio.sleep(3)
                 await event.client(functions.contacts.BlockRequest(chat_id))
         

         elif y == "3":
             await borg.send_message(chat, FOUR)
             response = await conv.get_response(chat)
             await event.delete()
             await response.delete()
             if not response.text == "/start":
                 await borg.send_message(chat, LWARN)
                 await event.delete()
                 response = await conv.get_response(chat)
                 if not response.text == "/start":
                     await borg.send_message(chat, TWO)
                     await asyncio.sleep(3)
                     await event.client(functions.contacts.BlockRequest(chat_id))
         elif y == "4":
             await borg.send_message(chat,FIVE)
             response = await conv.get_response(chat)
             if not response.text == "/start":
                 await borg.send_message(chat, LWARN)
                 response = await conv.get_response(chat)
                 if not response.text == "/start":
                     await borg.send_message(chat, TWO)
                     await asyncio.sleep(3)
                     await event.client(functions.contacts.BlockRequest(chat_id))
         else:
             await borg.send_message(chat, "`You have entered an invalid command. Please send /start again or do not send another message if you do not wish to be blocked and reported.`")
             response = await conv.get_response(chat)
             z = response.text
             if not z == "/start":
                 await borg.send_message(chat, LWARN)
                 await conv.get_response(chat)
                 if not response.text == "/start":
                     await borg.send_message(chat, TWO)
                     await asyncio.sleep(3)
                     await event.client(functions.contacts.BlockRequest(chat_id))

