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

from flask import Blueprint
from flask_restx import Api
from util import exception
from Ice import ConnectionRefusedException

############################
# REST API
############################

from .defaults import api as defaults_api
from .references import api as references_api
from .home import api as home_api
from .blocks import api as blocks_api
from .bob import api as bob_api
from .rtc import api as rtc_api
from .devices import api as devices_api
from .subsystems import api as subsystems_api
from .other import a as other_api

api_blueprint = Blueprint('api', __name__)

api = Api(api_blueprint,
          title='My Title',
          version='1.0',
          description='A description',
)

print("adding defaults api")
api.add_namespace(defaults_api)
print("adding references api")
api.add_namespace(references_api)
print("adding home api")
api.add_namespace(home_api)
print("adding blocks api")
api.add_namespace(blocks_api)
print("adding bob api")
api.add_namespace(bob_api)
print("adding rtc api")
api.add_namespace(rtc_api)
print("adding devices api")
api.add_namespace(devices_api)
print("adding subsystem api")
api.add_namespace(subsystems_api)
print("adding other api")
api.add_namespace(other_api)

@api.errorhandler(exception.TemplateException)
def handle_TemplateException(error):
    '''Return a custom message and 400 status code'''    
    return {'message': 'TemplateException'}, 200

@api.errorhandler(Exception)
def handle_IceConnectionRefusedException(ConnectionRefusedException):
    '''Return a custom message and 400 status code'''
    return {'message': 'ConnectionRefusedException'}, 200

############################
# WEB VIEW
############################

from .home import web as home_web
from .blocks import web as blocks_web
from .devices import web as devices_web
from .bob import web as bob_web
from .rtc import web as rtc_web
from .other import w as other_web

web_blueprint = Blueprint('web', __name__)

web = Api(web_blueprint,
          title='My Title',
          version='1.0',
          description='A description',
)

print("adding home web")
web.add_namespace(home_web)
print("adding blocks web")
web.add_namespace(blocks_web)
print("adding devices web")
web.add_namespace(devices_web)
print("adding bob web")
web.add_namespace(bob_web)
print("adding rtc web")
web.add_namespace(rtc_web)
print("adding other web")
web.add_namespace(other_web)
