from pyrogram import Filters
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound

from ..bot import Bot
from ..db import session
from ..helpers import filters
from ..helpers.handle_helpers import parse_guild_and_nick
from ..models.admins import Admin
from ..models.chats import Chat
from ..models.member import Member


@Bot.on_message(filters.admin_public_filter & Filters.command("add_chat"))
async def add_chat_handler(_, message):
    try:
        chat_id = message.chat.id
        chat_title = message.chat.title
        index = session.query(Chat).count() + 1
        chat = Chat(id=index, chat_id=chat_id, chat_title=chat_title)
        session.add(chat)
        session.commit()
        await message.reply("Chat has been added")
    except IntegrityError:
        await message.reply("Chat already exist")


@Bot.on_message(filters.admin_public_filter & Filters.command("add_admin") & Filters.reply)
async def add_admin_handler(_, message):
    try:
        replied_message = message.reply_to_message
        user = replied_message.from_user
        user_id = user.id
        username = user.username
        if not user.is_bot:
            admin = Admin(user_id=user_id, username=username)
            session.add(admin)
            session.commit()
            await message.reply("User " + username + " has been added")
        else:
            message.reply("Bot isn't allowed to be admin")
    except IntegrityError:
        await message.reply("This user is admin already")


@Bot.on_message(filters.admin_public_filter & Filters.command("del_chat"))
async def del_chat_handler_public(_, message):
    try:
        chat_id = message.chat.id
        session.query(Chat).filter(Chat.chat_id == chat_id).delete()
        session.commit()
        await message.reply("Chat has been deleted")
    except NoResultFound:
        await message.reply("Chat not found.")


@Bot.on_message(filters.admin_public_filter & Filters.command("add_member") & Filters.reply)
async def add_member_handler(_, message):
    try:
        user_id = message.reply_to_message.from_user.id
        guild, nick = '', ''
        for line in message.reply_to_message.text.splitlines():
            if '[' in line and ']' in line:
                guild, nick = parse_guild_and_nick(line)
        if guild == '' or nick == '':
            await message.reply("Please, use /add as reply to /me or /hero forwarded message")
        else:
            member = Member(user_id=user_id, guild=guild, nick=nick)
            session.add(member)
            session.commit()
            await message.reply(f"Member {nick} from guild {guild} was added")
    except IntegrityError:
        await message.reply("Member already exist")


@Bot.on_message(filters.admin_public_filter & Filters.command("del_member") & Filters.reply)
async def del_member_handler(_, message):
    try:
        user_id = message.reply_to_message.from_user.id
        session.query(Member).filter(Member.user_id == user_id).delete()
        session.commit()
        await message.reply("Member deleted")
    except NoResultFound:
        await message.reply("Member not found")
