import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class TODO {
	
	ArrayList<String> list;
	BufferedReader in;
	BufferedWriter out;
	
	public static void main(String[] args) {
		TODO t = new TODO();
		t.init();
	}
	
	public void init() {
		list = new ArrayList<String>();
		try {
			in = new BufferedReader(new FileReader("todo.list"));
			readFile();
		} catch (FileNotFoundException e) {
			System.out.println(e);
			System.out.println("if this is first time you run this program, ignore this error");
		} catch (IOException e) {
		}
		try {
			out = new BufferedWriter(new FileWriter("todo.list"));
		} catch (IOException e) {
			System.out.println(e);
			System.exit(0);
		}
		System.out.println("type 'help' for help");
		printList();
		run();
	}
	
	public void readFile() throws IOException {
		String line;
		while ((line = in.readLine()) != null) {
			list.add(line);
		}
	}
	
	public void addLine(String line) {
		if (!(line == null || line.equals(""))) {
			list.add(line);
		}
	}
	
	public void removeLine(int id) {
		if (!(id > list.size()))
			list.remove(id);
	}
	
	public void saveList() {
		for (int i = 0; i < list.size(); i++) {
			String line;
			line = list.get(i);
			try {
				out.write(line + "\n");
			} catch (IOException e) {
				System.out.println(e);
				System.exit(0);
			}
		}
	}
	
	public void printList() {
		System.out.println("TODO:");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(i +": " + list.get(i));
		}
	}
	
	public void run() {
		while(true) {
			Scanner cin = new Scanner(System.in);
			String sin = cin.nextLine();

			if (sin.equals("exit")) {
				saveList();
				try {
					in.close();
					out.close();
				} catch (IOException e) {
					System.out.println(e);
				} catch (NullPointerException e) {
				}
				System.exit(0);
			}

			if (sin.startsWith("add")) {
				addLine(sin.substring(4));
				System.out.println("entry added: " + sin.substring(4));
			}

			if (sin.startsWith("remove")) {
				int index = Integer.parseInt(sin.substring(7));
				removeLine(index);
				System.out.println("entry removed: " + index);
			}

			if (sin.equals("list")) {
				printList();
			}

			if (sin.equals("help")) {
				System.out.println("=======================================================");
				System.out.println("add <something to do>    | adds todo-entry to list");
				System.out.println("remove <index>           | removes todo-entry from list");
				System.out.println("list                     | prints list");
				System.out.println("help                     | prints this help");
				System.out.println("exit                     | closes TODO");
				System.out.println("=======================================================");
			}
		}
	}

}