from ..mongo import Mongo

class MongoController(Mongo):
    def __init__(self,host, port, db):
        super(MongoController,self).__init__(host, port, db)
        self.collection_job = "ControllerJobs"
        self.connection_mode = "ControllerModes"
        self.connection_current = "ControllerCurrent"
        self.connection_next = "ControllerNext"

    def insert_job(self,job):
        selected_collection = self.connection[self.collection_job]
        selected_collection.insert_one(job)

    def insert_model_into_job(self, job_id, model_id, type):
        selected_collection = self.connection[self.collection_job]
        if type == "text":
            result = selected_collection.update_one({"_id":job_id},{"$set":{"id_text": str(model_id)}})
        elif type == "matrix":
            result = selected_collection.update_one({"_id":job_id}, {"$set": {"id_matrix": str(model_id)}})

    def get_job(self, job_id):
        selected_collection = self.connection[self.collection_job]
        result = selected_collection.find_one({"_id": job_id} )
        return result

    def deactive_job(self, job_id):
        selected_collection = self.connection[self.collection_job]
        result = selected_collection.update_one({"_id": job_id}, {"$set": {"active": 0}})

    def active_job(self, job_id):
        selected_collection = self.connection[self.collection_job]
        result = selected_collection.update_one({"_id": job_id}, {"$set": {"active": 1}})

    def create_modes(self):
        selected_collection = self.connection[self.connection_mode]
        result = selected_collection.find_one({})
        if result is None:

            tmp = {
                "Arte e Cultura": 0,
                "Attualità": 0,
                "Benessere": 0,
                "Intrattenimento": 0,
                "Motori": 0,
                "Musica": 0,
                "Scienze": 0,
                "Spettacoli": 0,
                "Sport": 0,
                "Tecnologia": 0,
                "Viaggi": 0
            }

            selected_collection.insert_one(tmp)
        else:
            pass

    def change_mode(self,type,mode):
        selected_collection = self.connection[self.connection_mode]
        selected_collection.update_one({}, {"$set": {type: mode} })


    def create_current(self):
        selected_collection = self.connection[self.connection_current]
        result = selected_collection.find_one({})
        if result is None:

            tmp = {
                "Arte e Cultura": 0,
                "Attualità": 0,
                "Benessere": 0,
                "Intrattenimento": 0,
                "Motori": 0,
                "Musica": 0,
                "Scienze": 0,
                "Spettacoli": 0,
                "Sport": 0,
                "Tecnologia": 0,
                "Viaggi": 0
            }

            selected_collection.insert_one(tmp)
        else:
            pass


    def change_current(self,type,id):
        selected_collection = self.connection[self.connection_current]
        selected_collection.update_one({}, {"$set": {type: id}})


    def create_next(self):
        selected_collection = self.connection[self.connection_next]
        result = selected_collection.find_one({})
        if result is None:

            tmp = {
                "Arte e Cultura": 0,
                "Attualità": 0,
                "Benessere": 0,
                "Intrattenimento": 0,
                "Motori": 0,
                "Musica": 0,
                "Scienze": 0,
                "Spettacoli": 0,
                "Sport": 0,
                "Tecnologia": 0,
                "Viaggi": 0
            }

            selected_collection.insert_one(tmp)
        else:
            pass


    def change_next(self,type,id):
        selected_collection = self.connection[self.connection_next]
        selected_collection.update_one({}, {"$set": {type: id}})
