import sys
import os
import os.path
import re
from datetime import datetime, timedelta
from app.libraries.setup import Setup
from mastodon import Mastodon
from app.libraries.database import Database
from app.libraries.nodeinfo import Nodeinfo
import pdb

def cleanhtml(raw_html):
    cleanr = re.compile('<.*?>')
    cleantext = re.sub(cleanr, '', raw_html)
    return cleantext

def unescape(s):
    s = s.replace("&apos;", "'")
    return s

def replying():

    reply = False

    content = cleanhtml(text)
    content = unescape(content)

    try:

        start = content.index("@")
        end = content.index(" ")
        if len(content) > end:

            content = content[0: start:] + content[end +1::]

        neteja = content.count('@')

        i = 0
        while i < neteja :

            start = content.rfind("@")
            end = len(content)
            content = content[0: start:] + content[end +1::]
            i += 1

        question = content.lower()

        query_word = question
        query_word_length = len(query_word)

        if query_word[:4]  == 'soft':

            reply = True

        if query_word[:6]  == 'server':

            reply = True

        return (reply, query_word)

    except ValueError as v_error:

        print(v_error)

        query_word = ''

        return (reply, query_word)

# main

if __name__ == '__main__':

    setup = Setup()

    mastodon = Mastodon(
            access_token = setup.mastodon_app_token,
            api_base_url= setup.mastodon_hostname
            )

    db = Database()

    ndi = Nodeinfo()

    now = datetime.now()

    bot_id = mastodon.me().id

    notifications = mastodon.notifications()

    if len(notifications) == 0:

        print('No mentions')
        sys.exit(0)

    for notif in notifications:

        notification_id = notif.id

        if notif.type != 'mention':

            print(f'dismissing notification {notification_id}')

            mastodon.notifications_dismiss(notification_id)

            continue

        account_id = notif.account.id

        username = notif.account.acct

        status_id = notif.status.id

        text  = notif.status.content

        visibility = notif.status.visibility

        reply, query_word = replying()

        if reply == True:

            if query_word[:4] == 'soft':

                key_word = query_word[:4]

                search_soft = query_word[5:]

                if search_soft != '':

                    servers, users, mau = db.get_soft_data(search_soft)

                    toot_text = f'@{username}, my data for {search_soft} software:\n\n'

                    if servers != 0:

                        toot_text += f'software :{search_soft}:\nservers: {servers:,}\nusers: {users:,}\nMAU: {mau:,}'

                    else:

                        toot_text += 'software not found!'

                    mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)

                    print(f'Notification {notification_id} replied')

                    mastodon.notifications_dismiss(notification_id)

            if query_word[:6] == 'server':

                key_word = query_word[:6]

                search_server = query_word[7:]

                if search_server != '':

                    server, software, version, users, mau, alive = db.fediquery_server_data(search_server)

                    toot_text = f'@{username}, my data for {search_server}:\n\n'

                    if server == '' or server != '' and not alive:

                        server, api = db.get_nodeinfo_endpoint(server)

                        if server != '' and api != '':

                            try:

                                server, soft, version, users, mau, alive = ndi.getdata(server, api)

                                if soft != '':

                                    db.write_alive_server(server, soft, version, users, mau, alive)

                                else:

                                    db.write_not_alive_server(server)

                            except:

                                pass

                        if server != '' and alive:

                            toot_text += f"\nServer not found but it's alive. Added!\n\n"

                    if alive:

                        toot_text += f'server: {server}\nsoftware: :{software}:\nversion: {version}\nMAU: {int(mau):,}\nusers: {int(users):,}\nalive: {alive}'

                    else:

                        toot_text += 'server not found!'

                    mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)

                    print(f'Notification {notification_id} replied')

                    mastodon.notifications_dismiss(notification_id)
        else:

            try:

                print(f'Dismissing notification {notification_id}')

                mastodon.notifications_dismiss(notification_id)

            except MastodonNotFoundError as notfound_error:

                print(f'{notfound_error}')

                continue
