import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class SongSearch {
	private Song[] database;
	
	public static void main(String[] args) {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		SongSearch search = new SongSearch();
		search.readFile("songlist.txt");
		String input = null, searchTerm = null;
		do {
			System.out.println("What would you like to do?");
			System.out.println("\t1. Search for a song");
			System.out.println("\t2. Search for an artist");
			System.out.println("\t3. Quit");
			try {
				input = in.readLine().trim();
				if (!input.equals("3")) {
					System.out.print("Enter Search term: ");
					searchTerm = in.readLine().trim();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (input.equals("1")) {
				int result = search.searchForTitleAuto(searchTerm);
				System.out.println("Artist: " + search.database[result].getArtist());
			} else if (input.equals("2")) {
				for (int i = 0; i < search.database.length; i++) {
					if (search.database[i].getArtist().equals(searchTerm)) {
						System.out.println("Index: " + i + " Title: " + search.database[i].getTitle());
					}
				}
			}
		} while (!input.equals("3"));
	}
	
	public void readFile(String fileName) {
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String nextLine = null;
			
			int count = 0;
			while ((nextLine = in.readLine()) != null) {
				count++;
			}
			in.close();
			
			database = new Song[count];
			in = new BufferedReader(new FileReader(fileName));
			
			count = 0;
			while ((nextLine = in.readLine()) != null) {
				int split = nextLine.indexOf('-');
				String firstStr = nextLine.substring(0, split).trim();
				String secondStr = nextLine.substring(split + 1).trim();
				
				database[count] = new Song(firstStr, secondStr);
				count++;
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int searchForTitle(String term) {
		int first = 0, last = database.length - 1, middle;
		
		int count = 0;
		
		while (last >= first) {
			middle = (first + last) / 2;
			int comparison = term.compareToIgnoreCase(database[middle].getTitle());
			if (comparison == 0) {
				System.out.println("Found in " + count + " comparisons");
				return middle;
			} else if (comparison > 0) {
				first = middle + 1;
			} else {
				last = middle - 1;
			}
			count++;
		}
		System.out.println("Not found after " + count + " comparisons");
		return -1;
	}
	
	public int searchForTitleAuto(String term) {
		int index = Arrays.binarySearch(database, term);
		return index;
	}
}

class Song implements Comparable {
	private String title;
	private String artist;
	
	public Song(String title, String artist) {
		this.title = title;
		this.artist = artist;
	}
	
	public String getTitle() {
		return title;
	}
	
	public String getArtist() {
		return artist;
	}
	
	public int compareTo(Object o) {
		return this.title.compareTo((String)o);
	}
}


