<gh_stars>0
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from aiogram.dispatcher import FSMContext


from utils.pages import user_lists_page
from keyboards.inline.callback_datas import (
    get_list_callback,
    del_list_callback,
    del_value_callback,
    add_value_callback,
)
from keyboards.inline.lists_markups import start_page_keyboard, new_list_keyboard
from loader import dp, db


@dp.callback_query_handler(text_contains="user_lists")
async def user_lists(call: CallbackQuery):
    await user_lists_page(call, db)


@dp.callback_query_handler(get_list_callback.filter())
async def view_user_list(call: CallbackQuery, callback_data: dict):
    user_id = str(call.from_user.id)
    list_id = int(callback_data.get("list_id"))
    list_name = db.get_list_name_by_list_id(user_id, list_id)
    list_values = db.get_user_list_values(user_id, list_id)

    bot_answer = f"Название списка: {list_name}\nСодержание списка: \n"
    if list_values is None:
        await call.answer("Данный список уже удален.")
        return

    list_keyboard = InlineKeyboardMarkup()

    for ind, value in enumerate(list_values):
        bot_answer += f"{ind+1}. {value}\n"
        but = InlineKeyboardButton(
            text=f"🔴{ind+1}", callback_data=f"del_value:{value}:{list_id}"
        )
        list_keyboard.insert(but)

    del_list_but = InlineKeyboardButton(
        text="❌Удалить список", callback_data=f"del_list:{list_id}"
    )
    list_keyboard.row(del_list_but)
    back_but = InlineKeyboardButton(
        text="◀️Назад", callback_data=f"user_lists_{list_id}"
    )
    list_keyboard.row(back_but)

    await call.message.edit_text(bot_answer, reply_markup=list_keyboard)


@dp.callback_query_handler(del_list_callback.filter())
async def delete_list(call: CallbackQuery, callback_data: dict, state: FSMContext):
    list_id = int(callback_data.get("list_id"))
    user_id = str(call.from_user.id)
    list_name = db.get_list_name_by_list_id(user_id, list_id)
    db.del_list(user_id, list_name)

    await call.answer("Список успешно удален", show_alert=False)
    await user_lists_page(call, db)


@dp.callback_query_handler(del_value_callback.filter())
async def del_value_from_list(call: CallbackQuery, callback_data: dict):
    user_id = str(call.from_user.id)
    value = callback_data.get("value")
    list_id = int(callback_data.get("list_id"))
    list_name = db.get_list_name_by_list_id(user_id, list_id)

    list_values = db.get_user_list_values_by_list_name(user_id, list_name)

    message_text = call.message.text

    entities = call.message.entities
    crossed_values = [i.get_text(message_text) for i in entities]
    crossed_values.append(value)

    list_keyboard = InlineKeyboardMarkup()

    text = f"Название списка: {list_name}\nСодержание списка: \n"

    for ind, value in enumerate(list_values):
        line = (
            f"{ind+1}. {value}\n"
            if value not in crossed_values
            else f"{ind+1}. <s>{value}</s>\n"
        )
        text += line

        but = (
            InlineKeyboardButton(
                text=f"🔴{ind+1}", callback_data=f"del_value:{value}:{list_id}"
            )
            if value not in crossed_values
            else InlineKeyboardButton(
                text=f"🟢{ind+1}", callback_data=f"add_value:{value}:{list_id}"
            )
        )
        list_keyboard.insert(but)
    del_list_but = InlineKeyboardButton(
        text="❌Удалить список", callback_data=f"del_list:{list_id}"
    )
    list_keyboard.row(del_list_but)
    back_but = InlineKeyboardButton(
        text="◀️Назад", callback_data=f"user_lists_{list_id}"
    )
    list_keyboard.row(back_but)

    await call.message.edit_text(text, reply_markup=list_keyboard)


@dp.callback_query_handler(add_value_callback.filter())
async def add_value_to_list(call: CallbackQuery, callback_data: dict):
    user_id = str(call.from_user.id)
    value = callback_data.get("value")
    list_id = int(callback_data.get("list_id"))
    list_name = db.get_list_name_by_list_id(user_id, list_id)

    list_values = db.get_user_list_values_by_list_name(user_id, list_name)
    message_text = call.message.text

    entities = call.message.entities
    crossed_values = [i.get_text(message_text) for i in entities]
    crossed_values.remove(value)

    list_keyboard = InlineKeyboardMarkup()

    text = f"Название списка: {list_name}\nСодержание списка: \n"

    for ind, value in enumerate(list_values):
        line = (
            f"{ind+1}. {value}\n"
            if value not in crossed_values
            else f"{ind+1}. <s>{value}</s>\n"
        )
        text += line

        but = (
            InlineKeyboardButton(
                text=f"🔴{ind+1}", callback_data=f"del_value:{value}:{list_id}"
            )
            if value not in crossed_values
            else InlineKeyboardButton(
                text=f"🟢{ind+1}", callback_data=f"add_value:{value}:{list_id}"
            )
        )
        list_keyboard.insert(but)
    del_list_but = InlineKeyboardButton(
        text="❌Удалить список", callback_data=f"del_list:{list_id}"
    )
    list_keyboard.row(del_list_but)
    back_but = InlineKeyboardButton(
        text="◀️Назад", callback_data=f"user_lists_{list_id}"
    )
    list_keyboard.row(back_but)

    await call.message.edit_text(text, reply_markup=list_keyboard)

