import os
from pathlib import Path
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup

from .models import ListModel, set_db, db

MEDIA_DIR = Path(os.getenv("MEDIA_DIR"))
DB_DIR = MEDIA_DIR / "lists_db"
DB_DIR.mkdir(parents=True, exist_ok=True)


NAME, NEW, ACTION, ITEMADD, ITEMREMOVE = range(5)

from ..basehandler import BaseHandler


class ListHandler(BaseHandler):
    """Create and edit lists"""

    def __init__(self, entry_string, models):
        self.journal_models = models # not needed here
        self.entry_string = entry_string
        self.handler = ConversationHandler(
            entry_points=[CommandHandler(entry_string, self.entry_point)],
            states={
                NAME: [
                    CallbackQueryHandler(self.choose_list, pattern="^list-"),
                    CallbackQueryHandler(self.new_list, pattern="^new$"),
                    ],
                NEW : [MessageHandler(filters.TEXT, callback=self.new_listname)],
                ACTION: [
                    CallbackQueryHandler(self.list_add, pattern="^add$"),
                    CallbackQueryHandler(self.list_remove, pattern="^remove$"),
                    CallbackQueryHandler(self.list_clear, pattern="^clear$"),
                    CallbackQueryHandler(self.list_delete, pattern="^delete$"),
                    CallbackQueryHandler(self.list_print, pattern="^print$"),
                    CallbackQueryHandler(self.list_menu, pattern="^overview$"),
                    ],
                ITEMADD : [MessageHandler(filters.TEXT, callback=self.list_add_item)],
                ITEMREMOVE : [CallbackQueryHandler(self.list_remove_index)]
            },
            fallbacks=[CommandHandler('list', self.entry_point)],
        )


    async def entry_point(self, update, context) -> None:
        await super().entry_point(update, context)
        set_db(DB_DIR / f"chat_{update.message.chat_id}.db")
        with db:
            lists = ListModel.select()
        keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.name}")] for k in lists] + \
            [[InlineKeyboardButton("New list", callback_data="new")]]

        reply_markup = InlineKeyboardMarkup(keyboard)
        await update.message.reply_text(text="Here are the existing lists. You can also create a new one:", reply_markup=reply_markup)
        return NAME


    async def choose_list(self, update, context) -> None:
        query = update.callback_query
        data = query.data
        name = data.replace("list-","")
        await query.answer()
        self.current_name = name

        keyboard = [
            [InlineKeyboardButton("Add item", callback_data="add")],
            [InlineKeyboardButton("Remove item", callback_data="remove")],
            [InlineKeyboardButton("Clear list", callback_data="clear")],
            [InlineKeyboardButton("Print list", callback_data="print")],
            [InlineKeyboardButton("Delete list", callback_data="delete")],
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)

        await query.edit_message_text("Very well. For " + name + " the following actions are available:", reply_markup=reply_markup)
        return ACTION


    async def list_menu(self, update, context) -> None:
        query = update.callback_query
        await query.answer()

        keyboard = [
            [InlineKeyboardButton("Add item", callback_data="add")],
            [InlineKeyboardButton("Remove item", callback_data="remove")],
            [InlineKeyboardButton("Clear list", callback_data="clear")],
            [InlineKeyboardButton("Print list", callback_data="print")],
            [InlineKeyboardButton("Delete list", callback_data="delete")],
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)

        await query.edit_message_text("Very well. For " + self.current_name + " the following actions are available:", reply_markup=reply_markup)
        return ACTION


    async def new_list(self, update, context) -> None:
        query = update.callback_query
        await query.answer()
        await query.edit_message_text("What's the name of the new list?")
        return NEW
        

    async def new_listname(self, update, context) -> None:
        name = update.message.text
        try:
            with db:
                ListModel.create(name = name)
            keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]]
            reply_markup = InlineKeyboardMarkup(keyboard)
            self.current_name = name
            await update.message.reply_text("Thanks. List " + name + " was successfully created.", reply_markup=reply_markup)
            return ACTION
        except Exception as e:
            await update.message.reply_text("Oh no! Encountered exception: {}".format(e))
            return ConversationHandler.END

    
    async def list_add(self, update, context) -> None:
        query = update.callback_query
        await query.answer()
        await query.edit_message_text("What would you like to add?")
        return ITEMADD


    async def list_remove(self, update, context) -> None:
        query = update.callback_query
        await query.answer()
        with db:
            list_object = ListModel.get(name = self.current_name)

        keyboard = [[InlineKeyboardButton(k, callback_data=i)] for i,k in enumerate(list_object.content_list)]
        reply_markup = InlineKeyboardMarkup(keyboard)

        await query.edit_message_text("Which item would you like to remove?", reply_markup = reply_markup)
        return ITEMREMOVE


    async def list_clear(self, update, context) -> None:
        query = update.callback_query
        await query.answer()
        with db:
            ListModel.get(name = self.current_name).content_list = []
        keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
        reply_markup = InlineKeyboardMarkup(keyboard)
        await query.edit_message_text("List " + self.current_name + " cleared", reply_markup=reply_markup)
        return ACTION


    async def list_delete(self, update, context) -> None:
        query = update.callback_query
        await query.answer()
        with db:
            ListModel.get(name = self.current_name).delete_instance()
        await query.edit_message_text("List " + self.current_name + " deleted")
        return ConversationHandler.END


    async def list_print(self, update, context) -> None:
        query = update.callback_query
        await query.answer()
        with db:
            it = ListModel.get(name = self.current_name).content_list
        if it:
            content = "·" + "\n· ".join(it)
        else:
            content = "List empty"
        
        keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
        reply_markup = InlineKeyboardMarkup(keyboard)
        await query.edit_message_text("Content of " + self.current_name + ":\n" + content, reply_markup=reply_markup)
        return ACTION


    async def list_add_item(self, update, context) -> None:
        item = update.message.text
        with db:
            ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item]
        # TODO test me!
        keyboard = [[InlineKeyboardButton("Add some more", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
        reply_markup = InlineKeyboardMarkup(keyboard)
        await update.message.reply_text("Added " + item, reply_markup=reply_markup)
        return ACTION


    async def list_remove_index(self, update, context) -> None:
        query = update.callback_query
        ind = int(query.data)
        await query.answer()

        with db:
            list_object = ListModel.get(name = self.current_name)
            old = list_object.content_list
            name = old.pop(ind)
            list_object.content_list = old

        keyboard = [[InlineKeyboardButton("Remove another", callback_data="remove"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
        reply_markup = InlineKeyboardMarkup(keyboard)      

        await query.edit_message_text("Removed " + name, reply_markup=reply_markup)
        return ACTION