#!/usr/bin/env python3

import asyncio
import json

from pprint import pprint
import os
import sys

import datetime


sys.path.append(os.path.join(sys.path[0], 'bin'))


from main import main


def debugLog(msg, level):
    logFile = 'debug.log'
    current_time = datetime.datetime.now()
    ts = current_time.strftime('%d-%m-%Y %H:%M:%S.%f')


    logMsg = '[ ' + ts + ' ]  ' + '[ ' + msg +  ' ] ' + '[ ' + level +  ' ]'
    f = open(logFile, "a")
    f.write(logMsg)
    pprint(logMsg)
    f.close()

def prioSort(articles):
    sortArray = {}
    sortArray2 = {}
    sortArray3 = {}
    for id in articles:
        if articles[id]['prio'] == 'pinned':
            sortArray[id] = articles[id]
        elif articles[id]['prio'] == 'important':
            sortArray3[id] = articles[id]
        else:
            sortArray2[id] = articles[id]      

    sortArrayPrio = {**sortArray, **sortArray3} 
    return {**sortArrayPrio, **sortArray2} 

"""
from db import db
test =  {
        "Anstellungsart": "Festanstellung",
        "Alter": "06.01.2023",
        "Titel": "Sozialp\u00e4dagogin/Sozialp\u00e4dagoge , MNA-Zentrum Lilienberg 80%",
        "Firma": "Asyl Organisation Z\u00fcrich",
        "id": "dbfe35a30e0171da68a93139e777a62a",
        "siId": "550026",
        "href": "/job/sozialpaedagogin-sozialpaedagoge-mna-zentrum-lilienberg-affoltern-am-albis-550026",
        "lnk": "https://www.sozialinfo.ch/job/sozialpaedagogin-sozialpaedagoge-mna-zentrum-lilienberg-affoltern-am-albis-550026",
        "content": {
            "Ort": "Affoltern am Albis",
            "Antritt": "per sofort",
            "Beschreibung": "    StellenbeschriebDie AOZ ist mit\u00a0rund 1'300\u00a0Mitarbeiter*innen ",
            "Anmelden": "https://aoz-rexx.ch/sozialpaedagoginsozialpaedagoge-80-mna-zentrum-lilienberg-de-f545.html?jobPipeline=sozialinfo.ch",
            "Kontaktperson": "none"
        }
}


ntfy = push(ntfyLnk, ntfyTopic)
ntfy.send(test)
pprint(json.loads(main()))
print("filter result:", filter(test, filterFile))
dbCon = db(dbFile)
dbCon = db('/home/maru/Dev/pythonSoup/pythonSoup/data/articles.db')
print(json.dumps(dbCon.readAll('articles')))

async def loadMain():
    result = await main()
    data = json.loads(result)
    pprint(data)
"""
async def loadMain():
    result = await main('quick')
    data = json.loads(result)

    data = prioSort(data)
    c = 1
    sample = {}
    sampleC = "no pos"
    #pprint(data)

    
    for article in data:
        #pprint(data[article]['Firma'])
        if data[article]['Titel'] == 'ERROR':
            print(data[article])
        if data[article]['prio'] == 'normal':
            sample = data[article]
            sampleC = c
            
        c += 1
    if not sample:
        sample['Titel'] = 'None'
    title = sample['Titel']
    print(f'\n \n total articles: {c} \n ----------------- \n')
    print(f'chose: {title} at {sampleC}\n')
    #pprint(data)


asyncio.run(loadMain())

