#!/usr/bin/env python3
#coding: utf-8
from flask import Flask, render_template, Markup, request, redirect, url_for
from classes import note
from funcs import dumpnotes, getnotes, catnotes, delnote, findnote, addnote, mknotedir, exportnotes

#!---------- squiNotes.py ----------
# My notes-taking app
#-----------------------------!


#----------! MAIN
app = Flask(__name__)
mknotedir()

@app.route('/', methods=['GET'])
def render():
    #Delete has been clicked
    try:
        todelete = request.args.get("delete")
        if todelete is not None:
            delnote(int(todelete))
    except Exception as e:
        pass

    #Edit has been clicked
    try:
        toedit = request.args.get("edit")
        if toedit is not None:
            return redirect(url_for('edit', notenumber=toedit))
    except Exception as e:
        pass

    #Read has been clicked
    try:
        toread = request.args.get("toread")
        if toread is not None:
            return redirect(url_for('readmode', note=(int(toread))))
    except Exception as e: 
        print(e)
        pass
    
    return render_template("homepage.html", nr = catnotes(getnotes()))

#Export mode
@app.route('/export', methods=['GET'])
def rawnotes():
    return render_template("export.html", rawnotes = exportnotes())

#Read mode
@app.route('/readmode', methods=['GET','POST'])
def readmode():
    if request.method == 'GET':
        notenumber = request.args.get("note")
        mynote = findnote(int(notenumber))
    return render_template("read.html", note=mynote.flaskrender())


#Edition mode
@app.route('/edit', methods=['GET', 'POST'])
def edit():
    import time
    if request.method == "GET":
        print("GET")
        notenumber = request.args.get("notenumber")
        mynote = findnote(int(notenumber))
        return render_template("edit.html", notenumber=notenumber, ntitle=mynote.title, ntext=mynote.text)
    if request.method == "POST":
        print("POST")
        print(request.args.get("submit"))
        notetitle = request.form['title']
        notetext = request.form['text']
        notenumber = int(request.form['notenumber'])
        delnote(notenumber)
        rightnow = int(time.time())
        newnote = note(createtime=notenumber, modtime=rightnow, title=notetitle, text=notetext)
        addnote(newnote)
        return render_template("read.html", note=newnote.flaskrender())


#Basic route, allows note creation
@app.route('/', methods=['POST'])
def homepage():
    import time
    #New note
    try:
        notetitle = request.form['title']
        notetext = request.form['text']
        rightnow = int(time.time())
        newnote = note(createtime=rightnow, modtime=rightnow, title=notetitle, text=notetext)
        addnote(newnote)
    except:
        pass
    return render_template("homepage.html", nr = catnotes(getnotes()))



if __name__ == '__main__':
    app.run(host="0.0.0.0")
