// Copyright 2024 SquareBlock Inc. All Rights Reserved.
// Author: tianlei.richard@qq.com (tianlei.richard)

#include "mesh.h"
#include "OBJ_Loader.h"
#include "spdlog/spdlog.h"
#include <filesystem>

#define OBJL_VEC2_TO_EIGEN_VECTOR(objl_v)                                      \
  Eigen::Vector2d { objl_v.X, objl_v.Y }
#define OBJL_VEC3_TO_EIGEN_VECTOR(objl_v)                                      \
  Eigen::Vector3d { objl_v.X, objl_v.Y, objl_v.Z }

Mesh::Mesh(const std::vector<Vertex> &vertices,
           const std::vector<std::array<size_t, 3>> &indices,
           const std::shared_ptr<PhoneMaterial> &phone_material)
    : vertices_(vertices), indices_(indices), material_(phone_material) {}

std::vector<Vertex> &Mesh::get_vertices() { return vertices_; }

std::vector<Vertex>::size_type Mesh::get_vertex_size() const {
  return vertices_.size();
};

const decltype(Vertex::texture_coordinate) &
Mesh::get_texture_coordinate(const unsigned int index) const {
  return (vertices_[index]).texture_coordinate;
}

Point3d Mesh::get_normal_vector(const unsigned int index) const {
  return (vertices_[index]).normal;
}

std::vector<Triangle> Mesh::get_primitives() const {
  std::vector<Triangle> primitives;
  for (const auto &idx : indices_) {
    primitives.push_back(Triangle(vertices_, idx[0], idx[1], idx[2]));
  }
  return primitives;
}

Triangle Mesh::get_primitive(const size_t index) const {
  assert(index >= 0 && index < indices_.size());
  return Triangle(vertices_, indices_[index][0], indices_[index][1],
                  indices_[index][2]);
}

const std::shared_ptr<PhoneMaterial> &Mesh::get_material() const {
  return material_;
}

std::vector<Mesh> Mesh::load_mesh(
    const std::string &obj_path,
    const std::unordered_map<Texture::TextureType, std::string> texture_path) {
  const auto &obj_file_path = std::filesystem::path(obj_path);
  const auto &material_directory = obj_file_path.parent_path();

  objl::Loader loader{};
  assert(loader.LoadFile(obj_path.c_str()));

  std::vector<Mesh> res;
  for (const auto &mesh : loader.LoadedMeshes) {
    spdlog::info("Current mesh has {} vertices.", mesh.Vertices.size());

    std::vector<Vertex> vertices;
    for (decltype(mesh.Vertices)::size_type i = 0; i < mesh.Vertices.size();
         i++) {
      vertices.push_back(Vertex(
          OBJL_VEC3_TO_EIGEN_VECTOR(mesh.Vertices[i].Position),
          OBJL_VEC3_TO_EIGEN_VECTOR(mesh.Vertices[i].Normal),
          OBJL_VEC2_TO_EIGEN_VECTOR(mesh.Vertices[i].TextureCoordinate)));
    }
    std::vector<std::array<size_t, 3>> indices;
    for (decltype(mesh.Indices)::size_type i = 0; i < mesh.Indices.size();
         i += 3) {
      indices.push_back(
          {mesh.Indices[i], mesh.Indices[i + 1], mesh.Indices[i + 2]});
    }

    std::unordered_map<Texture::TextureType, std::shared_ptr<Texture>> textures;
    if (!texture_path.empty()) {
      for (const auto &[type, path] : texture_path) {
        textures.insert({type, std::make_shared<Texture>(path)});
      }
    } else if (!mesh.MeshMaterial.map_Kd.empty()) {
      textures.insert({Texture::DiffuseMap,
                       std::make_shared<Texture>(material_directory /
                                                 mesh.MeshMaterial.map_Kd)});
    }
    auto material = std::make_shared<PhoneMaterial>(textures);

    res.push_back(Mesh(vertices, indices, material));
  }
  return res;
}
