/*
 * 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.
 */


#ifndef _OWLPS_POSITIONING_REQUEST_HH_
#define _OWLPS_POSITIONING_REQUEST_HH_

#include "mobile.hh"
#include "measurement.hh"
#include "timestamp.hh"
#include "posutil.hh"

#include <owlps.h>

#include <ctime>
#include <vector>
#include <unordered_map>
#include <ostream>

/// Represents a request sent by a Mobile
class Request
{
protected:
  /// Type of the request
  uint_fast8_t type ;
  /// Number of packets sent by the mobile for this request
  pkt_id_t nb_packets ;
  /// The mobile that sent the request
  const Mobile *mobile ;
  /// Local date of the request on the mobile
  Timestamp time_sent ;
  /// Date at which we received the request from the aggregator
  Timestamp time_received ;
  /// List of Measurement of the request
  /** Note that this is not a pointer list, values are actually stored.
      The `string` parameter is the MAC address of the receiver CP. */
  std::unordered_map<std::string, Measurement> measurements ;
  /// Real coordinates of the request (normally unavailable for a
  /// standard positioning request)
  Point3D *real_position ;

  /** @name Write accessors */
  //@{
  void clear_real_position(void) ;
  //@}

  /// Measurements' list type
  /**
   * This really is just an alias to work-around clang++ that doesn't
   * handle the following syntax:
   *
   *     Request(..., const std::unordered_map<std::string, Measurement>
   *       &_measurements = std::unordered_map<std::string,
   *                                           Measurement>()) ;
   */
  using measurements_list = std::unordered_map<std::string, Measurement> ;

public:
  Request(const Mobile *const _mobile = nullptr,
          const Timestamp &_time_sent = Timestamp(),
          const std::unordered_map<std::string, Measurement>
          &_measurements = measurements_list()) ;

  explicit Request(const std::unordered_map<std::string, Measurement>
                   &_measurements);

  Request(const Timestamp &_time_sent,
          const std::unordered_map<std::string, Measurement>
          &_measurements = measurements_list()) ;

  Request(const Request &source) ;

  virtual ~Request(void) ;

  /** @name Read accessors */
  //@{
  uint_fast8_t get_type(void) const ;
  uint_fast16_t get_nb_packets(void) const ;
  const Mobile* get_mobile(void) const ;
  const Timestamp& get_time_sent(void) const ;
  const Timestamp& get_time_received(void) const ;
  /// Returns all the measurements
  const std::unordered_map<std::string, Measurement>&
  get_measurements(void) const ;
  /// Returns the measurement made by the CP `mac_receiver,` if any
  const Measurement*
  get_measurement(const std::string &mac_receiver) const ;
  const Point3D* get_real_position(void) const ;
  //@}

  /** @name Write accessors */
  //@{
  void set_type(const uint_fast8_t _type) ;
  void set_nb_packets(const uint_fast16_t _nb_packets) ;
  void set_mobile(const Mobile *const _mobile) ;
  void set_time_sent(const Timestamp &_time_sent) ;
  void received_now(void) ;
  void set_measurements(const std::unordered_map
                        <std::string, Measurement> &_measurements) ;
  void set_real_position(const Point3D &_real_position) ;
/// Reinitialises all attributes
  void clear(void) ;
  //@}

  /** @name Conversion accessors */
  //@{
  /// Converts to a CSV string
  const std::string to_csv(void) const ;
  //@}

  /** @name Operations */
  //@{
  /// Computes the similarity of two Request
  float similarity(const Request &source) const ;
  //@}

  /** @name Operators */
  //@{
  Request& operator=(const Request &source) ;
  bool operator==(const Request &comp) const ;
  bool operator!=(const Request &comp) const ;
  operator bool(void) const ;
  //@}

  /// Displays a Request
  friend std::ostream& operator<<(std::ostream &os, const Request &r) ;
} ;



/* *** Read accessors *** */



inline uint_fast8_t Request::get_type() const
{
  return type ;
}


inline uint_fast16_t Request::get_nb_packets() const
{
  return nb_packets ;
}


inline const Mobile* Request::get_mobile() const
{
  return mobile ;
}


inline const Timestamp& Request::get_time_sent() const
{
  return time_sent ;
}


inline const Timestamp& Request::get_time_received() const
{
  return time_received ;
}


inline const std::unordered_map<std::string, Measurement>&
Request::get_measurements(void) const
{
  return measurements ;
}


inline const Point3D* Request::get_real_position(void) const
{
  return real_position ;
}



/* *** Write accessors *** */


inline void Request::set_type(const uint_fast8_t _type)
{
  type = _type ;
}


inline void Request::set_nb_packets(const uint_fast16_t _nb_packets)
{
  nb_packets = _nb_packets ;
}


inline void Request::set_mobile(const Mobile *const _mobile)
{
  mobile = _mobile ;
}


inline void Request::set_time_sent(const Timestamp &_time_sent)
{
  time_sent = _time_sent ;
}


inline void Request::received_now()
{
  time_received.now() ;
}


inline void Request::
set_measurements(const std::unordered_map<std::string, Measurement>
                 &_measurements)
{
  measurements = _measurements ;
}



/* *** Operators *** */


inline bool Request::operator!=(const Request &comp) const
{
  return !(*this == comp) ;
}


/**
 * @returns `false` if the Request is empty.
 * @returns `true` if at least one attribute (other than #type and
 * #nb_packets) is initialised.
 */
inline Request::operator bool() const
{
  return
    mobile ||
    time_sent ||
    ! measurements.empty() ;
}



namespace std
{
template<> struct hash<Request>
{
public:
  /**
   * The Mobile MAC address and the Timestamp shourd be sufficient to
   * identify uniquely a Request, but we test everything.
   */
  size_t operator()(const Request &source) const
  {
    size_t seed = 0 ;

    PosUtil::hash_combine(seed, source.get_type()) ;
    PosUtil::hash_combine(seed, source.get_nb_packets()) ;
    PosUtil::hash_combine(seed, source.get_time_sent()) ;
    PosUtil::hash_combine(seed, source.get_time_received()) ;
    if (source.get_mobile())
      PosUtil::hash_combine(seed, source.get_mobile()->get_mac_addr()) ;
    if (source.get_real_position())
      PosUtil::hash_combine(seed, source.get_real_position()) ;

    return seed ;
  }
} ;
}



#endif // _OWLPS_POSITIONING_REQUEST_HH_
