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

# System modules
from flask_restx import Namespace, Resource, fields, Model

# Custom modules
import devices

api = Namespace('dome', description='Dome related operations')

position = api.model("position", {
        "azimuth": fields.Float(),
        "parked": fields.Boolean(),
    })

@api.route("/light")
class Light(Resource):
    '''Manage the dome illumination.'''

    @api.response(200, 'Success', fields.Boolean())
    def get(self):
        '''Check if the dome illumination is on or off.'''
        return devices.light.state

    @api.response(200, 'Success', fields.Boolean())
    def put(self):
        '''Switch on or off the dome illumination.'''
        state = api.payload
        devices.light.state = state
        return devices.light.state
    
@api.route("/position")
class Positiont(Resource):
    '''Get the position of the dome.'''
    
    @api.response(200, 'Success', position)
    def get(self):
        '''Check the azimuth of the dome, and if 
        this corresponds to the parking position.'''
        res = {
            "response": {
                "azimuth": devices.dom.azimuth,
                "parked": devices.dom.is_parked,
            },
            "error": devices.dom.error
            }
        return res
    
@api.route("/shutter")
class Shutter(Resource):
    '''Dome Shutter''' 
    
    @api.response(200, 'Success', fields.Integer())
    def get(self):
        '''Check the state of the dome shutter.'''
        return devices.dom.shutter

    
@api.route("/shutter/movement")
class ShutterMovement(Resource):
    '''Manage the movement of the dome shutter.'''
    
    @api.response(200, 'Success', fields.Boolean())
    def post(self):
        '''Move the shutter to a new position 
        (open or closed).'''
        target = api.payload
        devices.dom.open = target
        return devices.dom.open

    def delete(self):
        '''Stop the movement of the shutter'''
        pass
    
    
@api.route("/position/slaved")
class PositionSlaved(Resource):
    '''Manage the minioning of the shutter 
    to the telescope.'''
    
    @api.response(200, 'Success', fields.Boolean())
    def get(self):
        '''Check if the dome is slaved 
        to the telescope or not.'''
        res = {
            "response": devices.dom.slave,
            "error": devices.dom.error,
            }
        return res

    @api.response(200, 'Success', fields.Boolean())
    def put(self):
        '''Make the dome slaved to the telescope 
        or release it.'''
        slaved = api.payload
        devices.dom.slave = slaved
        res = {
            "response": devices.dom.slave,
            "error": devices.dom.error,
            }
        return res

    
@api.route("/position/sync")
class PositionSync(Resource):
    '''Manage the reference azimuth of the dome'''
    
    @api.response(200, 'Success', fields.Float())
    def put(self):
        '''Tell the dome that its current position 
        corresponds to a given azimuth.'''
        new_azimuth = api.payload
        devices.dom.sync = new_azimuth
        return devices.dom.azimuth

    
@api.route("/position/movement")
class PositionMovement(Resource):
    '''Manage the movement of the dome.'''
    
    @api.response(200, 'Success', fields.Boolean())
    def get(self):
        '''Return if the dome is moving or not.'''
        return devices.dom.is_moving

    def delete(self):
        '''Stop the movement of the dome.'''
        return devices.dom.abort()


@api.route("/position/movement/park")
class PositionMovementPark(Resource):
    '''Manage the dome parking.'''
    
    def post(self):
        '''Park the dome.'''
        return devices.dom.park()


@api.route("/position/movement/azimuth")
class PositionMovementAzimuth(Resource):
    '''Manage the position of the dome.'''
    
    @api.response(200, 'Success', fields.Float())
    def post(self):
        '''Set a new dome azimuth.'''
        azimuth = api.payload
        devices.dom.azimuth = azimuth
        res = {
            "response": devices.dom.azimuth,
            "error": devices.dom.error,
            }
        return res
