# from vrpmdv-server/vrpmdv-main import application
import sys
import json
from flask import Flask, request , send_from_directory , Response
from flask_cors import CORS
from vrpmdvdata import VRPMDV_Data
from vrpmdvmonreqschema import VRPMDV_MonReqSchema 

app = Flask(__name__, static_url_path='', static_folder='./build')
CORS(app) #comment this on deployment

vrpmdvdata = VRPMDV_Data()


@app.route("/", defaults={'path':''})
def serve(path):
    return send_from_directory(app.static_folder,'index.html')

@app.route('/vrpmdvapi/1_0/monitorings', methods=['GET'])
def get_monitorings():
    data = vrpmdvdata.getMonitorings()
    resp = Response(data, status=200, mimetype='application/json')
    return resp

@app.route('/vrpmdvapi/1_0/monitorings/<id>', methods=['GET'])
def get_monitoring(id):
    data = vrpmdvdata.getMonitoring(id)
    resp = Response(data, status=200, mimetype='application/json')
    return resp

@app.route('/vrpmdvapi/1_0/monitorings/<id>', methods=['PATCH'])
def set_monitoring(id):
    vrpmreq = VRPMDV_MonReqSchema().load(request.get_json())    
    data = vrpmdvdata.setMonitoring(id, vrpmreq)
    resp = Response(data, status=200, mimetype='application/json')
    return resp


@app.route('/vrpmdvapi/1_0/monitorings', methods=['POST'])
def create_monitoring():
    vrpmreq = VRPMDV_MonReqSchema().load(request.get_json())
    data = vrpmdvdata.createMonitoring(vrpmreq)
    resp = Response(data, status=200, mimetype='application/json')
    return resp


@app.route('/vrpmdvapi/1_0/monitorings/<id>', methods=['DELETE'])
def delete_monitoring(id):
    data = vrpmdvdata.deleteMonitoring(id)
    resp = Response(data, status=200, mimetype='application/json')
    return resp    



@app.route('/vrpmdvapi/1_0/monitorings/start', methods=['PUT'])
def start_monitoring():
    vrpmid = request.get_json()
    data = vrpmdvdata.startMonitoring(vrpmid)
    resp = Response(data, status=200, mimetype='application/json')
    return resp    

@app.route('/vrpmdvapi/1_0/monitorings/stop', methods=['PUT'])
def stop_monitoring():
    vrpmid = request.get_json()
    data = vrpmdvdata.stopMonitoring(vrpmid)
    resp = Response(data, status=200, mimetype='application/json')
    return resp    




if __name__ == "__main__":
    app.run()

