// SquareGrid8.hpp
//
//     Created on: 31. 01. 2018
//         Author: Jan Uhlik
//    Modified by:
//
// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
// Git repository: https://gitlab.fit.cvut.cz/algorithms-library-toolkit/automata-library

#ifndef ALIB2_SQUAREGRID8_HPP
#define ALIB2_SQUAREGRID8_HPP

#include <alib/pair>
#include <alib/tuple>

#include "SquareGrid.hpp"

namespace grid {

template<typename TCoordinate, typename TEdge>
class SquareGrid8 final : public SquareGrid<TCoordinate, TEdge> {
// ---------------------------------------------------------------------------------------------------------------------

 public:
  using coordinate_type = TCoordinate;
  using edge_type = TEdge;
  using node_type = ext::pair<TCoordinate, TCoordinate>;
  using direction_type = SquareGridDirections;

// =====================================================================================================================
// Constructor, Destructor, Operators
 public:
  explicit SquareGrid8(TCoordinate height, TCoordinate width);

// =====================================================================================================================
// GridBase interface
 public:
  int compare(const GridBase &other) const override;

  virtual int compare(const SquareGrid8 &other) const;

// =====================================================================================================================
// GridInterface interface

 public:

  bool isValidDirection(direction_type direction) const override;

// ---------------------------------------------------------------------------------------------------------------------

 protected:
  TEdge createEdge(const node_type &a, const node_type &b) const override;

// ---------------------------------------------------------------------------------------------------------------------
// =====================================================================================================================
// GraphInterface interface

  std::string name() const override;

// ---------------------------------------------------------------------------------------------------------------------

};

// =====================================================================================================================

template<typename TCoordinate, typename TEdge>
SquareGrid8<TCoordinate, TEdge>::SquareGrid8(TCoordinate height, TCoordinate width)
    : SquareGrid<TCoordinate, TEdge>(height, width) {
}

// ---------------------------------------------------------------------------------------------------------------------

template<typename TCoordinate, typename TEdge>
TEdge SquareGrid8<TCoordinate, TEdge>::createEdge(const SquareGrid8::node_type &a,
                                                  const SquareGrid8::node_type &b) const {
  return TEdge(a, b);
}

// ---------------------------------------------------------------------------------------------------------------------

template<typename TCoordinate, typename TEdge>
bool SquareGrid8<TCoordinate, TEdge>::isValidDirection(SquareGrid8::direction_type direction) const {
  return SQUARE_GRID_DIRECTIONS.find(direction) != SQUARE_GRID_DIRECTIONS.end();
}

// ---------------------------------------------------------------------------------------------------------------------

template<typename TCoordinate, typename TEdge>
std::string SquareGrid8<TCoordinate, TEdge>::name() const {
  return "SquareGrid8";
}

// ---------------------------------------------------------------------------------------------------------------------

template<typename TCoordinate, typename TEdge>
int SquareGrid8<TCoordinate, TEdge>::compare(const GridBase &other) const {
  if (ext::type_index(typeid(*this)) == ext::type_index(typeid(other))) return this->compare((decltype(*this)) other);
  return ext::type_index(typeid(*this)) - ext::type_index(typeid(other));
}

template<typename TCoordinate, typename TEdge>
int SquareGrid8<TCoordinate, TEdge>::compare(const SquareGrid8 &other) const {
  auto first = ext::tie(this->m_obstacles, this->m_height, this->m_width);
  TCoordinate height = other.getHeight();
  TCoordinate width = other.getWidth();
  auto second = ext::tie(other.getObstacleList(), height, width);

  static ext::compare<decltype(first)> comp;

  return comp(first, second);
}

// ---------------------------------------------------------------------------------------------------------------------

} // namespace grid

// =====================================================================================================================

namespace core {

/**
 * Helper for normalisation of types specified by templates used as internal datatypes of symbols and states.
 *
 * \returns new instance of the graph with default template parameters or unmodified instance if the template parameters were already the default ones
 */

template<typename TCoordinate, typename TEdge>
struct normalize < grid::SquareGrid8 < TCoordinate, TEdge > > {
  static grid::SquareGrid8<> eval(grid::SquareGrid8<TCoordinate, TEdge> &&value) {
    grid::SquareGrid8<> grid(value.getHeight(), value.getWidth());

    // Copy obstacles
    for (auto &&i: ext::make_mover(std::move(value).getObstacleList())) {
      DefaultSquareGridNodeType
          obstacle = common::Normalize::normalizeObstacle(std::move(i));

      grid.addObstacle(std::move(obstacle));
    }

    return grid;
  }
};

} // namespace grid

// =====================================================================================================================
#endif //ALIB2_SQUAREGRID8_HPP
