
package usda.weru.weps;

import java.util.StringTokenizer;
import usda.weru.util.*;

/**
 * Class containing methods used to manipulate latitude and longitude
 * display and storage.
 * 
 * @author wjr
 * @version 1.0
 */
public class LatLongOld {

	String latStr = "40";
	String latSign = "N";
	String longStr = "100";
	String longSign = "W";
//	double latNum = 0.0;
//	double longNum = 0.0;

	LatLongOld() {
	}

	/**
	 * Single argument constructor that creates the required latitude or longitude
         * and sign assigned based on the its location like N or E are +ve while
         * S & W are -ve.
	 * @param inpStr Direction of the latitude or the longitude created/assigned.
	 */
	public LatLongOld(String inpStr) {
                Util.debugPrint(true, inpStr);		
		StringTokenizer st = new StringTokenizer(inpStr, ";");
		if (st.countTokens() == 2) {
			String temp = st.nextToken();
			if (temp.indexOf('-') > -1) {
				latSign = "S";
				latStr = temp.substring(1);
			} else if (temp.indexOf("+") > -1) {
				latSign = "N";
				latStr = temp.substring(1);
			} else {
				latSign = "N";
				latStr = temp;
			}
			temp = st.nextToken();
			if (temp.indexOf('-') > -1) {
				longSign = "W";
				longStr = temp.substring(1);
			} else if (temp.indexOf("+") > -1) {
				longSign = "E";
				longStr = temp.substring(1);
			} else {
				longSign = "E";
				longStr = temp;
			}
		} else {
			//System.err.println("LL_LL: bad latlong input -> " + inpStr);
		}
	}

	void setLatitude(String inpLat) {
		latStr = inpLat;
	}

	void setLongitude(String inpLong) {
		longStr = inpLong;
	}

	void setLatitudeSign(String inpLatSign) {
		latSign = inpLatSign;
		if (latSign.equals("+") || latSign.equals("N")) {
			latSign = "N";
		} else if (latSign.equals("-") || latSign.equals("S")) {
			latSign = "S";
		}
              //  //System.out.println("latLong:latitude Sign set to:"+latSign);
	}

	void setLongitudeSign(String inpLongSign) {
		longSign = inpLongSign;
		if (longSign.equals("+") || longSign.equals("E") ) {
			longSign = "E";
		} else if (longSign.equals("-") || longSign.equals("W")) {
			longSign = "W";
		}
               // //System.out.println("latLong:longitude Sign set to:"+longSign);
	}

	/**
	 * Returns the latitude assigned like 50 degrees, etc.
	 * @return The latitude in a string format.
	 */
	public String getLatitude() {
		return latStr;
	}

	String getSignedLatitude() {
		return (latSign.equals("N") ? "+" : "-") + latStr;
	}
	
	/**
	 * Gets the sign of the latitude like "North" as +ve or "South" as -ve
	 * @return The sign in a string format
	 */
	public String getLatitudeSign() {
		return latSign;
	}
	
	/**
	 * Returns the longitude assigned like 60 degrees, etc.
	 * @return The longitude in a string format.
	 */
	public String getLongitude() {
		return longStr;
	}

        
	String getSignedLongitude() {
		return (longSign.equals("E") ? "+" : "-") + longStr;
	}

	/**
	 * Gets the sign of the longitude like "East" as +ve or "West" as -ve
	 * @return The sign in a string format
	 */
	public String getLongitudeSign() {
		return longSign;
	}

	/**
	 *
	 * @return
	 */
	public double getLat() {
        try {
        return Double.parseDouble(getSignedLatitude().trim());
        } catch (NumberFormatException nfe) {
            return 0.0;
        }
    }

	/**
	 *
	 * @return
	 */
	public double getLon() {
        try {
        return Double.parseDouble(getSignedLongitude().trim());
        } catch (NumberFormatException npe) {
            return 0.0;
        }
    }
    
	/**
	 * Converts the value of an object to a string
	 * @return The new string after conversion happened.
	 */
    @Override
	public String toString() {
		String temp = (latSign.equals("N") ? "+" : "-") + latStr + ";" + 
						  (longSign.equals("E") ? "+" : "-") + longStr;
////System.out.println("LL_tS: " + latSign + " " + latStr + " " + longSign + " " + longStr + " --> " + temp);		
		return temp;
	}
}