#pragma once

#include <graphene/geometry/Point_set.h>
#include <graphene/surface_mesh/algorithms/templatefitting/Landmarks_manager.h>


namespace graphene {
namespace geometry {

//
// Flips all normals based on two vertices (e.g belly and back). If back->belly points
// into the opposite direction of normal(belly), all normals are flipped
//
static bool flip_normals_based_on_selection(Point_set& point_set,
                                            const std::string& fn_selection)
{
    std::vector<unsigned int> indices;
    graphene::surface_mesh::Landmarks_manager lmm;
    if (!lmm.read(fn_selection, indices))
        return false;

    if (indices.size() != 2)
    {
        printf("[ERROR] flip_normals: Expected 2 vertices to validate normal direction, got %lu\n",
               indices.size());
        return false;
    }

    auto p0 = point_set.points_[indices[0]];
    auto p1 = point_set.points_[indices[1]];

    auto n0 = point_set.normals_[indices[0]];

    bool flip_normals = dot(p0 - p1, n0) < 0;

    if (!flip_normals)
        return true;

    for (size_t point_index = 0; point_index < point_set.points_.size(); ++point_index)
    {
        point_set.normals_[point_index] *= -1;
    }

    return true;
}

}
}
