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

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

from .data_access_object import SetupObject as DAO

dao = DAO("ob")

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

api = Namespace('templates', description='Default templates parameters')

@api.route("/")
class TemplateList(Resource):
    
    def get(self):
        """Show all template files"""
        return dao.todos

@api.route("/<string:name>")
@api.param("name", "The uniqe name of the template")
class Template(Resource):

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

web = Namespace('templates', description='Defaults template parameters')

@web.route("/")
class TemplateListWeb(Resource):

    def get(self):
        data = dao.todos
        print(data)
        return make_response(render_template("ob_list.html", data=data))

@web.route("/<string:name>")
class TemplateWeb(Resource):
    def get(self, name):
        included = request.args.get("included") or False
        data = dao.show(name)
        data["Template"] = data["Template"]
        return make_response(render_template("template.html",
                                             tpl=data,
                                             included=included))
