#ifndef AUGMENTED_POINT_CONFIGURATION_HH
#define AUGMENTED_POINT_CONFIGURATION_HH

#include "CommandlineOptions.hh"
#include "Chirotope.hh"
#include "Symmetry.hh"
#include "GKZ.hh"
#include "SymmetryBlackBox.hh"
#include "PointConfiguration.hh"
#include "Flip.hh"
      
template<typename checkFine, typename smallGroup, typename APCSpec>
class AugmentedPointConfiguration_enhanced;

template<typename checkFine, typename smallGroup>
class AugmentedPointConfigurationNoSym;
      
template<typename checkFine, typename smallGroup>
class AugmentedPointConfigurationSym;

template<typename checkFine, typename smallGroup, typename APCSpec>
class AugmentedPointConfiguration_core {

   private:
      const PointConfiguration&           points;
      const Chirotope&                    chiro;
      const SymmetryGroup&                symmetries;
      const gkz_type                      gkz;

      friend class ::AugmentedPointConfigurationNoSym<checkFine, smallGroup>;
      friend class ::AugmentedPointConfigurationSym<checkFine, smallGroup>;


   public:
      typedef smallGroup smallGroupBool;
      typedef checkFine checkFineBool;
      typedef std::true_type trivialSymmetry;
      typedef pmFlipHash<gkz_vector_type, Flip> pmFlipContainer;

      AugmentedPointConfiguration_core(
            const PointConfiguration& points_in,
            const Chirotope&          chiro_in,
            const SymmetryGroup&      symmetries_in) :
         points(points_in), chiro(chiro_in),
         symmetries(symmetries_in),
         gkz(points_in) 
         {}
      
      const PointConfiguration& get_points() const{
         return points;
      }

      const Chirotope& get_chiro() const{
         return chiro;
      }

      const SymmetryGroup& get_symmetries() const{
         return symmetries;
      }

      const gkz_type& get_gkz() const{
         return gkz;
      }
      
      std::string get_triangulation_data(const TriangNode& triang) const {
         std::ostringstream bos;
         TriangNode canonical(static_cast<const APCSpec*>(this)->canonicalize(triang));
         bos << canonical;
         bos << " gkz: ";
         bos << gkz.gkz_as_string(canonical);
         return bos.str();
      }

      int size() const {
         return gkz.size();
      }

      void init_check(const TriangNode& t) const {
         gkz.init_check<checkFine>(t);
      }

};

template<typename checkFine, typename smallGroup>
class AugmentedPointConfigurationNoSym : public AugmentedPointConfiguration_core<checkFine, smallGroup, AugmentedPointConfigurationNoSym<checkFine, smallGroup>> {

   public:
      AugmentedPointConfigurationNoSym(
            const PointConfiguration& points_in,
            const Chirotope&          chiro_in,
            const SymmetryGroup&      symmetries_in) :
         AugmentedPointConfiguration_core<checkFine, smallGroup, AugmentedPointConfigurationNoSym>(points_in, chiro_in, symmetries_in)
         {}
      
      bool check_triang_equality(const TriangNode& t1, const TriangNode& t2) const{
         return t1==t2;
      }
      
      int is_lex_upflip(const Flip& flip, gkz_vector_type& result_gkz) const{
         int result;
         if(checkFine::value){
            result_gkz = this->gkz.evaluate_fine(flip);
         } else {
            result_gkz = this->gkz.evaluate(flip);
         }
         result = this->gkz.is_lex_positive(result_gkz);
         return result;
      }
      
      int evaluate_flip(const Flip& flip, const TriangNode& source, const TriangNode& target, gkz_vector_type& result_gkz) const{
         return is_lex_upflip(flip, result_gkz);
      }

      void printInitialTriang(CerrWrapper<false>& ost, const TriangNode& triang) const {
         ost << "Initial triang: " << triang << std::endl;
      }

      TriangNode canonicalize(const TriangNode& triang) const {
         return triang;
      }

};


template<typename checkFine, typename smallGroup>
class AugmentedPointConfigurationSym: public AugmentedPointConfiguration_core<checkFine, smallGroup, AugmentedPointConfigurationSym<checkFine, smallGroup>>{

   private:
      const SymmetryBlackBox<smallGroup>  sbb;


   public:
      typedef smallGroup smallGroupBool;
      typedef checkFine checkFineBool;
      typedef std::false_type trivialSymmetry;

      AugmentedPointConfigurationSym(
            const PointConfiguration& points_in,
            const Chirotope&          chiro_in,
            const SymmetryGroup&      symmetries_in) :
         AugmentedPointConfiguration_core<checkFine, smallGroup, AugmentedPointConfigurationSym>(points_in, chiro_in, symmetries_in),
         sbb(CommandlineOptions::orbit_cache_size(), this->symmetries, this->gkz)
         {}
      
      bool check_triang_equality(const TriangNode& t1, const TriangNode& t2) const{
         return sbb.are_equivalent_triangulations(t1, t2);
      }

      const SymmetryBlackBox<smallGroup>& get_sbb() const{
         return sbb;
      }

      int evaluate_flip(const Flip& flip, const TriangNode& source, const TriangNode& target, gkz_vector_type& result_gkz) const{
         const triang_vector_type optimalVector = this->gkz.triangulationAsVector(target);
         int result;
         result_gkz = sbb.get_gkz_of_canonical(target) - sbb.get_gkz_of_canonical(source);
         if(checkFine::value){
            Scalar fineCoord = this->gkz.fine_coordinate(result_gkz);
            result_gkz = fineCoord | result_gkz;
         }
         result = this->gkz.is_lex_positive(result_gkz);
         result_gkz = result_gkz | gkz_vector_type(optimalVector);
         return result;
      }
      
      void printInitialTriang(CerrWrapper<false>& ost, const TriangNode& triang) const {
         ost << "Initial triang: " << triang << std::endl;
         ost << "Orbit size: " << sbb.get_orbit_size(triang) << std::endl;
      }
      
      TriangNode canonicalize(const TriangNode& triang) const {
         return sbb.get_canonical_representative(triang);
      }

};

#endif
