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

'''Web pages'''

# Third-party modules
from astropy.time import Time
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 VIEW
############################

web = Namespace('/', description='All pages')

@web.route("/blocks/")
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("/blocks/<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("/blocks/<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]


@web.route("/control/")
class ControlWeb(Resource):

    def get(self):
        data = {}
        return make_response(render_template("control.html", data=data))

    
@web.route("/display/")
class DisplayWeb(Resource):

    def get(self):
        data = {}
        return make_response(render_template("display.html", data=data))


@web.route("/environment/")
class EnvironmentWeb(Resource):
    """ Weatherpage  """

    def get(self):
        """ Display the meteo """
        data = Status().get()

        exclude = ["Barometric_trend", "Battery_status", "Battery_volts",
                   "ET_day", "ET_month", "ET_year",
                   "Storm_start_date", "Forecast_rule_number", "Forecast_icon"
                   ]

        for e in exclude:
            data["external"].pop(e, None)
        
        return make_response(render_template("meteo.html", data=data))

        
@web.route("/init/")
class InitWeb(Resource):

    def get(self):
        data = {}
        return make_response(render_template("init.html", data=data))


@web.route("/other/button/")
class ButtonWeb(Resource):

    def get(self):
        data = {}
        return make_response(render_template("other/button.html", data=data))

    
@web.route("/other/fits/")
class FitsWeb(Resource):

    def get(self):
        # System modules
        import io

        # Third-party modules
        from astropy.io import fits
        from flask import send_file
        
        with open("./data/fits/test.fits", mode='rb') as hdul:
            data = hdul.read()
            fitsdata = send_file(
                io.BytesIO(data),
                mimetype='image/fits',
                as_attachment=True,
                download_name='ciccio.fits')

        return make_response(fitsdata)


@web.route("/sequencer/")
class BobWeb(Resource):

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

@web.route("/testfulvio/test/")
class TestWeb(Resource):
    """First get/post test web page of Fulvio."""

    def get(self):
        """GET fulvio test web page."""
        data = {}
        return make_response(render_template("other/fulvio.html", data=data))



@web.route("/webcam/")
class WebcamWeb(Resource):

    def get(self):
        data = {}
        return make_response(render_template("webcam.html", data=data))

