/***************************************************************************
                          altitude.cpp  -  description
                             -------------------
    begin                : Sat Jul 20 2002
    copyright            : (C) 2002 by Andre Somers
    email                : andre@kflog.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "altitude.h"

Altitude::Altitude(){
  _isValid=false;
}

Altitude::Altitude(int meters):Distance(meters){
}

Altitude::Altitude(double meters):Distance(meters){
}

/** copy constructor */
Altitude::Altitude (const Altitude& alt): Distance (alt._dist){
}
Altitude::Altitude (const Distance& dst): Distance (dst){
}

Altitude::~Altitude(){
}

/** Get altitude as flightlevel (at standard pressure) */
double Altitude::getFL() const {
  return Distance::getFeet()/100;
}

void Altitude::setUnit(altitude unit) {
  _altitudeUnit=unit;
}

QString Altitude::getText(double meter, bool withUnit, int precision) {
  QString result, unit;
  double dist;
  int defprec=1;
  
  switch (_altitudeUnit) {
  case meters:
    unit="m";
    dist=meter;
    defprec=0;
    break;
  case feet:
    unit="ft";
    dist=meter/mFromFeet;
    defprec=0;
    break;
  case kilometers:
    unit="Km";
    dist=meter/mFromKm;
    defprec=2;
    break;
  case miles:
    unit="M.";
    dist=meter/mFromMile;
    defprec=3;
    break;
  case nautmiles:
    unit="nM.";
    dist=meter/mFromNMile;
    defprec=3;
    break;
  case flightlevel:
    unit="FL";
    dist=meter/(mFromFeet*100.0);
    defprec=3;
    break;
  default:
    unit="m";
    dist=meter;
    defprec=0;
  }

  if (precision<0) precision=defprec;

  QCString prec;
  prec.setNum(precision);

  if (dist<0) {
    if (withUnit) {
      result=unit;
    } else {
      result="";
    }
  } else {
    if (withUnit) {
       if( _altitudeUnit == flightlevel )
         result.sprintf("%s %1." + prec + "f" ,unit.latin1(),dist);
       else
         result.sprintf("%1." + prec + "f %s",dist,unit.latin1());
    } else {
      result.sprintf("%1." + prec + "f",dist);
    }
  }
  return result;
}

/** Converts a distance from the current units to meters. */
double Altitude::convertToMeters(double dist){
  double res;

  switch (_altitudeUnit) {
  case 0: //meters:
    res=dist;
    break;
  case 1: //feet:
    res=dist*mFromFeet;
    break;
  case 2: //kilometers:
    res=dist*mFromKm;
    break;
  case 3: //miles:
    res=dist*mFromMile;
    break;
  case 4: //nautmiles:
    res=dist*mFromNMile;
    break;
  case 5: //flightlevel:
    res=dist*(mFromFeet/100.0);
  default:
    res=dist;
  }

  return res;
}

QString Altitude::getText(bool withUnit, uint precision) const
{
  QString result, unit;
  double dist;

  switch (_altitudeUnit) {
  case meters:
    unit="m";
    dist=getMeters();
    break;
  case feet:
    unit="ft";
    dist=getFeet();
    break;
  case kilometers:
    unit="Km";
    dist=getKilometers();
    break;
  case miles:
    unit="M.";
    dist=getMiles();
    break;
  case nautmiles:
    unit="nM.";
    dist=getNautMiles();
    break;
  case flightlevel:
    unit="FL";
    dist=getFL();
    break;
  default:
    unit="m";
    dist=getMeters();
  }

  QCString prec;
  prec.setNum(precision);
  if (withUnit) {
    if( _altitudeUnit == flightlevel )
       result.sprintf("%s %1."+ prec + "f" ,unit.latin1(),dist);
    else
       result.sprintf("%1." + prec + "f %s",dist,unit.latin1());

  } else {
    result.sprintf("%1." + prec + "f",dist);
  }
  return result;
}

Altitude::altitude Altitude::_altitudeUnit=meters;

/** Get Altitude as Flightlevel */
//double Altitude::getFL(double pressure){

//}


