from . import command, STOP_PROCESSING, CONTINUE_PROCESSING, RestartException, USER_SET, users_map, reply_fn
import inspect
import sys
import subprocess
import importlib
import os
import signal
import re
import random
import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup

config = None

@command('units (?:(.+) to )?(.+)', pass_groups=True)
def units(bea, bot, update, groups):
    groups = [x for x in groups if x is not None]
    proc = subprocess.run(["units", *groups], capture_output=True, text=True)
    if proc.stderr:
        if (len(proc.stderr) < 1024):
            return bea.reply(f"error running units: {proc.stderr}")
    elif proc.stdout:
        if (len(proc.stdout) < 1024):
            return bea.reply(proc.stdout.replace('\t', ''))

def visent(bea, bot, update):
    """visent"""
    if not update.effective_message: return
    if not update.effective_message.text: return
    text = update.effective_message.text
    if (m := re.search(r'\bvis(s?)ent\b', text, flags=re.I)):
        update.message.reply_sticker("CAACAgQAAxkBAAIMIV5pfAABr3CDdAc201jqdCs6w0NUkgACXQIAAug0swKma3yXEo-HlRgE")
        if not m.group(1):
            bea.reply('Maar het is natuurlijk eigenlijk vissent.')
        return STOP_PROCESSING

@command('stil')
def stil(bea, bot, update):
    """ Je moet gewoon je bek houden. """
    video = config.get('stil_video_id') or open('bekhoudensloom.mp4', 'rb')
    msg = update.message.reply_video(video=video, quote=False)
    if 'stil_video_id' not in config:
        config['stil_video_id'] = msg.video.file_id

@command(r'reload (\w+)', pass_groups=True)
def reload_module(bea, bot, update, groups):
    """ Reload a Bea module. """
    modname = groups[0]
    name = 'modules.' + modname
    new_commands = []
    for cmd in bea.commands:
        if inspect.getmodule(cmd).__name__ != name:
            new_commands.append(cmd)

    try:
        mod = importlib.reload(sys.modules[name])
        sys.modules[name] = mod
        bea.commands = mod.init(bea, bea.config.get(modname, {})) + new_commands
    except Exception as e:
        update.message.reply_text(f"Error reloading module: {e}")
        return

@command(r'send-message (\S+) (.+)', pass_groups=True, auth=True)
def send_message(bea, bot, update, groups):
    """ Send a message to a chat. Chat ID first, then the message. """
    channel, text = groups
    channel = int(channel)
    print(f"{channel}, {text}")
    bot.send_message(channel, text)

@command('send-sticker (.+) (.+)', pass_groups=True, auth=True)
def send_sticker(bea, bot, update, groups):
    """ Send a sticker to a chat. Chat ID first, then the file ID of the sticker. """
    sticker_chat, sticker = groups
    sticker_chat = int(sticker_chat)
    bot.send_sticker(sticker_chat, sticker)

@command('lenny', inline=True)
def lenny(bea, bot, update):
    """ ( ͡° ͜ʖ ͡°) """
    if update.inline_query is not None:
        bea.inline_reply_text("( ͡° ͜ʖ ͡°)")
    else:
        update.message.reply_text('( ͡° ͜ʖ ͡°)')

@command('delete$', auth=True)
def delete(bea, bot, update):
    """ Delete the message that this is a reply to. """
    m = update.message.reply_to_message
    if not m:
        return
    bot.deleteMessage(update.effective_chat.id, m.message_id)
    bot.deleteMessage(update.effective_chat.id, update.effective_message.message_id)

@command('review', additional_match_fn=reply_fn)
def review(bea, bot, update, reply):
    bea.reply(random.choice(('based', 'cringe')), reply_to_message_id=reply.message_id)

@command('restart', auth=True)
def restart(bea, bot, update):
    """ Restart Bea. """
    bea.is_restarting = True
    os.kill(os.getpid(), signal.SIGINT)

@command(r"@iedereen(')? ?(-?\d+)?", pass_groups=True, prefix=False)
def iedereen(bea, bot, update, groups):
    geenat = groups[0] is not None
    group_id = int(groups[1]) if groups[1] else update.effective_chat.id
    admins = bot.get_chat_administrators(group_id)
    bea.reply(' '.join([('' if geenat else '@')+admin.user.username for admin in admins]), quote=False)


@command("hide", additional_match_fn=reply_fn)
def hide(bea, bot, update, reply):
    try:
        res = reply.forward('@beahiddenmsgs')
    except telegram.error.BadRequest as e:
        return bea.reply(f"Error while forwarding: {e}")
    button = InlineKeyboardButton(text='Show', url=f"https://t.me/tfckbot?start=unhide_{res.message_id}")
    keeb = InlineKeyboardMarkup([[button]])
    reply.delete()
    bea.reply('_Hidden message_', reply_markup=keeb, parse_mode='Markdown')


@command('/start (.+)', prefix=False, pass_groups=True)
def handle_start(bea, bot, update, groups):
    data = groups[0]
    if data.startswith('unhide_'):
        hidden_id = int(data.split('_')[1])
        bot.forward_message(update.effective_chat.id, '@beahiddenmsgs', hidden_id)


@command('cut$', additional_match_fn=reply_fn, auth=True)
def cut(bea, bot, update, reply):
    if reply.from_user.id != 571967181:
        return bea.reply("Can't cut: not the owner")

    if reply.text:
        return reply.edit_text('<snip>')

    if reply.photo:
        media = telegram.InputMediaPhoto("https://via.placeholder.com/1/000000", caption="<snip>")
        return reply.edit_media(media)

    return bea.reply("Can't cut: unsupported media")



def init(bea, _config):
    global config
    config = _config
    return [cut, handle_start, hide, units, review, iedereen, visent, stil, delete, lenny, send_message, send_sticker, restart, reload_module]
