#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Third-party modules
from flask_restx import Namespace, Resource

# Custom modules
from util.instances import RTC

api = Namespace('rtc', description='Real Time Control')


@api.route("/plots/")
class PlotListApi(Resource):
    
    def get(self):
        """Show RTC related data"""        
    
        data = RTC.data()
        
        return data

@api.route("/plots/<int:id>")
class PlotApi(Resource):

    def get(self, id):
        return RTC.read(id)

    def put(self, id):
        return RTC.update(id, api.payload)

@api.route("/cloud")
class SamplesApi(Resource):

    def put(self):
        return RTC.cloud_samples(float(api.payload))

    def get(self):
        return RTC.get_cloud_samples()

    
@api.route("/loop")
class Loop(Resource):

    def post(self):
        return RTC.close_loop()

    def delete(self):
        return RTC.open_loop()

@api.route("/loop/frequency")
class Frequency(Resource):

    def put(self):
        freq = api.payload
        return RTC.frequency(freq)
    
@api.route("/threshold")
class Pid(Resource):

    def get(self):
        return RTC.get_threshold()

    def put(self):
        threshold = api.payload
        return RTC.threshold(threshold)

@api.route("/pid")
class Pid(Resource):

    def put(self):
        pid = api.payload
        return RTC.pid(pid)
    
@api.route("/snapshot")
class PlotFileApi(Resource):

    def post(self):
        """Show RTC related data"""
        
        filename = RTC.save()
        
        return filename

@api.route("/command")
class Teccam(Resource):

    def post(self):
        return RTC.teccam_http_start()

    def put(self):
        return RTC.teccam_custom_command(api.payload)
    
@api.route("/reset")
class ResetApi(Resource):

    def post(self):
        return RTC.reset()

@api.route("/resurrect")
class ResurrectApi(Resource):

    def post(self):
        #return RTC.resurrect()
        return RTC.resurrect2()
