import flask
from flask_restful import Resource, Api, request, abort
from flask_cors import CORS, cross_origin

import echo_api.auth as Auth

# Endpoints
import echo_api.devices as DevicesAPI
import echo_api.device as DeviceInfoAPI
import echo_api.device as DeviceControlAPI

class EchoAPI:

    # Initialize Flask API Object
    app = flask.Flask(__name__)
    cors = CORS(app)
    app.config['CORS_HEADERS'] = 'Content-Type'
    api = Api(app)
    
    # Start API
    @staticmethod
    def init():
        EchoAPI.app.run(host='0.0.0.0', port=1337, debug=False)

    @staticmethod
    @app.route('/')
    def index():
        if not Auth.Auth.verify(request):
            return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
            
        return {'status': 'success', 'message': 'Echo API is healthy and running!'}, 200

    @staticmethod
    @app.errorhandler(404)
    def not_found(error):
        return {'status': 'failed', 'message': 'Endpoint not found!'}, 404
   
    api.add_resource(DevicesAPI.DevicesAPI, '/devices')
    api.add_resource(DeviceInfoAPI.DeviceInfoAPI, '/device/<string:uuid>')
    api.add_resource(DeviceControlAPI.DeviceControlAPI, '/device')

