//
// Created by rhetenor on 3/6/19.
//

#ifndef FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
#define FLIPPR_CODE_OUTPUTREQUESTHANDLER_H

#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/URI.h>
#include <Poco/JSON/JSON.h>
#include <memory>
#include <boost/optional.hpp>
#include <Poco/Logger.h>
#include <Poco/JSON/Object.h>

#include "output/OutputDriver.h"

namespace flippR_driver
{
namespace networking
{

class OutputRequestHandler : public Poco::Net::HTTPRequestHandler
{
public:
    OutputRequestHandler(std::shared_ptr<output::OutputDriver> output_driver);

    void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) override;

private:
  boost::optional<Poco::JSON::Object> parseRequest(const std::string& item_type, const std::string& item_name, const std::string& action, const std::string& score = 0);

  boost::optional<Poco::JSON::Object> parseSolenoid(const std::string& item_name, const std::string& action);
  boost::optional<Poco::JSON::Object> parseLamp(const std::string& item_name, const std::string& action);
  boost::optional<Poco::JSON::Object> parseSound(const std::string& item_name, const std::string& action);
  boost::optional<Poco::JSON::Object> parseDisplay(const std::string& item_name, const std::string& action, const std::string& score);
  boost::optional<Poco::JSON::Object> parseFlipper(const std::string& item_name, const std::string& action);

  std::vector<std::string> getPathSegments(Poco::URI uri);

private:
    std::shared_ptr<output::OutputDriver> output_driver;

};

template<typename T>
Poco::JSON::Array getItemArray(const std::vector<std::shared_ptr<T>> & items);

}
}


#endif //FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
