/*
 * 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 <cxxtest/TestSuite.h>

#include "topologyreadercsv.hh"

class TopologyReaderCSV_test: public CxxTest::TestSuite
{
private:
  std::string areas_file_name ;
  std::string waypoints_file_name ;

public:

  TopologyReaderCSV_test(void):
    areas_file_name("/tmp/TopologyReaderCSV_areas_file.csv"),
    waypoints_file_name("/tmp/TopologyReaderCSV_waypoints_file.csv")
  {
    std::ofstream areas_file(areas_file_name.c_str()) ;
    areas_file
        << "My building;My room #1;1;2;3;9;8;7\n"
        << "My building;Room #2;4;5;6;6;5;4\n"
        << "Building #2;My room #1;1;2;3;9;8;7\n"
        << "My building;Room #2;4;5;6;6;5;4\n"
        << "My building;Room #2;4;5;6;6;5;4\n" ;
    areas_file.close() ;

    std::ofstream waypoints_file(waypoints_file_name.c_str()) ;
    waypoints_file
        << "1;2;3;My building\n"
        << "4;5;6;My building\n"
        << "1;2;3;Building #2\n"
        << "4;5;6;My building\n"
        << "9;8;7;Building #2;My building\n"
        << "4;5;6;My building\n" ;
    waypoints_file.close() ;

    TS_ASSERT_THROWS_NOTHING(
      TopologyReaderCSV toporeader(areas_file_name,
                                   waypoints_file_name)) ;
  }


  ~TopologyReaderCSV_test(void)
  {
    TestUtil::remove_file(areas_file_name) ;
    TestUtil::remove_file(waypoints_file_name) ;
  }


  static TopologyReaderCSV_test* createSuite(void)
  {
    return new TopologyReaderCSV_test() ;
  }


  static void destroySuite(TopologyReaderCSV_test *suite)
  {
    delete suite ;
  }


  void test_areas(void)
  {
    Building *building1 = nullptr ;
    TS_ASSERT_THROWS_NOTHING(
      building1 = const_cast<Building*>(
                    &Stock::get_building("My building"))) ;
    Building *building2 = nullptr ;
    TS_ASSERT_THROWS_NOTHING(
      building2 = const_cast<Building*>(
                    &Stock::get_building("Building #2"))) ;

    TS_ASSERT_EQUALS(building1->get_areas().size(), 2u) ;

    const Area *area_ptr ;
    area_ptr = building1->get_areas().find("My room #1")->second ;
    Area area1(building1, "My room #1",
               Point3D(1,2,3), Point3D(9,8,7)) ;
    assert(area_ptr) ;
    TS_ASSERT_EQUALS(*area_ptr, area1) ;

    area_ptr = building1->get_areas().find("Room #2")->second ;
    assert(area_ptr) ;
    Area area2(building1, "Room #2",
               Point3D(4,5,6), Point3D(6,5,4)) ;
    TS_ASSERT_EQUALS(*area_ptr, area2) ;

    TS_ASSERT_EQUALS(building2->get_areas().size(), 1u) ;
    area1.set_building(building2) ;
    area_ptr = building2->get_areas().find("My room #1")->second ;
    assert(area_ptr) ;
    TS_ASSERT_EQUALS(*area_ptr, area1) ;
  }


  void test_waypoints(void)
  {
    Building *building1 = nullptr ;
    TS_ASSERT_THROWS_NOTHING(
      building1 = const_cast<Building*>(
                    &Stock::get_building("My building"))) ;
    Building *building2 = nullptr ;
    TS_ASSERT_THROWS_NOTHING(
      building2 = const_cast<Building*>(
                    &Stock::get_building("Building #2"))) ;

    Waypoint *w, *waypoint_ptr ;

    TS_ASSERT_EQUALS(building1->get_waypoints().size(), 3u) ;

    const Waypoint &waypoint1 =
      Stock::find_create_waypoint(Waypoint(building1, 1,2,3)) ;
    w = const_cast<Waypoint*>(&waypoint1) ;

    TS_ASSERT(building1->get_waypoints().find(w) !=
              building1->get_waypoints().end()) ;
    waypoint_ptr = *building1->get_waypoints().find(
                     const_cast<Waypoint*>(&waypoint1)) ;
    TS_ASSERT_EQUALS(waypoint_ptr, w) ;

    const Waypoint &waypoint2 =
      Stock::find_create_waypoint(Waypoint(building1, 4,5,6)) ;
    w = const_cast<Waypoint*>(&waypoint2) ;

    TS_ASSERT(building1->get_waypoints().find(w) !=
              building1->get_waypoints().end()) ;
    waypoint_ptr = *building1->get_waypoints().find(
                     const_cast<Waypoint*>(&waypoint2)) ;
    TS_ASSERT_EQUALS(waypoint_ptr, w) ;


    TS_ASSERT_EQUALS(building2->get_waypoints().size(), 2u) ;

    const Waypoint &waypoint3 =
      Stock::find_create_waypoint(Waypoint(building2, 1,2,3)) ;
    w = const_cast<Waypoint*>(&waypoint3) ;

    TS_ASSERT(building2->get_waypoints().find(w) !=
              building2->get_waypoints().end()) ;
    waypoint_ptr = *building2->get_waypoints().find(
                     const_cast<Waypoint*>(&waypoint3)) ;
    TS_ASSERT_EQUALS(waypoint_ptr, w) ;
  }

} ;
