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

from flask import render_template, make_response, request
from flask_restx import Namespace, Resource

from .data_access_object import ObservationBlockObject as DAO
from .data_access_object import guess

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

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

api = Namespace('blocks', description='Observation blocks')


@api.route("/")
class BlockList(Resource):
    """Show and create OBs"""

    def get(self):
        """Show all observation OB files"""
        return dao.todos

    def post(self):
        """Create a new OB file based on name"""
        name = api.payload
        dao.create(name)
        return "", 204

    def delete(self):
        """Delete an OB file based on name"""
        name = api.payload
        dao.delete(name)
        return "", 204


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

    def get(self, name):
        """Show a specific OB"""
        return dao.show(name)

    def put(self, name):
        """Update the OB"""
        data = api.payload
        for d in data:
            for key, val in d["params"].items():
                d["params"][key] = guess(val)
        dao.update(name, data)
        return "", 204

    def post(self, name):
        """Add a template to the selected OB"""
        content = dao.content(name)
        new_data = tpl.content(api.payload)
        data = content + new_data
        dao.update(name, data)
        return "", 204

    def delete(self, name):
        """Delete a template instance in the selected OB"""
        instance = api.payload
        data = dao.content(name)
        data.pop(instance)
        dao.update(name, data)
        return "", 204

############################
# 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("ob_list.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("ob.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("ob.html", data=data))
        # return [data]
