// +-------------------------------- DISCLAIMER ---------------------------------+
// |                                                                             |
// | This application program is provided to you free of charge as an example.   |
// | Despite the considerable efforts of Euresys personnel to create a usable    |
// | example, you should not assume that this program is error-free or suitable  |
// | for any purpose whatsoever.                                                 |
// |                                                                             |
// | EURESYS does not give any representation, warranty or undertaking that this |
// | program is free of any defect or error or suitable for any purpose. EURESYS |
// | shall not be liable, in contract, in torts or otherwise, for any damages,   |
// | loss, costs, expenses or other claims for compensation, including those     |
// | asserted by third parties, arising out of or in connection with the use of  |
// | this program.                                                               |
// +-----------------------------------------------------------------------------+
#include <iostream>
#include "ProcessingHandler.h"

#include <Easy.h>
using namespace Euresys::Open_eVision;

#include "CameraHandler.h"

int main(int argc, char* argv[])
{

  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " DEEP_LEARNING_TOOL CAMERA_PARAMS" << std::endl;
    std::cout << "   DEEP_LEARNING_TOOL: path to a model exported from Deep Learning Studio (.edltool file)" << std::endl;
    std::cout << "   CAMERA_PARAMS: For the FakeCamera, path towards the image or video file." << std::endl;

    return 0;
  }

  std::string deepLearningModel;
  std::string cameraParams;

  if (argc >= 2)
    deepLearningModel = argv[1];
  if (argc >= 3)
    cameraParams = argv[2];

  int error = 0;

  {
    CameraHandler cameraHandler(cameraParams);
    ProcessingHandler processingHandler(deepLearningModel, &cameraHandler);  // defined after cameraHandler because
    // destructor must be called before cameraHandler

    processingHandler.start();
    cameraHandler.launchCamera();
    if (cameraHandler.abort_ || processingHandler.abort_)
    {
      std::cerr << "some error occured, closing application" << std::endl;
      error = -1;
    }
    else
    {
      std::this_thread::sleep_for(std::chrono::milliseconds(100));  // waits for 100ms, assuming a problem will have happened by then
      if (!processingHandler.abort_ && !cameraHandler.abort_)
      {
        std::cout << "Press anything to Quit\n" << std::endl;

        char c;
        std::cin >> c;
      }
      else
      {
        std::cerr << "An error occured, exciting" << std::endl;
      }
    }
  }  // calls processingHandler and cameraHandler destructors

  return error;
}
