# from vrpmdv-server/vrpmdv-main import application
import logging
import os
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 
# import ecal.core.core as ecal_core
# from proto_messages.RTService_pb2 import _RTSERVICE as rtservice


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

vrpmdvdata = VRPMDV_Data()


@app.route("/", defaults={'path':''})
#@app.route("/")
@app.route('/<path:path>')
def serve(path):
    logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")            
    logging.info("vrpmdvserver: path:%s", path)                        
    # path_dir = os.path.abspath("../build") #path react build
    # if path != "" and os.path.exists(os.path.join(path_dir, path)):
    #     return send_from_directory(os.path.join(path_dir), path)
    # else:
    #     return send_from_directory(os.path.join(path_dir),'index.html')
    return send_from_directory(app.static_folder,'index.html')

@app.errorhandler(404)
def not_found(e):
    return app.send_static_file('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())
    vrpmreq = 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)
    data = vrpmdvdata.createMonitoring(request)
    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=['PATCH'])
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=['PATCH'])
def stop_monitoring():
    vrpmid = request.get_json()
    data = vrpmdvdata.stopMonitoring(vrpmid)
    resp = Response(data, status=200, mimetype='application/json')
    return resp    

@app.route('/vrpmdvapi/1_0/monitoringstatus/<id>', methods=['PATCH'])
def setStatus(id):
    vrpmStatus = request.get_json()
    data = vrpmdvdata.setStatus(id, vrpmStatus)
    resp = Response(data, status=200, mimetype='application/json')
    return resp    


@app.route('/vrpmdvapi/1_0/monitoring/mondfaivalable', methods=['GET'])
def get_mondfaivalable():
    data = vrpmdvdata.getMonsDFAvalable()
    if (data == None):
        resp = Response("no Item found", status=404, mimetype='application/json')
    else:
        resp = Response(data, status=200, mimetype='application/json')
    return resp


@app.route('/vrpmdvapi/1_0/monitoringfiles', methods=['GET'])
def get_monitoringfiles():
    vrpmreqjson = request.args
    #vrpmreq = VRPMDV_MonReqSchema().load(request.get_json())
    #data = vrpmdvdata.createMonitoring(vrpmreq)
    data = vrpmdvdata.getMonitoringfiles(0, request.args)
    if (data == None):
        resp = Response("no Item found", status=404, mimetype='application/json')
    else:
        resp = Response(data, status=200, mimetype='application/json')
    return resp

@app.route('/vrpmdvapi/1_0/monitoringfiles/download', methods=['GET'])
def get_mfDownload():
    vrpmreqjson = request.args

    data = vrpmdvdata.getMFDownload(request.args)
    if (data == None):
        resp = Response("no Item found", status=404, mimetype='application/json')
    else:
        resp = Response(data, status=200, mimetype='application/json')
    return resp



if __name__ == "__main__":
  # initialize eCAL API. The name of our Process will be

  # "Python Protobuf Subscriber"

  #ecal_core.initialize(sys.argv, "Python Protobuf Subscriber")

  app.run()



# if __name__ == "__main__":



#   # Create a Protobuf Publisher that publishes on the topic

#   # "hello_world_python_protobuf_topic". The second parameter tells eCAL which

#   # datatype we are expecting to receive on that topic.

#   sub = ProtoSubscriber("hello_world_python_protobuf_topic"

#                       , hello_world_pb2.HelloWorld)


#   # Set the Callback

#   sub.set_callback(callback)

  

#   # Just don't exit

#   while ecal_core.ok():

#     time.sleep(0.5)

  

#   # finalize eCAL API

#   ecal_core.finalize()