// Copyright 2016 The Draco 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 "compression/mesh/mesh_encoder.h"

#include "compression/encode.h"
#include "core/draco_test_base.h"
#include "core/draco_test_utils.h"
#include "io/obj_decoder.h"

namespace draco {

class MeshEncoderTest : public ::testing::TestWithParam<const char *> {
 protected:
  MeshEncoderTest() {}

  // Fills out_method with id of the encoding method used for the test.
  // Returns false if the encoding method is not set properly.
  bool GetMethod(MeshEncoderMethod *out_method) const {
    if (strcmp(GetParam(), "sequential") == 0) {
      *out_method = MESH_SEQUENTIAL_ENCODING;
      return true;
    }
    if (strcmp(GetParam(), "edgebreaker") == 0) {
      *out_method = MESH_EDGEBREAKER_ENCODING;
      return true;
    }
    return false;
  }
  std::unique_ptr<Mesh> DecodeObj(const std::string &file_name) const {
    const std::string path = GetTestFileFullPath(file_name);
    std::unique_ptr<Mesh> mesh(new Mesh());
    ObjDecoder decoder;
    if (!decoder.DecodeFromFile(path, mesh.get()))
      return nullptr;
    return mesh;
  }
};

TEST_P(MeshEncoderTest, EncodeGoldenMesh) {
  // This test verifies that a given set of meshes are encoded to an expected
  // output. This is useful for catching bugs in code changes that are not
  // supposed to change the encoding.
  // The test is expected to fail when the encoding is modified. In such case,
  // the golden files need to be updated to reflect the changes.
  MeshEncoderMethod method;
  ASSERT_TRUE(GetMethod(&method))
      << "Test is run for an unknown encoding method";

  const std::string file_name = "test_nm.obj";
  std::string golden_file_name = file_name;
  golden_file_name += '.';
  golden_file_name += GetParam();
  golden_file_name += ".out";
  const std::unique_ptr<Mesh> mesh(DecodeObj(file_name));
  ASSERT_NE(mesh, nullptr) << "Failed to load test model " << file_name;

  EncoderOptions options = CreateDefaultEncoderOptions();
  EncoderBuffer buffer;
  ASSERT_TRUE(EncodeMeshToBuffer(*mesh.get(), options, &buffer))
      << "Failed encoding test mesh " << file_name << " with method "
      << GetParam();

  if (!FLAGS_update_golden_files) {
    EXPECT_TRUE(
        CompareGoldenFile(golden_file_name, buffer.data(), buffer.size()))
        << "Encoded data is different from the golden file. Please verify that "
           "the"
           " encoding works as expected and update the golden file if necessary"
           " (run the test with --update_golden_files flag).";
  } else {
    // Save the files into the local folder.
    EXPECT_TRUE(
        GenerateGoldenFile(golden_file_name, buffer.data(), buffer.size()))
        << "Failed to generate new golden file for " << file_name;
  }
}

INSTANTIATE_TEST_CASE_P(MeshEncoderTests, MeshEncoderTest,
                        ::testing::Values("sequential", "edgebreaker"));

}  // namespace draco
