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

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

# Third-party modules
from flask import request

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 = {
            "response": {
                "azimuth": self.dev.azimuth,
                "parked": 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 = {
            "response": 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 = {
            "response": self.dev.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 = {
            "response": 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 = {
            "response": 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 = {
            "response": 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.'''

        res = {
            "response": self.dev.connection,
            "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 = {
            "response": self.dev.connection,
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res
