//
//  EagleInterfaceGrpcServer.hpp
//  shardok_engine
//
//  Created by Dan Crosby on 1/28/19.
//  Copyright © 2019 none. All rights reserved.
//

#ifndef EagleInterfaceGrpcServer_hpp
#define EagleInterfaceGrpcServer_hpp

#include <string>
#include <thread>

#include "ShardokGamesManager.hpp"
#include "src/main/cpp/net/eagle0/common/ProtobufWarningSuppression.hpp"
#pragma GCC diagnostic push
SUPPRESS_PROTOBUF_WARNINGS
#include <grpc/grpc.h>
#include <grpcpp/impl/codegen/server_context.h>
#include <grpcpp/security/server_credentials.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>

#include "src/main/protobuf/net/eagle0/common/shardok_internal_interface.grpc.pb.h"
#pragma GCC diagnostic pop

namespace shardok {
using grpc::ServerContext;
using grpc::Status;
using net::eagle0::common::GameSetupInfo;
using net::eagle0::common::GameStatusRequest;
using net::eagle0::common::GameStatusResponse;
using net::eagle0::common::HexMapNamesRequest;
using net::eagle0::common::HexMapNamesResponse;
using net::eagle0::common::HexMapRequest;
using net::eagle0::common::HexMapResponse;
using net::eagle0::common::NewGameRequest;
using net::eagle0::common::PlacementCommandsRequest;
using net::eagle0::common::PostCommandRequest;
using net::eagle0::common::ShardokInternalInterface;
using std::shared_ptr;

class EagleInterfaceImpl final : public ShardokInternalInterface::Service {
private:
    std::shared_ptr<ShardokGamesManager> gamesManager;

    auto ControllerForGame(const GameId& gameId, const GameSetupInfo& setupInfo)
            -> std::shared_ptr<ShardokGameController>;
    void StartGame(const NewGameRequest& newGameRequest);

    void PopulateGameStatusResponse(
            shared_ptr<ShardokGameController>& controller,
            int64_t startingActionId,
            GameStatusResponse* response);

public:
    explicit EagleInterfaceImpl(std::shared_ptr<ShardokGamesManager> manager);

    auto PostCommand(
            ServerContext* context,
            const PostCommandRequest* request,
            GameStatusResponse* response) -> Status override;
    auto PostPlacementCommands(
            ServerContext* context,
            const PlacementCommandsRequest* request,
            GameStatusResponse* response) -> Status override;
    auto GetGameStatus(
            ServerContext* context,
            const GameStatusRequest* request,
            GameStatusResponse* response) -> Status override;

    auto GetHexMap(ServerContext* context, const HexMapRequest* request, HexMapResponse* response)
            -> Status override;
    auto GetHexMapNames(
            ServerContext* context,
            const HexMapNamesRequest* request,
            HexMapNamesResponse* response) -> Status override;
};
}  // namespace shardok

#endif /* EagleInterfaceGrpcServer_hpp */
