/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 */


#include "inputmedium.hh"
#include "calibrationrequest.hh"
#include "stock.hh"
#include "configuration.hh"
#include "posexcept.hh"

using namespace std ;



/* *** Constructors *** */


InputMedium::InputMedium():
  current_line_nb(0)
{
  current_request = new Request() ;
}


InputMedium::~InputMedium()
{
  delete current_request ;
}



/* *** Operations *** */


/**
 * Reads a Request, increments current_line_nb, updates #current_request
 * and returns it.
 *
 * The input medium should be ready to be read before
 * processing requests; otherwise, #current_request is
 * [cleared](@ref Request::clear) and a blank Request is
 * returned.
 *
 * @returns The Request read.
 * @returns An empty Request in case of error or if the end of the
 * file is reached (note that when casted to bool, an empty Request
 * is `false`, see Request::operator bool()).
 */
const Request& InputMedium::get_next_request()
{
  clear_current_request() ;

  if (! fill_current_request())
    {
      clear_current_request() ;
      return *current_request ;
    }

  current_request->received_now() ;
  return *current_request ;
}


void InputMedium::clear_current_request()
{
  if (! dynamic_cast<CalibrationRequest*>(current_request))
    current_request->clear() ;
  else
    {
      delete current_request ;
      current_request = nullptr ;
      current_request = new Request() ;
    }
}


/**
 * The `position` argument can be modified by this function.
 */
void InputMedium::
fill_calibration_request_data(const string &mac_mobile,
                              Point3D &position,
                              const Direction &direction,
                              const uint8_t type)
{
  // We set the real coordinates (if found) for non-calibration requests
  if (type != OWL_REQUEST_CALIBRATION &&
      type != OWL_REQUEST_AUTOCALIBRATION)
    {
      if (position)
        current_request->set_real_position(position) ;
      return ;
    }

  if (position)
    {
      // Update the CP's coordinates if allowed (and if the mobile is
      // a CP, of course)
      if (Configuration::
          bool_value("positioning.update-cp-coordinates-online"))
        {
          if (type == OWL_REQUEST_AUTOCALIBRATION &&
              Configuration::bool_value("positioning.accept-new-cps"))
            {
              CapturePoint &transmitter =
                const_cast<CapturePoint&>(
                  Stock::find_create_cp(mac_mobile)) ;
              transmitter.set_coordinates(position) ;
            }
          else
            {
              try
                {
                  CapturePoint &transmitter =
                    const_cast<CapturePoint&>(Stock::get_cp(mac_mobile)) ;
                  transmitter.set_coordinates(position) ;
                }
              catch (element_not_found &e)
                {
                  // The mobile is not a CP or the CP does not exist
                }
            }
        }
    }

  else if (type == OWL_REQUEST_AUTOCALIBRATION)
    {
      // If an autocalibration request does not contain the coordinates
      // of the CP, we use the current coordinates of the CP as
      // ReferencePoint.
      try
        {
          CapturePoint &transmitter =
            const_cast<CapturePoint&>(Stock::get_cp(mac_mobile)) ;
          position = transmitter.get_coordinates() ;
        }
      catch (element_not_found &e)
        {
          // The mobile is not a CP or the CP does not exist
        }
    }

  const ReferencePoint &reference_point =
    Stock::find_create_reference_point(position) ;
  current_request_to_calibration_request(
    &reference_point, direction, type) ;
}


void InputMedium::current_request_to_calibration_request(
  const ReferencePoint *const reference_point,
  const Direction &direction,
  const uint_fast8_t request_type)
{
  CalibrationRequest *calibration_request =
    dynamic_cast<CalibrationRequest*>(current_request) ;
  if (calibration_request)
    {
      calibration_request->set_reference_point(
        const_cast<ReferencePoint*>(reference_point)) ;
      calibration_request->set_direction(direction) ;
      calibration_request->set_type(request_type) ;
      return ;
    }

  Request *tmp = current_request ;
  current_request = nullptr ;
  current_request =
    new CalibrationRequest(*tmp,
                           const_cast<ReferencePoint*>(reference_point),
                           direction, request_type) ;
  delete tmp ;
}
