from uuid import uuid4
import uuid
from flask import jsonify
import json

# from vrpmdvmonitoringschema import VRPMDV_MonitoringSchema
# from vrpmdvmonitoring  import VRPMDV_Monitoring 

from vrpmdvmonitoringschema import VRPMDV_MonitoringSchema
from vrpmdvmonitoring import VRPMDV_Monitoring

class VRPMDV_Data:   

    def __init__(self):
        #self.loaded = False
        self.mons = []
        self.loaded = self.loadFile()

    def loadFile(self):
        try: 
            with open('./mons.json', 'r') as f:
                fmons = f.read()

                if not fmons:
                    # return no Item
                    return True
                else:
                    # data = json.loads(fmons)
                    schema = VRPMDV_MonitoringSchema(many=True)
                    res = schema.loads(fmons) # data
                    self.mons = schema.loads(fmons) # .append(res)
                    return True
        except:
            #nothing todo
            print("file not found")

    def saveFile(self):
        try: 
            with open('./mons.json', 'w') as f:
                schema = VRPMDV_MonitoringSchema()
                result = schema.dumps(self.mons, many=True) 
                f.write(result)
                return True
        except:
            #nothing todo
            print("could not write file")

    def findMon(mon):
        return mon

    def getMonitorings(self):
        # 1st time read file
        if not self.loaded :
            self.loaded = self.loadFile()
        
        schema = VRPMDV_MonitoringSchema()
        result = schema.dumps(self.mons, many=True)
        return result

    def getMonitoring(self, vrpmid):
        try:
            matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
        except:
            return "no Item found"
        
        # we find it and we return it
        schema = VRPMDV_MonitoringSchema()
        return schema.dumps(matched_obj)
    
    def setMonitoring(self, vrpmid, request):
        try:
            matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
            matched_obj.name = request.name
            matched_obj.samplerate = request.samplerate
            matched_obj.sampleperiod = request.sampleperiod
            matched_obj.downtime = request.downtime
            self.saveFile()

        except:
            return "no Item found"
        

        # we find it and we return it
        schema = VRPMDV_MonitoringSchema()
        return schema.dumps(matched_obj)



    def createMonitoring(self, request):
        #mon = VRPMDV_Monitoring(request["name"], request["samplerate"], request["sampleperiod"], request["downtime"], request["owner"])
        id = uuid.uuid4()
        mon = VRPMDV_Monitoring(id, request.name, request.samplerate, request.sampleperiod, request.downtime, request.owner)

        if not self.loaded :
            self.loaded = self.loadFile()
        # create monitoring 
        self.mons.append(mon)
        #save to file
        self.saveFile()
        #create result Object in json
        schema = VRPMDV_MonitoringSchema()
        return schema.dumps(mon) 

    def deleteMonitoring(self, vrpmid):
        if not self.loaded :
            self.loaded = self.loadFile()

        # find monitoring with uuid
        #result = filter(lambda mon: str(mon.uuid) == vrpmid["uuid"], self.mons)
        try:
            matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
        except:
            return "no Item found"
        
        # we find it and now remove from list
        self.mons.remove(matched_obj)
        #save the list
        self.saveFile()
        #make result
        schema = VRPMDV_MonitoringSchema()
        return schema.dump(matched_obj)


    def startMonitoring(self, vrpmid):
        #if not self.loaded :
        #    self.loaded = self.loadFile()
        #call the start API of the kernel treiber


        return "started"

    def stopMonitoring(self, vrpmid):
        #if not self.loaded :
        #    self.loaded = self.loadFile()
        #call the start API of the kernel treiber
        
        return "stopped"

    