


#include "kubecontrol/KubePod.hpp"
#include "nlohmann/json_fwd.hpp"
#include <math.h>

#include <string>
#include <thread>
#include <vector>
#include <future>

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

#include <kubecontrol/PodController.hpp>
#include <crossguid/guid.hpp>

const int podGrid = 2;

void startShip(kubecontrol::PodController* podc,std::string uuid,std::string Name, std::string lat, std::string lon)
{

std::string owner = "controller";
std::string type = "ship";
std::string image = "ship:latest";
    kubecontrol::KubePod ShipPod1(owner,uuid,type,image,"simulator");

    nlohmann::json vars;
    vars["ENTITY_ID"] = uuid;
    vars["ENTITY_NAME"] = Name;
    vars["ENTITY_SIDE"] = "Neutral";
    vars["POSITION"]["LAT"] = lat;
    vars["POSITION"]["LON"] = lon;
    vars["POSITION"]["Height"] = "0";
    vars["COURSE"] = "0";
    vars["SPEED"] = "0";
    vars["GROUNDTRUTH_PORT"] = std::to_string(10000);
    vars["GROUNDTRUTH_ADDR"] = "239.0.0.1";
    vars["COMMAND_PORT"] = "5555";
    vars["ENTITY_RCS"] = std::to_string(850);

    vars["ENTITY_SENSORS"].push_back("radar:latest");
    ShipPod1.setEnvironmentVar("CONFIG", vars.dump());

    podc->startPod(ShipPod1);

}
void createScenario(kubecontrol::PodController* podc,std::vector<std::string> *uuidList)
{
    // GeographicLib::Geodesic geod(GeographicLib::Constants::WGS84_a(), GeographicLib::Constants::WGS84_f());
      double lat = 54, lon = 1;
      int counter  = 0;
      double distance = 10000;

      int rasterSize = podGrid;

      for (int i = 0; i < rasterSize; i++ )
      {
        double lonTmp = lon;
        for (int a = 0; a < rasterSize; a++)
        {

        std::string name = "test";
        name += std::to_string(counter);
        double lat2 = 0;
        double lon2 = 0;
        // geod.Direct(lat, lonTmp, 90, distance, lat2, lon2);
        lat2= lat+10;
        // SimCore::Identifier id;
        // ids->push_back(id.getUUID());
        std::string uuid = xg::newGuid().str();
        uuidList->push_back(uuid);
        // startShip( podc,uuid,name, std::to_string(lat2), std::to_string(lon2));
        auto retrurn = std::async(std::launch::async, &startShip,podc,uuid,name, std::to_string(lat2), std::to_string(lon2));

        // SimCore::Position pos;
        // pos.setGeodesicPos(lat2, lon2, 0);
        // auto track =  std::make_shared<SimCore::SimTrack>(id,name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL);
        // track->setPosition(pos);
        // list->addTrack(track);
        // SimControl::startNewShip(name, std::to_string(lat2), std::to_string(lon2), "0", "0", "0");
        lonTmp = lon2;
        counter ++;
        }
        double lat2, lon2;

        // geod.Direct(lat, lon, 0, distance, lat2, lon2);

        lat = lat + 10;

      }

}



SCENARIO("Testing massiv pod handling")
{


// kubecontrol::PodController  podc("docs/config");
auto podc = new kubecontrol::PodController("docs/config");
std::vector<std::string> uuidList;

createScenario(podc, &uuidList);

LOG_S(INFO)<<"Amount Parent Pods: " << uuidList.size();
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
      // podc.stopPod(std::string Label)

// for (auto i: uuidList)
// {
//   auto retrurn = std::async(std::launch::async, &kubecontrol::PodController::stopPod,podc,i);
// }

// std::this_thread::sleep_for(std::chrono::milliseconds(20000));
// Create a random number generator
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(0, uuidList.size()-1);

    // Generate a random index
    int random_index = distribution(generator);


  GIVEN("different Attributes for a Track in different forms")
  {
    WHEN("constructing Track Object with data")
    {

      THEN("check if Track attributes are correct")
      {
        auto info = podc->getInfoForPod(uuidList[random_index]);
        REQUIRE(info.Component == "ship");
        REQUIRE(info.UUID == uuidList[random_index]);
        REQUIRE(info.IP != "");
        REQUIRE(info.Owner == "controller");
        REQUIRE(podc->getInfoForAllPods().size() == podGrid*podGrid);

        // REQUIRE(info1 != "");
        podc->stopAllPods();





      } //THEN
    } // WHEN
  } // GIVEN
} //SCENARIO
