#ifndef __TILEIMAGE_H__
#define __TILEIMAGE_H__
#include "dbimage.h"
#include "tile.h"
#include "transformation.h"
#include <string>
#ifdef _BIOCLUSTER_
#include <map>
#endif

#define MAXTILESZ 64

/// class of an image comprised of tiles
class tileimage
{
 public:
  struct tnodelink
  {
    int t1x, t1y; ///< tilenode 1 coors
    int t2x, t2y; ///< tilenode 2 coors
    int transx, transy; ///< transformation to apply on tilenode 2
    void *t1;  ///< pointer to tile node t1
    void *t2;  ///< pointer to tile node t2
    double score;
  };
  struct tilenode
  {
    int x, y;
    tile *mytile;
    int color;
    int visited;
    std::vector< tnodelink > connections;
  };

  struct tilepair
  {
    int t1x, t1y;
    int t2x, t2y;
    /// function for map less than compair
    bool operator()( const tilepair &a, const tilepair &b ) const
    {
      unsigned long long int ia, ib;
      ia = a.t2y + a.t2x*MAXTILESZ + a.t1y*MAXTILESZ*MAXTILESZ
        + a.t1x*MAXTILESZ*MAXTILESZ*MAXTILESZ;
      ib = b.t2y + b.t2x*MAXTILESZ + b.t1y*MAXTILESZ*MAXTILESZ
        + b.t1x*MAXTILESZ*MAXTILESZ*MAXTILESZ;
      return ia < ib;
    }
  };
  tileimage();
  ~tileimage();
  int del();

  int init( dbimage *idb );

  tile* gettile( int x, int y );

  int getsize( std::vector< int > &s);

  int setalignments( int x1, int y1, int x2, int y2,
                     std::vector< transformation > &ts );

  int outputtrans( std::string &data );

  int gettileid( int x, int y )
  {
    return x*32+y;
  }
  int gettilcoor( int id, int &x, int &y )
  {
    x = id/32;
    y = id%32;
    return 0;
  }

 private:
  dbimage *idb;

  /// vector of tile rows
  std::vector< tile* > mytiles;
  std::vector< int > mytilessz;

  /// transstuff
  std::map< tilepair, std::vector<transformation>, tilepair > trans;

  std::vector< tilenode* > tilegraph;
};

#endif
