/**
 * Copyright 2011-2013,2017 Centreon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 */

#include <absl/strings/match.h>

#include "com/centreon/broker/config/applier/state.hh"
#include "com/centreon/broker/io/factory.hh"
#include "com/centreon/exceptions/msg_fmt.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::io;

/**
 *  @brief Create a new stream.
 *
 *  This function is used to generate new streams after successful
 *  stream construction like for a feature negotiation.
 *
 *  @param[in] to          Stream on which the stream will work.
 *  @param[in] is_acceptor true if stream must be an accepting stream.
 *  @param[in] proto_name  Protocol name.
 *
 *  @return New stream.
 */
std::shared_ptr<stream> factory::new_stream(
    std::shared_ptr<stream> to,
    bool is_acceptor,
    const std::unordered_map<std::string, std::string>&) {
  (void)to;
  (void)is_acceptor;
  throw msg_fmt(
      "This is probably a bug, this protocol does not support feature "
      "negotiation");
  return nullptr;
}

bool factory::direct_grpc_serialized(const config::endpoint& cfg) {
  if (cfg.type == "bbdo_client" || cfg.type == "bbdo_server") {
    auto it = cfg.params.find("transport_protocol");
    if (it != cfg.params.end()) {
      return absl::EqualsIgnoreCase(it->second, "grpc") &&
             config::applier::state::instance()
                     .get_bbdo_version()
                     .total_version >=
                 bbdo::bbdo_version(3, 1, 0).total_version;
    }
  }
  return false;
}
