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

'''REST API for Dome related operations'''

# Third-party modules
from flask import request

# Custom modules
from config import constants
from .baseresource import ResourceDev


# @api.route("/position")
class Position(ResourceDev):
    '''Get the position of the dome.'''

    def get(self):
        '''Check the azimuth of the dome, and if
        this corresponds to the parking position.'''
        res = {
            "raw": self.dev.is_parked,
            "response": {
                "azimuth": self.dev.azimuth,
                "parked": constants.yes_no[self.dev.is_parked],
            },
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/shutter")
class Shutter(ResourceDev):
    '''Dome Shutter'''

    def get(self):
        '''Check the state of the dome shutter.'''
        res = {
            "raw": self.dev.shutter,
            "response": constants.open_state[self.dev.shutter],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

# @api.route("/shutter/movement")
class ShutterMovement(ResourceDev):
    '''Manage the movement of the dome shutter.'''

    def post(self):
        '''Move the shutter to a new position
        (open or closed).'''
        target = request.json
        self.dev.open = target
        res = {
            "raw": self.dev.open,
            "response": constants.open_state[self.open],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

    def delete(self):
        '''Stop the movement of the shutter.'''
        res = {
            "response": self.dev.abort(),
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/position/slaved")
class PositionSlaved(ResourceDev):
    '''Manage the minioning of the shutter
    to the telescope.'''

    def get(self):
        '''Check if the dome is slaved
        to the telescope or not.'''
        res = {
            "raw": self.dev.slave,
            "response": constants.yes_no[self.dev.slave],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

    def put(self):
        '''Make the dome slaved to the telescope
        or release it.'''
        slaved = request.json
        self.dev.slave = slaved
        res = {
            "raw": self.dev.slave,
            "response": constants.yes_no[self.dev.slave],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/position/sync")
class PositionSync(ResourceDev):
    '''Manage the reference azimuth of the dome'''

    def put(self):
        '''Tell the dome that its current position
        corresponds to a given azimuth.'''
        new_azimuth = request.json
        res = {
            "response": self.dev.sync(new_azimuth),
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/position/movement")
class PositionMovement(ResourceDev):
    '''Manage the movement of the dome.'''

    # @api.marshal_with(model_bool, 200)
    def get(self):
        '''Return if the dome is moving or not.'''
        res = {
            "raw": self.dev.is_moving,
            "response": constants.yes_no[self.dev.is_moving],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

    def delete(self):
        '''Stop the movement of the dome.'''
        res = {
            "response": self.dev.abort(),
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/position/movement/park")
class PositionMovementPark(ResourceDev):
    '''Manage the dome parking.'''

    def post(self):
        '''Park the dome.'''
        res = {
            "response": self.dev.park(),
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/position/movement/azimuth")
class PositionMovementAzimuth(ResourceDev):
    '''Manage the position of the dome.'''

    def post(self):
        '''Set a new dome azimuth.'''
        azimuth = request.json
        self.dev.azimuth = azimuth
        res = {
            "response": self.dev.azimuth,
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res


# @api.route("/connection")
class Connection(ResourceDev):
    '''Manage the connection to ASCOM.'''

    def get(self):
        '''check if the dome is connected to ASCOM.'''
        conn = self.dev.connection
        res = {
            "raw": conn,
            "response": constants.yes_no[conn] if bool(conn) else False,
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

    def put(self):
        '''Connect or disconnect the telescope to ASCOM.'''
        connection = request.json
        self.dev.connection = connection
        res = {
            "raw": self.dev.connection,
            "response": constants.yes_no[self.dev.connection],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res
