#(Â©)CodeXBotz
import os
import asyncio
from pyrogram import Client, filters, __version__
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from pyrogram.errors import FloodWait, UserIsBlocked, InputUserDeactivated

from bot import Bot
from config import ADMINS, FORCE_MSG, START_MSG, OWNER_ID, CUSTOM_CAPTION, DISABLE_CHANNEL_BUTTON
from helper_func import subscribed, encode, decode, get_messages
from database.sql import add_user, query_msg, full_userbase


#=====================================================================================##

WAIT_MSG = """"<b>Processing ...</b>"""

REPLY_ERROR = """<code>Use this command as a replay to any telegram message with out any spaces.</code>"""

#=====================================================================================##


@Bot.on_message(filters.command('start') & filters.private & subscribed)
async def start_command(client: Client, message: Message):
    id = message.from_user.id
    user_name = '@' + message.from_user.username if message.from_user.username else None
    try:
        await add_user(id, user_name)
    except:
        pass
    text = message.text
    if len(text)>7:
        try:
            base64_string = text.split(" ", 1)[1]
        except:
            return
        string = await decode(base64_string)
        argument = string.split("-")
        if len(argument) == 3:
            try:
                start = int(int(argument[1]) / abs(client.db_channel.id))
                end = int(int(argument[2]) / abs(client.db_channel.id))
            except:
                return
            if start <= end:
                ids = range(start,end+1)
            else:
                ids = []
                i = start
                while True:
                    ids.append(i)
                    i -= 1
                    if i < end:
                        break
        elif len(argument) == 2:
            try:
                ids = [int(int(argument[1]) / abs(client.db_channel.id))]
            except:
                return
        temp_msg = await message.reply("ğ�™°ğ�™²ğ�™²ğ�™´ğ�š‚ğ�š‚ğ�™¸ğ�™½ğ�™¶ ğ�™µğ�™¸ğ�™»ğ�™´ğ�š‚...!")
        try:
            messages = await get_messages(client, ids)
        except:
            await message.reply_text("ğ�š‚ğ�š˜ğ�š–ğ�š�ğ�š�ğ�š‘ğ�š’ğ�š—ğ�š� ğ�š ğ�š�ğ�š—ğ�š� ğ�š ğ�š›ğ�š˜ğ�š—ğ�š�..!")
            return
        await temp_msg.delete()

        for msg in messages:

            if bool(CUSTOM_CAPTION) & bool(msg.document):
                caption = CUSTOM_CAPTION.format(previouscaption = "" if not msg.caption else msg.caption.html, filename = msg.document.file_name)
            else:
                caption = "" if not msg.caption else msg.caption.html

            if DISABLE_CHANNEL_BUTTON:
                reply_markup = msg.reply_markup
            else:
                reply_markup = None

            try:
                await msg.copy(chat_id=message.from_user.id, caption = caption, parse_mode = 'html', reply_markup = reply_markup)
                await asyncio.sleep(0.5)
            except FloodWait as e:
                await asyncio.sleep(e.x)
                await msg.copy(chat_id=message.from_user.id, caption = caption, parse_mode = 'html', reply_markup = reply_markup)
            except:
                pass
        return
    else:
        reply_markup = InlineKeyboardMarkup(
            [
                    [
                        InlineKeyboardButton("ğ�š‚ğ�š„ğ�™±ğ�š‚ğ�™²ğ�š�ğ�™¸ğ�™±ğ�™´", url="https://youtube.com/channel/UCf_dVNrilcT0V2R--HbYpMA")                        
                    ],
                    [
                        InlineKeyboardButton("ğ�™°ğ�™±ğ�™¾ğ�š„ğ�šƒ", callback_data = "about"),
                        InlineKeyboardButton("ğ�™²ğ�™»ğ�™¾ğ�š‚ğ�™´", callback_data = "close")
                    ]
                ]
            )        
        await message.reply_text(
            text = START_MSG.format(
                first = message.from_user.first_name,
                last = message.from_user.last_name,
                username = None if not message.from_user.username else '@' + message.from_user.username,
                mention = message.from_user.mention,
                id = message.from_user.id
            ),
            reply_markup = reply_markup,
            disable_web_page_preview = True,
            quote = True
        )
        return

