// This simple utility class is used by the SQLRetriever to provide formatting
// functionality for database columns. This class should be subclassed and then
// the method 'formatData()' overridden to provide different formatting.
//
// The default implementation here simply returns the original data unchanged...

package net.alanmaxwell.sql;

import java.text.DecimalFormat;

import net.mar.SLALIB;

public class SQLDataFormatterSexagecimal extends SQLDataFormatter
{
  DecimalFormat to2digits = new DecimalFormat("00");

  DecimalFormat to1dp = new DecimalFormat("00.0");
  DecimalFormat to2dp = new DecimalFormat("00.00");
  DecimalFormat to3dp = new DecimalFormat("00.000");

  public boolean useHours = true;
  public int decimalPlaces = 0;

  public SQLDataFormatterSexagecimal()
  {
    this(false, 0);
  };
  
  public SQLDataFormatterSexagecimal(boolean useHours)
  {
    this(useHours, 0);
  };
  
  public SQLDataFormatterSexagecimal(boolean useHours, int decimalPlaces)
  {
    this.useHours = useHours;
    this.decimalPlaces = decimalPlaces;
  };

  protected String formatSexagecimal(double dataValue)
  {
   /* StringBuffer resultString = new StringBuffer("");

    if (dataValue < 0)
    {
      resultString.append("-");
    }
    else
    {
      resultString.append(" ");
    }
    
    double hoursORdegrees = 0.0;
*/
    if (useHours)
    {
      if (dataValue >= 1485) {
          dataValue=1485;
      }
      if (dataValue <= -1485) {
          dataValue=-1485;
      }
      
      return SLALIB.ToSexagesimal(dataValue*Math.PI/180.0,decimalPlaces,true);
      
      //hoursORdegrees = Math.abs(dataValue) / 15.0; // Convert to hours
    }
    else
    {
        if (dataValue >= 99) {
            dataValue=99;
        }
        if (dataValue <= -99) {
            dataValue=-99;
        }
        return SLALIB.ToSexagesimal(dataValue*Math.PI/180.0,decimalPlaces,false);
      //hoursORdegrees = Math.abs(dataValue);
    }

    
   /*
    double minutes = (hoursORdegrees - ((int) hoursORdegrees)) * 60.0; // Work out minutes
    double seconds = (minutes - ((int) minutes)) * 60.0; // Work out seconds

    resultString.append(
      to2digits.format( (int) hoursORdegrees ) + ":" +
      to2digits.format( (int) minutes ) + ":"
    );

    switch (decimalPlaces)
    {
    case 3:
      resultString.append(
        to3dp.format( seconds )
      );
      break;
    case 2:
      resultString.append(
        to2dp.format( seconds )
      );
      break;
    case 1:
      resultString.append(
        to1dp.format( seconds )
      );
      break;
    default:
      resultString.append(
        to2digits.format( seconds )
      );
      break;
    };

    return resultString.toString();
    */
  };

  public String formatDataForHTML(String data)
  {
    if (data == null)
    {
      return "&lt;null&gt;";
    };

    if (data.length() < 1)
    {
      return "&nbsp;";
    };
    
    try
    {
    	double dataValue = Double.parseDouble(data);
      return formatSexagecimal(dataValue);
    }
    catch (NumberFormatException nfe)
    {
      return "&lt;NaN&gt;";
    }
  };

  public String formatDataForCSV(String data)
  {
    if (data == null)
    {
      return "<null>";
    };

    try
    {
    	double dataValue = Double.parseDouble(data);
      return formatSexagecimal(dataValue);
    }
    catch (NumberFormatException nfe)
    {
      return "<NaN>";
    }
  };
};