/*
 * TestEventNotifier.cpp
 *
 *  Created on: Jun 19, 2018
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */


#include "catch.hpp"
#include "fakeit.hpp"

// testing purposes
#define private public

#include "utility/LoggerFactory.h"
#include "input/EventHandler.h"
#include "utility/IBlockingQueue.h"
#include "input/detail/EventNotifier.h"

#include "input/EventNotifier.h"


using namespace flippR_driver;
using namespace input;
using namespace fakeit;
using namespace utility;

SCENARIO("An EventNotifier gets created", "[construction]")
{
  LoggerFactory::CreateInputTestLogger();

  WHEN("The EventNotifier gets created")
  {
    Event event(0, 0, "test");

    Mock<IBlockingQueue<Event>> queue_mock;
    Fake(Method(queue_mock, push));
    When(Method(queue_mock, pop)).AlwaysDo([event](){return event;});
    Fake(Dtor(queue_mock));

    input::detail::EventNotifier notifier(&(queue_mock.get()));
    THEN("It sets the running variable to true")
    {
      REQUIRE(notifier.is_running);
    }
    THEN("It creates a notify thread")
    {
      REQUIRE(typeid(notifier.notify_thread).hash_code() == typeid(std::thread).hash_code());
    }
  }
}

SCENARIO("An EventHandler gets [un]registered at the notifier", "[un-register notifier]")
{
  GIVEN("An EventHandler")
  {
    LoggerFactory::CreateInputTestLogger();

    Event event(0, 0, "test");

    Mock<IBlockingQueue<Event>> queue_mock;
    Fake(Method(queue_mock, push));
    When(Method(queue_mock, pop)).AlwaysDo([event](){return event;});
    Fake(Dtor(queue_mock));

    Mock<input::EventHandler> event_handler_mock;
    Fake(Method(event_handler_mock, handle));
    Fake(Dtor(event_handler_mock));

    input::detail::EventNotifier notifier(&(queue_mock.get()));

    WHEN("The EventHandler gets registered at the eventNotifier")
    {
      notifier.register_event_handler(&event_handler_mock.get());

      THEN("The EventHandler gets saved")
      {
      }

      AND_WHEN("The EventHandler gets unregistered at the driver")
      {
        REQUIRE(!notifier.event_handlers.empty());

        notifier.unregister_event_handler(&event_handler_mock.get());

        THEN("The list of event_handlers is empty")
        {
          REQUIRE(notifier.event_handlers.empty());
        }
      }
    }


  }
}

SCENARIO("An event should be distributed", "[distribute]")
{
  GIVEN("An event")
  {
    Event event(0, 0, "test");

    Mock<IBlockingQueue<Event>> queue_mock;
    Fake(Method(queue_mock, push));
    When(Method(queue_mock, pop)).AlwaysDo([event](){return event;});
    Fake(Dtor(queue_mock));

    input::detail::EventNotifier notifier(&(queue_mock.get()));

    WHEN("The event comes in")
    {
      notifier.distribute_event(event);
      THEN("The event gets queued")
      {
        notifier.is_running = false;
//        REQUIRE((bool)Verify(Method(queue_mock, push)));
        REQUIRE((bool)Verify(Method(queue_mock, push).Using(event)));
      }
    }
  }
}

SCENARIO("The EventHandler gets notified")
{
  GIVEN("An Event and an EventHandler")
  {
    Event event(0, 0, "test");

    Mock<input::EventHandler> event_handler_mock;
    When(Method(event_handler_mock, handle)).AlwaysReturn();
    Fake(Dtor(event_handler_mock));

    Event test_event(0, 0, "test");

    Mock<IBlockingQueue<Event>> queue_mock;
    Fake(Method(queue_mock, push));
    When(Method(queue_mock, pop)).AlwaysDo([test_event](void){return test_event;});
    Fake(Dtor(queue_mock));

    input::detail::EventNotifier notifier(&(queue_mock.get()));

    notifier.event_handlers.insert(&(event_handler_mock.get()));

    WHEN("The event gets queued")
    {
      std::this_thread::sleep_for(std::chrono::milliseconds(50));
      THEN("The EventHandler gets called")
      {
        REQUIRE((bool) Verify(Method(event_handler_mock, handle).Using(test_event)));
      }
    }
  }
}