from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from aiogram.dispatcher import FSMContext


from utils.pages import user_lists_page
from keyboards.inline.callback_datas import (
    get_list_callback,
    del_list_callback,
    del_value_callback,
    add_value_callback,
)
from keyboards.inline.lists_markups import start_page_keyboard, new_list_keyboard
from loader import dp, db


@dp.callback_query_handler(text_contains="user_lists")
async def user_lists(call: CallbackQuery):
    await user_lists_page(call, db)


@dp.callback_query_handler(get_list_callback.filter())
async def view_user_list(call: CallbackQuery, callback_data: dict):
    user_id = str(call.from_user.id)
    list_id = int(callback_data.get("list_id"))
    list_name = db.get_list_name_by_list_id(user_id, list_id)
    list_values = db.get_user_list_values(user_id, list_id)

    bot_answer = f"Название списка: {list_name}\nСодержание списка: \n"
    if list_values is None:
        await call.answer("Данный список уже удален.")
        return

    list_keyboard = InlineKeyboardMarkup()

    for ind, value in enumerate(list_values):
        bot_answer += f"{ind+1}. {value}\n"
        but = InlineKeyboardButton(
            text=f"🔴{ind+1}", callback_data=f"del_value:{value}:{list_id}"
        )
        list_keyboard.insert(but)

    del_list_but = InlineKeyboardButton(
        text="❌Удалить список", callback_data=f"del_list:{list_id}"
    )
    list_keyboard.row(del_list_but)
    back_but = InlineKeyboardButton(
        text="◀️Назад", callback_data=f"user_lists_{list_id}"
    )
    list_keyboard.row(back_but)

    await call.message.edit_text(bot_answer, reply_markup=list_keyboard)


@dp.callback_query_handler(del_list_callback.filter())
async def delete_list(call: CallbackQuery, callback_data: dict, state: FSMContext):
    list_id = int(callback_data.get("list_id"))
    user_id = str(call.from_user.id)
    list_name = db.get_list_name_by_list_id(user_id, list_id)
    db.del_list(user_id, list_name)

    await call.answer("Список успешно удален", show_alert=False)
    await user_lists_page(call, db)


@dp.callback_query_handler(del_value_callback.filter())
async def del_value_from_list(call: CallbackQuery, callback_data: dict):
    user_id = str(call.from_user.id)
    value = callback_data.get("value")
    list_id = int(callback_data.get("list_id"))
    list_name = db.get_list_name_by_list_id(user_id, list_id)

    list_values = db.get_user_list_values_by_list_name(user_id, list_name)

    message_text = call.message.text

    entities = call.message.entities
    crossed_values = [i.get_text(message_text) for i in entities]
    crossed_values.append(value)

    list_keyboard = InlineKeyboardMarkup()

    text = f"Название списка: {list_name}\nСодержание списка: \n"

    for ind, value in enumerate(list_values):
        line = (
            f"{ind+1}. {value}\n"
            if value not in crossed_values
            else f"{ind+1}. <s>{value}</s>\n"
        )
        text += line

        but = (
            InlineKeyboardButton(
                text=f"🔴{ind+1}", callback_data=f"del_value:{value}:{list_id}"
            )
            if value not in crossed_values
            else InlineKeyboardButton(
                text=f"🟢{ind+1}", callback_data=f"add_value:{value}:{list_id}"
            )
        )
        list_keyboard.insert(but)
    del_list_but = InlineKeyboardButton(
        text="❌Удалить список", callback_data=f"del_list:{list_id}"
    )
    list_keyboard.row(del_list_but)
    back_but = InlineKeyboardButton(
        text="◀️Назад", callback_data=f"user_lists_{list_id}"
    )
    list_keyboard.row(back_but)

    await call.message.edit_text(text, reply_markup=list_keyboard)


@dp.callback_query_handler(add_value_callback.filter())
async def add_value_to_list(call: CallbackQuery, callback_data: dict):
    user_id = str(call.from_user.id)
    value = callback_data.get("value")
    list_id = int(callback_data.get("list_id"))
    list_name = db.get_list_name_by_list_id(user_id, list_id)

    list_values = db.get_user_list_values_by_list_name(user_id, list_name)
    message_text = call.message.text

    entities = call.message.entities
    crossed_values = [i.get_text(message_text) for i in entities]
    crossed_values.remove(value)

    list_keyboard = InlineKeyboardMarkup()

    text = f"Название списка: {list_name}\nСодержание списка: \n"

    for ind, value in enumerate(list_values):
        line = (
            f"{ind+1}. {value}\n"
            if value not in crossed_values
            else f"{ind+1}. <s>{value}</s>\n"
        )
        text += line

        but = (
            InlineKeyboardButton(
                text=f"🔴{ind+1}", callback_data=f"del_value:{value}:{list_id}"
            )
            if value not in crossed_values
            else InlineKeyboardButton(
                text=f"🟢{ind+1}", callback_data=f"add_value:{value}:{list_id}"
            )
        )
        list_keyboard.insert(but)
    del_list_but = InlineKeyboardButton(
        text="❌Удалить список", callback_data=f"del_list:{list_id}"
    )
    list_keyboard.row(del_list_but)
    back_but = InlineKeyboardButton(
        text="◀️Назад", callback_data=f"user_lists_{list_id}"
    )
    list_keyboard.row(back_but)

    await call.message.edit_text(text, reply_markup=list_keyboard)