
# Copyright 2021 Mohammad Kazemi <kazemi.me.222@gmail.com>.
# SPDX-License-Identifier: MIT
# Telegram API framework core imports
from collections import namedtuple
from functools import partial
from ganjoor.ganjoor import Ganjoor
from telegram.ext import Dispatcher, CallbackContext
from telegram import Update
# Helper methods import
from utils.logger import get_logger
from utils.telegram.keyboards import category_keyboard
# Telegram API framework handlers imports
from telegram.ext import CallbackQueryHandler

# Init logger
logger = get_logger(__name__)
CallbackData = namedtuple('CallbackData', "menu_name doto")


def init(dispatcher: Dispatcher, ganjoor: Ganjoor):
    """Provide handlers initialization."""
    dispatcher.add_handler(CallbackQueryHandler(
        partial(category_id, ganjoor=ganjoor), pattern=r'^category_*'))


def category_id(update: Update, context: CallbackContext, ganjoor: Ganjoor) -> int:
    """Process a /start command."""
    query = update.callback_query

    message_id = '_'.join(query.data.split('_')[2:])
    cat_id = query.data.split('_')[1]
    cat = ganjoor.find_category_by_id(cat_id, with_poems=True)

    # query.answer()
    query.answer()

    context.bot.edit_message_reply_markup(
        inline_message_id=message_id, reply_markup=category_keyboard(cat, message_id))
    # query.edit_reply_markup()

[((17, 9, 17, 29), 'utils.logger.get_logger', 'get_logger', ({(17, 20, 17, 28): '__name__'}, {}), '(__name__)', False, 'from utils.logger import get_logger\n'), ((18, 15, 18, 59), 'collections.namedtuple', 'namedtuple', ({(18, 26, 18, 40): '"""CallbackData"""', (18, 42, 18, 58): '"""menu_name doto"""'}, {}), "('CallbackData', 'menu_name doto')", False, 'from collections import namedtuple\n'), ((24, 8, 24, 45), 'functools.partial', 'partial', (), '', False, 'from functools import partial\n'), ((39, 51, 39, 85), 'utils.telegram.keyboards.category_keyboard', 'category_keyboard', ({(39, 69, 39, 72): 'cat', (39, 74, 39, 84): 'message_id'}, {}), '(cat, message_id)', False, 'from utils.telegram.keyboards import category_keyboard\n')]