from typing import Any

from aiogram.types import InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder

from bot.services.texts import texts


async def payment_methods_keyboard(order_id: str, methods: dict[str, Any]) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()
    if methods.get("wallet_available"):
        builder.button(
            text=await texts.text("btn_pay_wallet"), callback_data=f"pay:wallet:{order_id}"
        )
    if methods.get("tron_available"):
        builder.button(text=await texts.text("btn_pay_tron"), callback_data=f"pay:tron:{order_id}")
    if methods.get("bank_available"):
        builder.button(text=await texts.text("btn_pay_bank"), callback_data=f"pay:bank:{order_id}")
    builder.button(
        text=await texts.text("btn_back_invoice"), callback_data=f"ord:invoice:{order_id}"
    )
    builder.adjust(1)
    return builder.as_markup()


async def tron_keyboard(payment_id: str, order_id: str) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()
    builder.button(text=await texts.text("btn_check_payment"), callback_data=f"chk:{payment_id}")
    builder.button(text=await texts.text("btn_copy_amount"), callback_data=f"cpy:{payment_id}")
    builder.button(text=await texts.text("btn_back"), callback_data=f"ord:pay:{order_id}")
    builder.adjust(1)
    return builder.as_markup()


async def bank_keyboard(order_id: str) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()
    builder.button(text=await texts.text("btn_cancel"), callback_data=f"ord:pay:{order_id}")
    return builder.as_markup()


async def success_keyboard(order_id: str) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()
    builder.button(text=await texts.text("btn_view_order"), callback_data=f"ord:view:{order_id}")
    builder.button(text=await texts.text("btn_buy_again"), callback_data="menu:buy")
    builder.button(text=await texts.text("btn_main_menu"), callback_data="menu:main")
    builder.adjust(2, 1)
    return builder.as_markup()


async def failed_keyboard(order_id: str) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()
    builder.button(text=await texts.text("btn_retry"), callback_data=f"ord:pay:{order_id}")
    builder.button(
        text=await texts.text("btn_back_invoice"), callback_data=f"ord:invoice:{order_id}"
    )
    builder.button(text=await texts.text("btn_main_menu"), callback_data="menu:main")
    builder.adjust(2, 1)
    return builder.as_markup()


async def timeout_keyboard(order_id: str) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()
    builder.button(text=await texts.text("btn_new_invoice"), callback_data=f"pay:tron:{order_id}")
    builder.button(text=await texts.text("btn_back"), callback_data=f"ord:pay:{order_id}")
    builder.adjust(1)
    return builder.as_markup()
