#!/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('dm', description='Real Time Control')

    
@api.route("/zonal")
class ModeApi(Resource):

    def post(self):
        mode = api.payload
        return RTC.uploadRTC(mode)

@api.route("/zonal/<int:actuator>")
class ModeZonalApi(Resource):

    def put(self):
        strain = api.payload
        status = RTC.zonalCommand([actuator,strain])
        if not status:
            return status, 200
        else:
            return status, 400

@api.route("/rotation")
class ModeRotationApi(Resource):
    
    def put(self):
        action_start = api.payload
        return RTC.rotateNCPA(action_start)
    
    def delete(self):
        action_stop = api.payload
        return RTC.rotateNCPA(action_stop)

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

@api.route("/ncpa")
class Ncpa(Resource):

    def get(self):
        print("qui GET 'mode/ncpa' rtc.py")
        return RTC.todos_flat

    def put(self):
        print("qui PUT 'mode/ncpa' rtc.py")
        name = api.payload
        print(name)
        return RTC.load_ncpa(name)

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

@api.route("/shape")
class Shape(Resource):

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

@api.route("/tiptilt")
class TipTilt(Resource):

    # def get(self):
    #     return RTC.todos

    def put(self):
        modes = api.payload
        if modes == None:
            return RTC.tiptilt( absolute=True )
        else:
            return RTC.tiptilt( list(map(float, modes)) )

@api.route("/flat")
class Flat(Resource):

    def get(self):
        return RTC.todos_flat

    def put(self):
        name = api.payload
        print(name)
        return RTC.load_flat(name)

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