#!/usr/bin/env python3

import json

import requests

class push:

    def __init__(self, url, topic):
        self.url = url
        self.topic = topic


    def send(self,obj):

        msg = ''
        priority = 1
        tags = ["bell"]

        if obj == 'none':
            return requests.post(self.url,
                data=json.dumps({
                    "topic": self.topic,
                    "message": 'no new articles',
                    "tags": ["exclamation"],
                    "priority": 2
                })
            )

        if obj['Titel'] == 'ERROR':
            return requests.post(self.url,
                data=json.dumps({
                    "topic": self.topic,
                    "message": obj['msg'],
                    "title": obj['Titel'],
                    "tags": ["warning"],
                    "priority": 5
                })
            )
        

        if obj['prio'] == 'important':
            tags = ["bangbang"]
            priority = 3
        if obj['prio'] == 'pinned':
            tags = ["pushpin"]
            priority = 5

        if obj['content'] != 'none':
            msg += obj['Firma'] + ' - ' + obj['content']['Ort']  + '\n'
            msg += obj['content']['Antritt'] + '\n\n'
            if priority > 2:
                msg += obj['content']['Beschreibung']
        else:
            msg += obj['Firma'] + ' - ' + obj['Anstellungsart']


        return requests.post(self.url,
            data=json.dumps({
                "topic": self.topic,
                "message": msg,
                "title": obj['Titel'],
                "tags": tags,
                "priority": priority,
                "actions": [{ "action": "view", "label": "Open Source", "url": obj['lnk'], "clear": True }]
            })
        )


