package base;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Folder implements Comparable<Folder> {

	private ArrayList<Note> notes;
	private String name;
	
	public Folder(String name) {
		this.name = name;
		notes = new ArrayList<Note>();
	}
	
	public void addNote(Note aNote) {
		notes.add(aNote);
	}
	
	public String getName() {
		return name;
	}
	
	public ArrayList<Note> getNotes() {
		return notes;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		// Remove per lab2.pdf requirement
		//if (getClass() != obj.getClass())
		//	return false;
		Folder other = (Folder) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		int nText = 0;
		int nImage = 0;
		
		for (Note note : notes) {
			if (note instanceof TextNote) {
				nText += 1;
			} else if (note instanceof ImageNote) {
				nImage += 1;
			}
		}
		
		return name + ':' + nText + ':' + nImage;
	}

	@Override
	public int compareTo(Folder o) {
		// For class Folder, we compare its name.
		// Folder with smaller name is considered as smaller. 
		return getName().compareTo(o.getName());
	}
	
	public void sortNotes() {
		Collections.sort(notes);
	}

	public List<Note> searchNotes(String keywordsWithOr) {
		ArrayList<Note> matchedNotes = new ArrayList<Note>();
		
		// Make it case-insensitive by toLowerCase all strings
		keywordsWithOr = keywordsWithOr.toLowerCase();
		String[] keywordsStringList = keywordsWithOr.split(" or ");
		String[][] keywordsList = new String[keywordsStringList.length][];
		
		for (int i = 0; i < keywordsStringList.length; i++) {
			keywordsList[i] = keywordsStringList[i].split(" ");
		}
		
		for (Note note : notes) {
			for (String[] keywords : keywordsList) {
				boolean matched = true;
				for (String keyword : keywords) {
					if (!note.matchKeyword(keyword)) {
						matched = false;
						break;
					}
				}
				if (matched) {
					matchedNotes.add(note);
					break;
				}
			}
		}
		
		return matchedNotes;
	}
}

@@ -1701,32 +1701,34 @@
 tring%5B%5D keywords
+Or
 StringList = key
@@ -1750,12 +1750,22 @@
 it(%22
- or 
+(?%3C!or) (?!or)
 %22);%0D
@@ -1804,32 +1804,34 @@
  String%5Bkeywords
+Or
 StringList.lengt
@@ -1863,32 +1863,34 @@
  0; i %3C keywords
+Or
 StringList.lengt
@@ -1929,16 +1929,18 @@
 keywords
+Or
 StringLi
@@ -1952,16 +1952,19 @@
 .split(%22
+ or
  %22);%0D%0A%09%09
@@ -1999,16 +1999,47 @@
 tes) %7B%0D%0A
+%09%09%09boolean andMatched = true;%0D%0A
 %09%09%09for (
@@ -2087,17 +2087,19 @@
 boolean 
-m
+orM
 atched =
@@ -2095,27 +2095,28 @@
 orMatched = 
-tru
+fals
 e;%0D%0A%09%09%09%09for 
@@ -2155,17 +2155,16 @@
 %09%09%09%09if (
-!
 note.mat
@@ -2188,25 +2188,27 @@
 )) %7B%0D%0A%09%09%09%09%09%09
-m
+orM
 atched = fal
@@ -2196,36 +2196,35 @@
 %09%09%09%09orMatched = 
-fals
+tru
 e;%0D%0A%09%09%09%09%09%09break;
@@ -2248,17 +2248,20 @@
 %09%09%09%09if (
-m
+!orM
 atched) 
@@ -2260,24 +2260,84 @@
 atched) %7B%0D%0A%09
+%09%09%09%09andMatched = false;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09if (andMatched) %7B%0D%0A
 %09%09%09%09matchedN
@@ -2357,28 +2357,8 @@
 );%0D%0A
-%09%09%09%09%09break;%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%7D

Fix bugs reading specification wrongly.