/*
 * Copyright 2018 The Cartographer Authors
 *
 * 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.
 */

#include "cartographer_grpc/testing/test_helpers.h"

namespace cartographer_grpc {
namespace testing {

template <>
DataPredicateType BuildDataPredicateEquals<proto::AddImuDataRequest>(
    const proto::AddImuDataRequest &proto) {
  return [proto](const cartographer::sensor::Data &data) {
    const auto *dispatchable =
        dynamic_cast<const cartographer::sensor::Dispatchable<
            cartographer::sensor::ImuData> *>(&data);
    CHECK_NOTNULL(dispatchable);
    return google::protobuf::util::MessageDifferencer::Equals(
               cartographer::sensor::ToProto(dispatchable->data()),
               proto.imu_data()) &&
           dispatchable->GetSensorId() == proto.sensor_metadata().sensor_id();
  };
}

template <>
DataPredicateType BuildDataPredicateEquals<proto::AddFixedFramePoseDataRequest>(
    const proto::AddFixedFramePoseDataRequest &proto) {
  return [proto](const cartographer::sensor::Data &data) {
    const auto *dispatchable =
        dynamic_cast<const cartographer::sensor::Dispatchable<
            cartographer::sensor::FixedFramePoseData> *>(&data);
    CHECK_NOTNULL(dispatchable);
    return google::protobuf::util::MessageDifferencer::Equals(
               cartographer::sensor::ToProto(dispatchable->data()),
               proto.fixed_frame_pose_data()) &&
           dispatchable->GetSensorId() == proto.sensor_metadata().sensor_id();
  };
}

template <>
DataPredicateType BuildDataPredicateEquals<proto::AddOdometryDataRequest>(
    const proto::AddOdometryDataRequest &proto) {
  return [proto](const cartographer::sensor::Data &data) {
    const auto *dispatchable =
        dynamic_cast<const cartographer::sensor::Dispatchable<
            cartographer::sensor::OdometryData> *>(&data);
    CHECK_NOTNULL(dispatchable);
    return google::protobuf::util::MessageDifferencer::Equals(
               cartographer::sensor::ToProto(dispatchable->data()),
               proto.odometry_data()) &&
           dispatchable->GetSensorId() == proto.sensor_metadata().sensor_id();
  };
}

template <>
DataPredicateType BuildDataPredicateEquals<proto::AddLandmarkDataRequest>(
    const proto::AddLandmarkDataRequest &proto) {
  return [proto](const cartographer::sensor::Data &data) {
    const auto *dispatchable =
        dynamic_cast<const cartographer::sensor::Dispatchable<
            cartographer::sensor::LandmarkData> *>(&data);
    CHECK_NOTNULL(dispatchable);
    return google::protobuf::util::MessageDifferencer::Equals(
               cartographer::sensor::ToProto(dispatchable->data()),
               proto.landmark_data()) &&
           dispatchable->GetSensorId() == proto.sensor_metadata().sensor_id();
  };
}

template <>
DataPredicateType BuildDataPredicateEquals<proto::AddRangefinderDataRequest>(
    const proto::AddRangefinderDataRequest &proto) {
  return [proto](const cartographer::sensor::Data &data) {
    const auto *dispatchable =
        dynamic_cast<const cartographer::sensor::Dispatchable<
            cartographer::sensor::TimedPointCloudData> *>(&data);
    CHECK_NOTNULL(dispatchable);
    return google::protobuf::util::MessageDifferencer::Equals(
               cartographer::sensor::ToProto(dispatchable->data()),
               proto.timed_point_cloud_data()) &&
           dispatchable->GetSensorId() == proto.sensor_metadata().sensor_id();
  };
}

ProtoPredicateType BuildProtoPredicateEquals(
    const google::protobuf::Message *proto) {
  return [proto](const google::protobuf::Message &message) {
    return google::protobuf::util::MessageDifferencer::Equals(*proto, message);
  };
}

}  // namespace testing
}  // namespace cartographer_grpc
