import queue  
import threading 
from os import path 

import apprise 

APPRISE_CONFIG_PATH = "config/apprise.yml"


class NotificationHandler: 
    def __init__(self, enabled=True): 
        if enabled and path.exists(APPRISE_CONFIG_PATH): 
            self.apobj = apprise.Apprise() 
            config = apprise.AppriseConfig() 
            config.add(APPRISE_CONFIG_PATH) 
            self.apobj.add(config) 
            self.queue = queue.Queue() 
            self.start_worker() 
            self.enabled = True 
        else: 
            self.enabled = False 

    def start_worker(self): 
        threading.Thread(target=self.process_queue, daemon=True).start() 

    def process_queue(self): 
        while True: 
            message, attachments =  self.queue.get() 

            if attachments: 
                self.apobj.notify(body=message, attach=attachments) 
            else: 
                self.apobj.notify(body=message) 
            self.queue.task_done() 

    def send_notification(self, message, attachements=None): 
        if self.enabled: 
            self.queue.put((message, attachements or []))