import html_parser

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ParseMode
from telegram.ext import ContextTypes


async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(
        f"hello, i'm {html_parser.bold(context.bot.first_name)}, an inline image grabber.\n\n"
        f"to get help, use /help",
        parse_mode=ParseMode.HTML
    )


async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(
        f"{html_parser.bold(f"how to use {context.bot.first_name}")}\n\n"
        f"1. open your message box and type:\n"
        f"{html_parser.code(f"@{context.bot.username} <post ID>")}\n"
        f"2. click on the box that has popped up\n"
        f"3. ???\n"
        f"4. done!",
        parse_mode=ParseMode.HTML
    )


async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    from main import config
    reply_markup = None
    source_url = config.get('General', 'SourceUrl')
    if source_url.startswith('http://') or source_url.startswith('https://'):
        keyboard = [
            [
                InlineKeyboardButton(f"source code", url=source_url)
            ]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)

    await update.message.reply_text(
        f"{html_parser.bold(f"about {context.bot.first_name}")}\n"
        f"{context.bot.first_name} is an inline image grabber written in python that grabs images from Danbooru "
        f"(or other similar services).\n\n"
        f"{html_parser.bold("currently configured instance:")}\n"
        f"{html_parser.italic(config.get('Service', 'Name'))}  ({config.get('Service', 'Domain')})",
        parse_mode=ParseMode.HTML,
        reply_markup=reply_markup
    )