#include "CommandlineOptions.hh"
#include "PointConfiguration.hh"
#include "Symmetry.hh"
#include "Chirotope.hh"
#include "Incidences.hh"
#include "PlacingTriang.hh"
#include "ConfData.hh"
#include "RegularityCheck.hh"
#include "Message.hh"

int main(const int argc, const char** argv) {

  using namespace topcom;
  
  CommandlineOptions::init(argc, argv);
  
  MessageStreams::forced() << std::endl;
  MessageStreams::forced() << "------------------------------------------------------------------\n";
  MessageStreams::forced() << " checking regularity of triangulations \n";
  MessageStreams::forced() << " TOPCOM client: " << CommandlineOptions::client() << '\n';
  MessageStreams::forced() << "------------------------------------------------------------------\n";
  MessageStreams::forced() << std::endl;

  Options options;

  // this client only makes sense with a seed:
  options.remove_options(Options::NO_SEED);
  ConfData confdata(options);
  CommentGrinder commentgrinder;
  confdata.read_input(std::cin);
  RegularityCheck::init();
  if (!confdata.has_seed()) {
    MessageStreams::forced() << "no triangulation found - check done." << std::endl;
    return 0;
  }
  SimplicialComplex triang = confdata.seed();
  size_type count(0);
  size_type count_nonregs(0);
  confdata.make_inctable();
  do {
    ++count;
    RegularityCheck regcheck(confdata, triang);
    if (!regcheck.is_regular()) {
      ++count_nonregs;
      if (CommandlineOptions::debug()) {
	MessageStreams::result() << "T[" << count << "] := " << triang << " is non-regular." << std::endl;
      }
      else {
	MessageStreams::result() << count << ": " << triang << " -> non-regular." << std::endl;
      }
    }
    else {
      if (CommandlineOptions::debug()) {
	MessageStreams::result() << "T[" << count << "] := " << triang << " is regular." << std::endl;
      }
      else {
	MessageStreams::result() << count << ": " << triang << " -> regular." << std::endl;
      }
      if (CommandlineOptions::output_heights()) {
	if (CommandlineOptions::debug()) {
	  MessageStreams::result() << "h[" << count
				   << "] := " << regcheck.heights() << ";\n";
	}
	else {
	  MessageStreams::result() << count << ": " << regcheck.heights() << ";\n";
	}
      }
    }
    MessageStreams::verbose() << "checked " << count << " triangulations, " 
			      << count_nonregs << " non-regular so far." << std::endl;
    std::cin >> commentgrinder;
  } while (triang.read(std::cin));
  MessageStreams::verbose() << "... no more triangulations found." << std::endl;
  RegularityCheck::term();
  return 0;
}


// eof checkregularity.cc
