import string

from random import choices

from vars import *
from config import *
from helpers import get_user_config, set_user_setting

from aiogram.types import (
    InlineKeyboardButton,
    InlineKeyboardMarkup,
    InlineQueryResultArticle,
    InputTextMessageContent,
    Message,
)

async def da(bot, message, user_id):
    config = get_user_config(user_id)
    if user_id in alo_command:
        if message.chat.id == alo_command[user_id]["chat_id"]:
            if alo_command[user_id]["action"] == "p_name":
                alo_command[user_id]["action"] = "p_prompt"
                alo_command[user_id]["name"] = message.text[:16]
                return await bot.edit_message_text(
                    text=f"установи промпт для `{alo_command[user_id]['name']}`:",
                    chat_id=alo_command[user_id]["chat_id"],
                    message_id=alo_command[user_id]["message_id"],
                    reply_markup=InlineKeyboardMarkup(
                        inline_keyboard=[
                            [
                                InlineKeyboardButton(
                                    text=f"❌ отменить", callback_data=f"p_cancel%{user_id}"
                                ),
                            ],
                        ]
                    )
                )
            elif alo_command[user_id]["action"] == "p_prompt":
                alo_command[user_id]["action"] = None
                alo_command[user_id]["prompt"] = message.text

                if len(config["prompts"]) >= MAX_PROMPTS:
                    return await bot.edit_message_text(
                        text=f"невозможно создать новый промпт так как ты превысил лимит промптов da.",
                        chat_id=alo_command[user_id]["chat_id"],
                        message_id=alo_command[user_id]["message_id"],
                        reply_markup=InlineKeyboardMarkup(
                            inline_keyboard=[
                                [
                                    InlineKeyboardButton(
                                        text=f"ок", callback_data=f"user_prompts%{user_id}"
                                    ),
                                ],
                            ]
                        )
                    )
                
                while 1:
                    alo_command[user_id]["id"] = ''.join(choices(string.ascii_letters, k=10))
                    if alo_command[user_id]["id"] not in config["prompts"]: 
                        break

                config["prompts"] = config["prompts"] | {
                    alo_command[user_id]["id"]: {
                        "name": alo_command[user_id]["name"],
                        "text": alo_command[user_id]["prompt"],
                        "filer": False,
                        "public": False
                    }
                }

                set_user_setting(
                    user_id, 
                    prompts=config["prompts"]
                )
                
                return await bot.edit_message_text(
                    text=f"промпт `{alo_command[user_id]['name']}` создан!",
                    chat_id=alo_command[user_id]["chat_id"],
                    message_id=alo_command[user_id]["message_id"],
                    reply_markup=InlineKeyboardMarkup(
                        inline_keyboard=[
                            [
                                InlineKeyboardButton(
                                    text=f"✅ посмотреть", callback_data=f"vp%{user_id}%{alo_command[user_id]['id']}"
                                ),
                            ],
                        ]
                    )
                )
            elif alo_command[user_id]["action"] == "p_e_name":
                alo_command[user_id]["action"] = None
                alo_command[user_id]["name"] = message.text[:16]
                
                config["prompts"] = config["prompts"] | {
                    alo_command[user_id]["prompt_id"]: {
                        "name": alo_command[user_id]["name"],
                        "text": config["prompts"][alo_command[user_id]["prompt_id"]]["text"],
                        "filer": config["prompts"][alo_command[user_id]["prompt_id"]]["filer"],
                        "public": config["prompts"][alo_command[user_id]["prompt_id"]]["public"],
                    }
                }

                set_user_setting(
                    user_id, 
                    prompts = config["prompts"]
                )
                
                return await bot.edit_message_text(
                    text=f"имя промпта было изменено на `{alo_command[user_id]['name']}`.",
                    chat_id=alo_command[user_id]["chat_id"],
                    message_id=alo_command[user_id]["message_id"],
                    reply_markup=InlineKeyboardMarkup(
                        inline_keyboard=[
                            [
                                InlineKeyboardButton(
                                    text=f"✅ посмотреть", callback_data=f"vp%{user_id}%{alo_command[user_id]['prompt_id']}"
                                ),
                            ],
                        ]
                    )
                )
            elif alo_command[user_id]["action"] == "p_e_prompt":
                alo_command[user_id]["action"] = None
                alo_command[user_id]["prompt"] = message.text
                
                config["prompts"] = config["prompts"] | {
                    alo_command[user_id]["prompt_id"]: {
                        "name": config["prompts"][alo_command[user_id]["prompt_id"]]["name"],
                        "text": alo_command[user_id]["prompt"],
                        "filer": config["prompts"][alo_command[user_id]["prompt_id"]]["filer"],
                        "public": config["prompts"][alo_command[user_id]["prompt_id"]]["public"],
                    }
                }

                set_user_setting(
                    user_id, 
                    prompts=config["prompts"]
                )

                return await bot.edit_message_text(
                    text=f"текст промпта был изменён.",
                    chat_id=alo_command[user_id]["chat_id"],
                    message_id=alo_command[user_id]["message_id"],
                    reply_markup=InlineKeyboardMarkup(
                        inline_keyboard=[
                            [
                                InlineKeyboardButton(
                                    text=f"✅ посмотреть", callback_data=f"vp%{user_id}%{alo_command[user_id]['prompt_id']}"
                                ),
                            ],
                        ]
                    )
                )
    return None
