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

from flask import Blueprint
from flask_restx import Api
from flask_httpauth import HTTPBasicAuth
import json  # for users

# REST API
from .home import api as home_api
from .blocks import api as blocks_api
from .templates import api as templates_api
from .other import api as other_api

from .dome import api as dome_api
from .telescope import api as telescope_api
from .camera import api as camera_api
from .webcam import api as webcam_api
from .environment import api as environment_api

# WEB VIEW
from .webcam import web as webcam_web
from .environment import web as environment_web
from .home import web as home_web
from .blocks import web as blocks_web
from .templates import web as templates_web
from .other import web as other_web
from .control import web as control_web
from .init import web as init_web


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

api_blueprint = Blueprint('api', __name__)

api = Api(api_blueprint,
          title='OARPAF APIs for observatory control',
          version='1.0',
          description='A description',
          )

api.add_namespace(home_api)
api.add_namespace(blocks_api)
api.add_namespace(templates_api)
api.add_namespace(other_api)

api.add_namespace(dome_api)
api.add_namespace(telescope_api)
api.add_namespace(camera_api)
api.add_namespace(webcam_api)
api.add_namespace(environment_api)


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

auth = HTTPBasicAuth()

users_file = 'users.json'

try:
    with open(users_file) as json_file:
        users = json.load(json_file)
except FileNotFoundError as e:
    print(f"{users_file} containing user:pass not found")
    exit(0)


@auth.verify_password
def verify(username, password):
    if not (username and password):
        return False
    return users.get(username) == password


web_blueprint = Blueprint('web', __name__)

web = Api(web_blueprint,
          title='OARPAF Web view for observatory control',
          version='1.0',
          description='A description',
          #          decorators=[auth.login_required], # on every page.
          )

web.add_namespace(home_web)
web.add_namespace(blocks_web)
web.add_namespace(templates_web)
web.add_namespace(webcam_web)
web.add_namespace(environment_web)
web.add_namespace(other_web)
web.add_namespace(control_web)
web.add_namespace(init_web)
