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

'''REST API for Focuser related operations'''

# Third-party modules
from flask import request

from .baseresource import ResourceDev


# @api.route("/focuser")
class Focuser(ResourceDev):
    '''Secondary mirror position.'''

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


# @api.route("/focuser/movement")
class FocuserMovement(ResourceDev):
    '''Manage the secondary mirror position.'''

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

    def put(self):
        '''Update the secondary mirror position.'''
        target = request.json
        self.dev.position = target
        res = {
            "response": self.dev.position,
            "error": self.dev.error,
        }
        return res

    # def delete(self):
    #     '''Stop the movement.'''
    #     return

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

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

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

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

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