from socketio import Server

from pycs import app
from pycs.database.File import File
from pycs.database.Label import Label
from pycs.database.Model import Model
from pycs.database.Project import Project
from pycs.database.Result import Result
from pycs.frontend.util.JSONEncoder import JSONEncoder
from pycs.jobs.Job import Job


class NotificationManager:
    """
    send events via a socket.io connection
    """

    def __init__(self, sio: Server):
        self.sio = sio
        self.json = JSONEncoder()

    def __emit(self, name, obj):
        enc = self.json.default(obj)
        self.sio.emit(name, enc)

    def create_job(self, created_job: Job):
        """
        fire create-job event

        :param created_job:
        :return:
        """
        app.logger.info(created_job)
        self.__emit('create-job', created_job)

    def edit_job(self, edited_job: Job):
        """
        fire edit-job event

        :param edited_job:
        :return:
        """
        app.logger.info(edited_job)
        self.__emit('edit-job', edited_job)

    def remove_job(self, removed_job: Job):
        """
        fire remove-job event

        :param removed_job:
        :return:
        """
        app.logger.info(removed_job)
        self.__emit('remove-job', removed_job)

    def create_model(self, created_model: Model):
        """
        fire create-model event

        :param created_model:
        :return:
        """
        app.logger.info(created_model)
        self.__emit('create-model', created_model)

    def remove_model(self, removed_model: Model):
        """
        fire remove-model event

        :param removed_model:
        :return:
        """
        app.logger.info(removed_model)
        self.__emit('remove-model', removed_model)

    def create_project(self, created_project: Project):
        """
        fire create-project event

        :param created_project:
        :return:
        """
        app.logger.info(created_project)
        self.__emit('create-project', created_project)

    def remove_project(self, removed_project: Project):
        """
        fire remove-project event

        :param removed_project:
        :return:
        """
        app.logger.info(removed_project)
        self.__emit('remove-project', removed_project)

    def edit_project(self, edited_project: Project):
        """
        fire edit-project event

        :param edited_project:
        :return:
        """
        app.logger.info(edited_project)
        self.__emit('edit-project', edited_project)

    def create_label(self, created_label: Label):
        """
        fire create-label event

        :param created_label:
        :return:
        """
        app.logger.info(created_label)
        self.__emit('create-label', created_label)

    def edit_label(self, edited_label: Label):
        """
        fire edit-label event

        :param edited_label:
        :return:
        """
        app.logger.info(edited_label)
        self.__emit('edit-label', edited_label)

    def remove_label(self, removed_label: Label):
        """
        fire remove-label event

        :param removed_label:
        :return:
        """
        app.logger.info(removed_label)
        self.__emit('remove-label', removed_label)

    def create_file(self, created_file: File):
        """
        fire create-file event

        :param created_file:
        :return:
        """
        app.logger.info(created_file)
        self.__emit('create-file', created_file)

    def edit_file(self, edited_file: File):
        """
        fire edit-file event

        :param edited_file:
        :return:
        """
        app.logger.info(edited_file)
        self.__emit('edit-file', edited_file)

    def remove_file(self, removed_file: File):
        """
        fire remove-file event

        :param removed_file:
        :return:
        """
        app.logger.info(removed_file)
        self.__emit('remove-file', removed_file)

    def create_result(self, created_result: Result):
        """
        fire create-result event

        :param created_result:
        :return:
        """
        app.logger.info(created_result)
        self.__emit('create-result', created_result)

    def edit_result(self, edited_result: Result):
        """
        fire edit-result event

        :param edited_result:
        :return:
        """
        app.logger.info(edited_result)
        self.__emit('edit-result', edited_result)

    def remove_result(self, removed_result: Result):
        """
        fire remove-result event

        :param removed_result:
        :return:
        """
        app.logger.info(removed_result)
        self.__emit('remove-result', removed_result)
