import typing as t
import urllib.parse

from flask import Blueprint, abort, render_template, request, send_from_directory
from pymongo import MongoClient

from .config import dbadmin, dbpassword

main = Blueprint("main", __name__, url_prefix="/")

username = urllib.parse.quote_plus(dbadmin)
password = urllib.parse.quote_plus(dbpassword)
myclient = MongoClient('mongodb://%s:%s@149.202.41.75:27017' % (username, password))
mydb = myclient["actesdb"]
dbhouse = mydb["house"]


# (unproudly borrowed from the flask-pymongo's helpers :)
from flask import abort
def find_one_or_404(self, *args, **kwargs):
      """Find a single document or raise a 404.

      This is like :meth:`~pymongo.collection.Collection.find_one`, but
      rather than returning ``None``, cause a 404 Not Found HTTP status
      on the request.

      .. code-block:: python

          @app.route("/user/<username>")
          def user_profile(username):
              user = mongo.db.users.find_one_or_404({"_id": username})
              return render_template("user.html",
                  user=user)

      """
      found = self.find_one(*args, **kwargs)
      if found is None:
          abort(404)
      return found


@main.route("/")
def home():
    """home route"""
    return render_template("home.html")


@main.route("/about/")
def about():
    """about route"""
    return render_template("about.html")


@main.route("/actes/")
def corpora_all():
    """copora all

    lists houses
    sample_house_names = ["Bourbon", "Berry", "Anjou", ...]
    """
    houses = list(dbhouse.find())
    return render_template("corpora_all.html", houses=houses)


@main.route("/actes/<house>")  # dont put a slash at the end
def actes(house):
    """actes route"""
    #house = "Berry"
    # FIXME faire des princes correspondant à la maison (house) sélectionnée
    # FIXME : supprimer ces ids
    # Corpus > Bourbon
    princes = [("Charles Ier de Bourbon", 36), ("Agnès de Bourgogne", 38),
               ("Marie de Berry", 39)]
    return render_template("corpus.html", house=house, princes=princes)


# FIXME SLUGGIFY the house
# FIXME supprimer cet id de prince et remplacer par le slug du prince
@main.route("/actes/<house>/<prince>")  # don't put a slash at the end
def prince_corpus(house=None, prince=None):
    """copora prince, **timeline view**"""
    return "FIXME"


# FIXME : slugigy this route (remove acte_id) (prince and acte)
# FIXME: isn't it /acte/house/prince/slug instead of /acte/prince/slug ?
@main.route("/acte/<prince>/<acte_id>")  # don't put a slash at the end
def acte(house=None, prince=None, acte_id=None):
    """specific prince's acte"""
    return "FIXME"

@main.route("/contact")
def contact() -> t.Text:
    """Displays the Contact page"""
    return render_template("contact.html", title="Contact")


@main.route("/termsofservice")
def terms() -> t.Text:
    """Displaysthe T&C page."""
    return render_template("terms.html", title="Mentions légales")


@main.route("/privacy")
def privacy() -> t.Text:
    """Displays the privacy policy page."""
    return render_template("privacy.html", title="Politique de confidentialité")
