import handlers
import profiles
import locales
from profiles.ProfileManager import get_full_profile_by_tg_id
from profiles.ProfileManager import update
from keyboards.basic_kb import inline
import profiles.ProfileStep as ProfileStep
from keyboards.language_kb import language_keyboard
from bot import bot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton


@bot.callback_query_handler(func=lambda c:c.data.startswith("lang_"))
def language(call):
    p = get_full_profile_by_tg_id(call.from_user.id)

    if (p.language == call.data.split("_")[1]) and (p.step == ProfileStep.LANGUAGE):
        return



    if(call.data != 'lang_back'):
        lang = call.data.split("_")[1]
        p.language = lang
    p.step = ProfileStep.LANGUAGE
    update(p)

    text = (
        f"{locales.get(p.language, 'language_title')}"
    )

    reply_markup=language_keyboard(
        texts=locales.LANGUAGES[p.language]
    )

    button_text = (
        f"{locales.get(p.language, 'language')}"
        " | "
        f"{locales.get(p.language, 'btn_lang_continue')}"
    )

    button_ctn = InlineKeyboardButton(text=button_text, callback_data="btn_lang_continue")
    reply_markup.add(button_ctn)

    handlers.ui.render(
        p,
        call.message.chat.id,
        text,
        reply_markup=reply_markup
    )