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

'''REST API for Rotator related operations'''

# Third-party modules
from flask import request

from .baseresource import ResourceDev


# @api.route("/rotator")
class Rotator(ResourceDev):
    '''Field derotator position.'''

    def get(self):
        '''Retrieve the field derotator position.'''
        res = {
            "response": self.dev.position,
            "error": self.dev.error,
        }
        return res


# @api.route("/rotator/movement")
class RotatorMovement(ResourceDev):
    '''Manage the field derotator position.'''

    def get(self):
        '''Check if the field derotator is moving.'''
        res = {
            "response": self.dev.is_moving,
            "error": self.dev.error,
        }
        return res

    def post(self):
        '''Set the field derotator to a new position.'''
        target = request.json
        self.dev.position = target
        res = {
            "response": self.dev.position,
            "error": self.dev.error,
        }
        return res

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

    def get(self):
        '''Check if the rotator is connected.'''

        res = {
            "response": self.dev.connection,
            "error": self.dev.error,
        }
        return res

    def put(self):
        '''Connect or disconnect the rotator.'''

        connection = request.json
        self.dev.connection = connection
        res = {
            "response": self.dev.connection,
            "error": self.dev.error,
        }
        return res

