package ljp37.assignment1;

import java.util.HashMap;
import java.util.Map;

import cs2110.assignment1.Genome;

/**
 * A genome is a collection of unique genes.
 * <p>
 * Store the genes and assign them unique numbers.  The tricky part is that the same gene
 * may come up multiple times, and we only want to save and number it once.
 * <p>
 * Hint: Use a HashMap using the gene itself as the "key".  If a gene is already known, return
 * its old number.  Otherwise, add it to the HashMap and assign it the next sequence number
 *<p>
 * Not shown: the constructor will be simple, just "public Genes();"
 *<p>
 * When creating your HashMap, remember that there will be a total of 100 to 200
 * genes in the overall animals database
 */
public class MyGenome implements Genome {
	
	HashMap<String,Integer> genes = new HashMap<String,Integer>();
	HashMap<Integer, String> geneIndex = new HashMap<Integer, String>();
	int i = 0;
	int n;
	
	public MyGenome() {
		genes = new HashMap<String, Integer>();
		geneIndex = new HashMap<Integer, String>();
	}
	
	/**
	 * Adds a gene if not previously known, else returns the old gene number
	 *
	 * @param  gene a gene string
	 * @return      number of the added gene, or the previous number if gene already known
	 */
	public int addGene(String Gene) {
		
		n = lookupGene(Gene);
		
		if(n != -1)  {
		return n;
		}
		
		i++;
		geneIndex.put(i, Gene);
		genes.put(Gene, i);
		return i;
	}

	/** Finds a Gene and returns its number, else returns -1 if unknown
	 * 
	 * @param gene
	 * @return number of requested gene, or -1 if it is not in the genome
	 */
	public int lookupGene(String Gene) {	
		for(String value  : geneIndex.values()) {
			if(value.equals(Gene)) {
				return genes.get(Gene);
			}
		}
		return -1;
	}

}
