from flask import Flask, redirect, url_for, request, render_template
import uuid

# Create the application.
APP = Flask(__name__)

# Define different routes of the application.
@APP.route('/2ndpage', methods=['GET', 'POST'])
def drawPlz():
	title = "Lost Stories"
	if request.method == 'POST':
		print("hello")
		image = request.form["data"]
		print(image)
		idi = request.args.get('id')
		print(idi)
		with open('database/'+ str(idi) +'.txt', 'a+') as db:
			db.write(f"{ image }")
		return  redirect(url_for('saver'))
	name = request.form.get('lostobject')
	msg = "Sorry that you lost {name}. But {name} will be having fun somewhere."  
	return render_template("drawing.html", title=title, msg=msg)
	

@APP.route('/saver', methods=['GET', 'POST'])
def saver():
	return render_template("saver.html")



@APP.route('/', methods=['GET', 'POST'])
def index():

	title = "Lost Stories"

	# If something is submitted through the form...
	# ref: https://pythonbasics.org/flask-http-methods/
	if request.method == 'POST':
		id = uuid.uuid4() 
		print(id)
		# Read the values from the form element.
		name = request.form.get('lostobject', 'nana')
		text = request.form.get('text', 'haha')
		print(text)
		
		with open('database/'+ str(id) +'.txt', 'w') as db:
			db.write(f"{ name } \n{ text } \n")

		msg = "Thanks!"
		# return render_template('index.html', title=title, msg=msg)
		return redirect(url_for('drawPlz', title=title, msg=msg, id=id))
	
	# If the index page is loaded...
	msg = "Please submite the form below."
	return render_template('index.html', title=title, msg=msg)


if __name__ == '__main__':
	APP.debug = True
	APP.run(port = 5000)
