package eyeglassesmain;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class EyeglassDatabase {
	private ArrayList<Glasses> glasses;

	public EyeglassDatabase(File file) throws FormattingException{
		System.out.println(checkAxis(150,179,30));
		
		try {
			glasses = Reader.readFile(file);
		} catch (FormattingException e) {
			throw e;
		}
		EyeglassesMain.log("Database read into RAM");
	}

	public ArrayList<Glasses> getGlasses(){
		return glasses;
	}

	/*
	 * 
	 * This is the main search routine for glasses
	 * 
	 * Searches through read-in database and returns that array filtered
	 * 
	 */
	public ArrayList<Glasses> search(double rsph, double rcyl, int raxis, double lsph,
			double lcyl, int laxis){
		ArrayList<Glasses> hits = new ArrayList<Glasses>();

		int searches = 0;

		ArrayList<Glasses> possibles = null;

		while(searches <= 4 && hits.size() <= 15){
			searches++;
			possibles = new ArrayList<Glasses>();

			for(int i = 0; i < glasses.size(); i++){
				if(!(glasses.get(i).getRcyl() >= rcyl)){
					continue;
				}
				
				if(!(glasses.get(i).getLcyl() >= lcyl)){
					continue;
				}
				
				if(!(checkAxis(glasses.get(i).getRaxis(), raxis, 30))){
					continue;
				}
				
				if(!(checkAxis(glasses.get(i).getLaxis(), laxis, 30))){
					continue;
				}

				possibles.add(glasses.get(i));
			}

			Iterator<Glasses> iter = possibles.iterator();
			
			while(iter.hasNext()){
				Glasses glasses = iter.next();
				
				double margin = 0;
				if(searches == 1){
					margin = .25;
				}
				else if(searches == 2){
					margin = .5;
				}
				else if(searches == 3){
					margin = .75;
				}
				else{
					margin = 1;
				}
				
				if(rsph < 0){
					if(!(glasses.getRsph() == rsph || 
							glasses.getRsph() == rsph + margin)){
						iter.remove();
						continue;
					}
				}
				if(rsph > 0){
					if(!(glasses.getRsph() == rsph || 
							glasses.getRsph() == rsph - margin)){
						iter.remove();
						continue;
					}
				}

				if(lsph < 0){
					if(!(glasses.getLsph() == lsph || 
							glasses.getLsph() == lsph + margin)){
						iter.remove();
						continue;
					}
				}
				if(lsph > 0){
					if(!(glasses.getLsph() == lsph || 
							glasses.getLsph() == lsph - margin)){
						iter.remove();
						continue;
					}
				}
			}



			//hits.addAll(possibles);
			for(Glasses glasses : possibles){
				if(!hits.contains(glasses)){
					hits.add(glasses);
				}
			}
		}
		
		Collections.sort(hits);

		return hits;
	}

	public ArrayList<Glasses> search(String rsph, String rcyl, String raxis, String lsph,
			String lcyl, String laxis){
		return search(Double.valueOf(rsph), Double.valueOf(rcyl), Integer.valueOf(raxis), 
				Double.valueOf(lsph), Double.valueOf(lcyl), Integer.valueOf(laxis));
	}

	public boolean checkAxis(int axis1, int axis2, int range){
		
		axis1 = axis1 % 180;
		axis2 = axis2% 180;
		
		if(axis2 + range > 180){
			if(!(axis1 <= axis2 + range - 180 || axis1 >= axis2 - range)){
				return false;
			}
		}
		else if(axis2 - range < 0){
			if(!(axis1 <= axis2 + range || axis1 >= axis2 - range + 180)){
				return false;
			}
		}
		else{
			if(!isBetween(axis1, axis2 - range, axis2 + range)){
				return false;
			}
		}
		
		return true;
	}

	public boolean isBetween(double num, double low, double high){
		return (num >= low) && (num <= high);
	}
}

@@ -912,9 +912,9 @@
  %3C= 
-4
+8
  && 
@@ -1516,16 +1516,17 @@
 %09double 
+l
 margin =
@@ -1530,39 +1530,25 @@
 n = 
+0.
 0;%0A%09%09%09%09
-if(searches == 1)%7B%0A%09%09%09%09%09
+double r
 marg
@@ -1556,28 +1556,22 @@
 n = 
-.25
+0.0
 ;%0A%09%09%09%09
-%7D
 %0A%09%09%09%09
-else 
 if(s
@@ -1582,20 +1582,20 @@
 hes 
-== 2
+%3C= 7
 )%7B%0A%09%09%09%09%09
 marg
@@ -1582,32 +1582,33 @@
 hes %3C= 7)%7B%0A%09%09%09%09%09
+l
 margin = .5;%0A%09%09%09
@@ -1605,37 +1605,21 @@
  = .
-5;%0A%09%09%09%09%7D%0A%09%09%09%09else if(
+25 * 
 searches
  == 
@@ -1618,21 +1618,16 @@
 ches
- == 3)%7B
+;
 %0A%09%09%09%09%09
+r
 marg
@@ -1635,11 +1635,15 @@
 n = 
-.75
+lmargin
 ;%0A%09%09
@@ -1661,16 +1661,17 @@
 e%7B%0A%09%09%09%09%09
+r
 margin =
@@ -1672,16 +1672,40 @@
 rgin = 1
+.00;%0A%09%09%09%09%09lmargin = 2.00
 ;%0A%09%09%09%09%7D%0A
@@ -1798,24 +1798,25 @@
 ) == rsph + 
+r
 margin))%7B%0A%09%09
@@ -1952,24 +1952,25 @@
 ) == rsph - 
+r
 margin))%7B%0A%09%09
@@ -2111,16 +2111,17 @@
  lsph + 
+l
 margin))
@@ -2265,16 +2265,17 @@
  lsph - 
+l
 margin))

Change margin code to go up a full diopter