// -*- LSST-C++ -*-
#include <iomanip>
#include <sstream>

#include "Detection.h"
#include "Exceptions.h"

namespace ctExcept = collapseTracklets::exceptions;

/*
 * jmyers 7/29/08
 * 
 */


Detection::Detection()
{
    hasETime = false;
    initialized = false;
}


long int Detection::getID() const 
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for ID from uninitialized Detection\n");
        return -1;
    }
    else {
        return ID;
    }

}

double Detection::getEpochMJD() const  
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for EpochMJD from uninitialized Detection\n");
        return -1;
    }
    else {
        return MJD;
    }
}


double Detection::getRA()  const 
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for RA from uninitialized Detection\n");
        return -1;
    }
    else {
        return RA;
    }
}


double Detection::getDec()  const 
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for Dec from uninitialized Detection\n");
        return -1;
    }
    else {
        return dec;
    }
}


std::string Detection::getObscode() const  
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for ObsCode from uninitialized Detection\n");
        return "";
    }
    else {
        return obscode;
    }

}

std::string Detection::getObjName() const  
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for ObjName from uninitialized Detection\n");
        return "";
    }
    else {
        return objName;
    }
}

double Detection::getMag()  const 
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for magnitude from uninitialized Detection\n");
        return -1000;
    }
    else {
        return mag;
    }
}


double Detection::getLength()  const 
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for length from uninitialized Detection\n");
        return -1000;
    }
    else {
        return length;
    }
}



double Detection::getAngle() const 
{
    if (!initialized) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for angle from uninitialized Detection\n");
        return -1000;
    }
    else {
        return angle;
    }
    
}


bool Detection::hasExposureTime() const {
    return hasETime;
}

double Detection::getExposureTime() const 
{
    if ((!initialized) || (!hasETime)) {
        throw LSST_EXCEPT(ctExcept::UninitializedException, 
                          "Detection: request for exposure from Detection which is uninitialized or has no etime data\n");
        return -1000;
    }
    else {
        return etime;
    }
    
}




void Detection::fromMITIString(std::string mitiString) {
    /*     --------- PANSTARRS Input File Format (from Larry Denneau's Spec):
           ID EPOCH_MJD RA_DEG DEC_DEG MAG OBSCODE OBJECT_NAME LENGTH ANGLE [ETIME]*/
    std::istringstream ss(mitiString);
    /* make SS raise an exception if reading doesn't happen correctly */
    ss.exceptions(std::ifstream::failbit | std::ifstream::badbit);    
    try {
        ss >> ID;
        ss >> MJD;
        ss >> RA;
        ss >> dec;
        ss >> mag;
        ss >> obscode;
        ss >> objName;
        ss >> length;
        ss >> angle;
    }
    catch (...) {
        throw LSST_EXCEPT(ctExcept::BadParameterException, 
                          "Badly-formatted MITI string\n");
    }
    try {
        ss >> etime;
        hasETime = true;
    }
    catch (...) {
        hasETime = false;
    }
    initialized = true;
}
