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

# Third-party modules
from flask_restx import Namespace, Resource

# Custom modules
from util.instances import deo, RTC

api = Namespace('devices', description='Devices')

@api.route("/")
class DeviceList(Resource):
    """SHARK devices"""

    def get(self):
        resource = deo.readall()
        return resource

    def post(self):
        data = api.payload["data"]
        #deo.stop(data)
        return deo.home(data), 201

    def put(self):
        motion = api.payload["motion"]
        #action = api.payload["action"]
        data = api.payload["data"]
        deo.stop(data)

        print("posting this:")
        print(data)
        
        if motion == "named":
            return deo.move(data), 201
        elif motion == "relative":
            return deo.relative(data), 201
        elif motion == "absolute":
            return deo.absolute(data), 201

    def delete(self):
        data = api.payload["data"]
        return deo.abort(), 201


@api.route("/<string:device>")
class Device(Resource):
    def get(self, device):
        resource = deo.read(device)
        return resource

    
@api.route("/derotation")
class Derotation(Resource):

    def put(self):
        cmd = api.payload
        print("rtc.py -_____________________________-{}".format(cmd))
        return RTC.manageDerotation(cmd)


@api.route("/externals/soul")
class SoulMovementApi(Resource):

    def put(self):
        soul_pos = api.payload
        output = RTC.move_soul(soul_pos, apply_movement=True)
        print("PUT Received this: {}".format(output))
        if not output[0]:
            return output, 200
        else:
            return output, 400
    def post(self):
        soul_pos = api.payload
        output = RTC.move_soul(soul_pos, apply_movement=False)
        print("POST Received this: {}".format(output))
        if not output[0]:
            return output, 200
        else:
            return output, 400
