#!/usr/bin/python

import postit, sys, string


def print_notes(notes):
	for n in range(len(notes)):
		print "%3d. %s" % (n+1, notes[n][0])

def chooser(notes, text, to_do):
	which=raw_input(text)
	try:
		n=int(which)-1
		if n<0: raise IndexError, ''
		to_do(notes[n])
	except (ValueError, IndexError):
		print "Invalid number"
		

def read():
	notes=postit.list()
	print_notes(notes)
	chooser(notes, "Which one do you want to read? ",
	        lambda x: sys.stdout.write(x[1]))

def remove():
	notes=postit.list()
	print_notes(notes)
	chooser(notes, "Which one do you want to remove? ",
	        lambda x: postit.remove(x[0]))

def create():
	title=raw_input("What is the title? ")
	text=sys.stdin.read()
	postit.new(text, title)

def change():
	def _change(title):
		postit.remove(title)
		text=sys.stdin.read()
		postit.new(text, title)
	notes=postit.list()
	print_notes(notes)
	chooser(notes, "Which one do you want to change? ",
	        lambda x,_change=_change: _change(x[0]))

message="What do you want to do [Read/delete/new/change/quit]? "

while 1:
	choice=raw_input(message)
	if string.lower(choice[0])=='r':
		read()
	if string.lower(choice[0])=='d':
		remove()
	if string.lower(choice[0])=='n':
		create()
	if string.lower(choice[0])=='c':
		change()
	if string.lower(choice[0])=='q':
		break
