/* -------------------------------------------------------------------------
 * BKG NTRIP Client
 * -------------------------------------------------------------------------
 *
 * Class:      t_pppObsPool
 *
 * Purpose:    Buffer with observations
 *
 * Author:     L. Mervart
 *
 * Created:    29-Jul-2014
 *
 * Changes:
 *
 * -----------------------------------------------------------------------*/

#include "pppObsPool.h"

using namespace BNC_PPP;
using namespace std;

// Constructor
/////////////////////////////////////////////////////////////////////////////
t_pppObsPool::t_epoch::t_epoch(const bncTime& epoTime, vector<t_pppSatObs*>& obsVector,
                               bool pseudoObsIono, const QMap<char, t_pppRefSat*>& refSatMap) {
  _epoTime        = epoTime;
  _pseudoObsIono  = pseudoObsIono;
  for (unsigned ii = 0; ii < obsVector.size(); ii++) {
    _obsVector.push_back(obsVector[ii]);
  }
  obsVector.clear();

  QMapIterator<char, t_pppRefSat*> it(refSatMap);
  while (it.hasNext()) {
    it.next();
    char sys = it.key();
    t_pppRefSat* refSat = it.value();
    _refSatMap[sys] = new t_pppRefSat(refSat->prn(), refSat->stecValue());
  }
  //refSatMap.clear();
}

// Destructor
/////////////////////////////////////////////////////////////////////////////
t_pppObsPool::t_epoch::~t_epoch() {
  for (unsigned ii = 0; ii < _obsVector.size(); ii++) {
    delete _obsVector[ii];
  }

  QMapIterator<char, t_pppRefSat*> it(_refSatMap);
  while (it.hasNext()) {
    it.next();
    delete it.value();
  }
  _refSatMap.clear();
}

// Constructor
/////////////////////////////////////////////////////////////////////////////
t_pppObsPool::t_pppObsPool() {
  for (unsigned ii = 0; ii <= t_prn::MAXPRN; ii++) {
    _satCodeBiases[ii] = 0;
  }
  for (unsigned ii = 0; ii <= t_prn::MAXPRN; ii++) {
    _satPhaseBiases[ii] = 0;
  }
  _vTec = 0;
  _refSatChangeRequiredMap['G'] = false;
  _refSatChangeRequiredMap['R'] = false;
  _refSatChangeRequiredMap['E'] = false;
  _refSatChangeRequiredMap['C'] = false;

}

// Destructor
/////////////////////////////////////////////////////////////////////////////
t_pppObsPool::~t_pppObsPool() {
  for (unsigned ii = 0; ii <= t_prn::MAXPRN; ii++) {
    delete _satCodeBiases[ii];
  }
  for (unsigned ii = 0; ii <= t_prn::MAXPRN; ii++) {
    delete _satPhaseBiases[ii];
  }
  delete _vTec;
  while (_epochs.size() > 0) {
    delete _epochs.front();
    _epochs.pop_front();
  }
}

//
/////////////////////////////////////////////////////////////////////////////
void t_pppObsPool::putCodeBias(t_satCodeBias* satCodeBias) {
  int iPrn = satCodeBias->_prn.toInt();
  delete _satCodeBiases[iPrn];
  _satCodeBiases[iPrn] = satCodeBias;
}

//
/////////////////////////////////////////////////////////////////////////////
void t_pppObsPool::putPhaseBias(t_satPhaseBias* satPhaseBias) {
  int iPrn = satPhaseBias->_prn.toInt();
  delete _satPhaseBiases[iPrn];
  _satPhaseBiases[iPrn] = satPhaseBias;
}

//
/////////////////////////////////////////////////////////////////////////////
void t_pppObsPool::putTec(t_vTec* vTec) {
  delete _vTec;
  _vTec = new t_vTec(*vTec);
  delete vTec;
}

//
/////////////////////////////////////////////////////////////////////////////
void t_pppObsPool::putEpoch(const bncTime& epoTime, vector<t_pppSatObs*>& obsVector,
                            bool pseudoObsIono, const QMap<char, t_pppRefSat*>& refSatMap) {
  const unsigned MAXSIZE = 2;
  _epochs.push_back(new t_epoch(epoTime, obsVector, pseudoObsIono, refSatMap));

  if (_epochs.size() > MAXSIZE) {
    delete _epochs.front();
    _epochs.pop_front();
  }
}

//
/////////////////////////////////////////////////////////////////////////////
void t_pppObsPool::deleteLastEpoch() {

  if (!_epochs.empty()) {
    delete _epochs.back();
    _epochs.pop_back();
  }
}
