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

'''Web pages for observation blocks'''

# Third-party modules
from flask import make_response, render_template, request
from flask_restx import Namespace, Resource

from utils.data_access_object import ObservationBlockObject as DAO

dao = DAO("ob")
tpl = DAO("defaults")

web = Namespace('blocks', description='OB generator')

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

web = Namespace('blocks', description='OB generator')

@web.route("/")
class BlockListWeb(Resource):
    """Show and create OBs"""

    def get(self):
        """Show all observation OB files"""
        data = dao.todos
        return make_response(render_template("blocks.html", data=data, tpl=tpl.todos))


@web.route("/<string:name>")
@web.param("name", "The uniqe name of the OB")
class BlockWeb(Resource):
    """Show a selected OB, update it, or add a new
    template in it.
    """

    def get(self, name):
        """Show a specific OB"""
        included = request.args.get("included") or False
        data = dao.show(name)
        #data = block.get(name)
        return make_response(render_template("block.html",
                                             data=data, tpl=tpl.todos,
                                             included=included))


@web.route("/<string:name>/<int:instance>")
@web.param("name", "The uniqe name of the OB")
@web.param("instance", "The template instance in the selected OB")
class BlockInstanceWeb(Resource):

    def get(self, name, instance):
        """Show a specific template instance of the selected OB"""
        data = dao.show(name)
        data["content"] = [data["content"][instance]]
        return make_response(render_template("block.html", data=data))
        # return [data]