@Bot.on_message(filters.command('start') & filters.private)
async def not_joined(client: Client, message: Message):
    buttons = [
        [
            InlineKeyboardButton(
                "ğ�™¹ğ�™¾ğ�™¸ğ�™½ ğ�™²ğ�™·ğ�™°ğ�™½ğ�™½ğ�™´ğ�™»",
                url = client.invitelink)
        ]
    ]
    try:
        buttons.append(
            [
                InlineKeyboardButton(
                    text = 'ğ�šƒğ�š�ğ�šˆ ğ�™°ğ�™¶ğ�™°ğ�™¸ğ�™½',
                    url = f"https://t.me/{client.username}?start={message.command[1]}"
                )
            ]
        )
    except IndexError:
        pass

    await message.reply(
        text = FORCE_MSG.format(
                first = message.from_user.first_name,
                last = message.from_user.last_name,
                username = None if not message.from_user.username else '@' + message.from_user.username,
                mention = message.from_user.mention,
                id = message.from_user.id
            ),
        reply_markup = InlineKeyboardMarkup(buttons),
        quote = True,
        disable_web_page_preview = True
    )

@Bot.on_message(filters.command('users') & filters.private & filters.user(ADMINS))
async def get_users(client: Bot, message: Message):
    msg = await client.send_message(chat_id=message.chat.id, text=WAIT_MSG)
    users = await full_userbase()
    await msg.edit(f"{len(users)} users are using this bot")

@Bot.on_message(filters.private & filters.command('broadcast') & filters.user(ADMINS))
async def send_text(client: Bot, message: Message):
    if message.reply_to_message:
        query = await query_msg()
        broadcast_msg = message.reply_to_message
        total = 0
        successful = 0
        blocked = 0
        deleted = 0
        unsuccessful = 0
        
        pls_wait = await message.reply("<b><i>ğ�™±ğ�š�ğ�™¾ğ�™°ğ�™³ğ�™²ğ�™°ğ�š‚ğ�šƒğ�™¸ğ�™½ ğ�™¼ğ�™´ğ�š‚ğ�š‚ğ�™°ğ�™¶ğ�™´ğ�š‚....</i></b>")
        for row in query:
            chat_id = int(row[0])
            try:
                await broadcast_msg.copy(chat_id)
                successful += 1
            except FloodWait as e:
                await asyncio.sleep(e.x)
                await broadcast_msg.copy(chat_id)
                successful += 1
            except UserIsBlocked:
                blocked += 1
            except InputUserDeactivated:
                deleted += 1
            except:
                unsuccessful += 1
                pass
            total += 1
        
        status = f"""<b>ğ�™±ğ�š�ğ�™¾ğ�™°ğ�™³ğ�™²ğ�™°ğ�š‚ğ�šƒ ğ�™²ğ�™¾ğ�™¼ğ�™¿ğ�™»ğ�™´ğ�šƒğ�™´ğ�™³

ğ�šƒğ�™¾ğ�šƒğ�™°ğ�™» ğ�š„ğ�š‚ğ�™´ğ�š�ğ�š‚: {total}
ğ�š‚ğ�š„ğ�™²ğ�™²ğ�™´ğ�š‚ğ�š‚ğ�™µğ�š„ğ�™»: {successful}
ğ�™±ğ�™»ğ�™¾ğ�™²ğ�™ºğ�™´ğ�™³ ğ�š„ğ�š‚ğ�™´ğ�š�ğ�š‚: {blocked}
ğ�™³ğ�™´ğ�™»ğ�™´ğ�šƒğ�™´ğ�™³ ğ�™°ğ�™²ğ�™²ğ�™¾ğ�š„ğ�™½ğ�šƒ: {deleted}
ğ�š„ğ�™½ğ�š‚ğ�š„ğ�™²ğ�™²ğ�™´ğ�š‚ğ�š‚ğ�™µğ�š„ğ�™»: {unsuccessful}</b>"""
        
        return await pls_wait.edit(status)

    else:
        msg = await message.reply(REPLY_ERROR)
        await asyncio.sleep(8)
        await msg.delete()
