from telebot.async_telebot import AsyncTeleBot
from telebot.types import Message, InputFile
import asyncio
from secret import TOKEN
import textbook
from sqlalchemy.orm import Session

from db.engine import engine
from db import models
from msgprocessor import TrackerRemovalMsgProcessor, TrackerRemovalProcessorMessage
from femboygenerator import FemboyGeneratorMsgProcessor, FemboyGeneratorMessage

bot = AsyncTeleBot(TOKEN)


@bot.message_handler(commands=["start"])
async def start(msg: Message):
    if msg.chat.type == "private":
        await bot.send_message(chat_id=msg.chat.id, text=textbook.start_private)
    elif msg.chat.type in ("group", "supergroup"):
        await bot.send_message(chat_id=msg.chat.id, text=textbook.start_group)


@bot.message_handler(func=lambda message: True)
async def got_message(msg: Message):
    # god i love nones as fuck
    if msg.text is None:
        return
    if msg.from_user is None:
        return

    tracker_removal_result = TrackerRemovalMsgProcessor(
        TrackerRemovalProcessorMessage(fromUser=msg.from_user, text=msg.text)
    ).process()

    femboy_generator_result = FemboyGeneratorMsgProcessor(msg=msg).process()

    if tracker_removal_result.needsToReply:
        try:
            await bot.delete_message(msg.chat.id, msg.id, timeout=5)
        except Exception as e:
            await bot.reply_to(
                message=msg.id,
                text="Uoghhhh, i am not an admin here? I can't cleanup this tracking(",
            )
            print(e, flush=True)  # todo: логгер

        await bot.send_message(
            msg.chat.id, tracker_removal_result.text, parse_mode="html"
        )

    if femboy_generator_result.send:
        with Session(engine) as db:
            upload = (
                db.query(models.TelegramUpload)
                .filter(
                    models.TelegramUpload.path == femboy_generator_result.media_path
                )
                .first()
            )

            if upload:
                if femboy_generator_result.media_type == "gif":
                    await bot.send_animation(
                        reply_to_message_id=femboy_generator_result.message.id,
                        chat_id=femboy_generator_result.message.id,
                        animation=upload.file_id,
                    )
                elif femboy_generator_result.media_type == "pic":
                    await bot.send_photo(
                        reply_to_message_id=femboy_generator_result.message.id,
                        chat_id=femboy_generator_result.message.id,
                        photo=upload.file_id,
                    )
            else:
                if femboy_generator_result.media_type == "gif":
                    reply_msg = await bot.send_animation(
                        reply_to_message_id=femboy_generator_result.message.id,
                        chat_id=femboy_generator_result.message.id,
                        animation=InputFile(femboy_generator_result.media_path),
                    )
                elif femboy_generator_result.media_type == "pic":
                    await bot.send_photo(
                        reply_to_message_id=femboy_generator_result.message.id,
                        chat_id=femboy_generator_result.message.id,
                        photo=InputFile(femboy_generator_result.media_path),
                    )
                file_id = reply_msg.animation.file_id
                new_upload = models.TelegramUpload(
                    path=femboy_generator_result.media_path,
                    file_id=file_id,
                    type=femboy_generator_result.media_type,
                )
                db.add(new_upload)
                db.commit()


async def main():
    a = asyncio.create_task(bot.polling(non_stop=True))
    await a


if __name__ == "__main__":
    print("Bot started", flush=True)
    asyncio.run(main())
