#include "tileimage.h"

tileimage::tileimage()
{
}

tileimage::~tileimage()
{
  del();
}

int tileimage::del()
{
  for( unsigned i = 0; i < mytiles.size(); i++ )
    delete [] mytiles[i];
  mytiles.clear();
  mytilessz.clear();
  trans.clear();
  return 0;
}

int tileimage::outputtrans( std::string &data )
{
  data.clear();
  data = "<image>\n<rows>";
  char buffer[512];
  for( unsigned i = 0; i < mytilessz.size(); i++ )
    {
      memset( buffer, 0, 512 );
      sprintf(buffer, "%i ", mytilessz[i]);
      data = data + buffer;
    }
  data = data + "</rows>\n";
  for( std::map< tilepair, std::vector<transformation>, tilepair >::iterator it = trans.begin();
       it != trans.end(); it++ )
    {

      data = data + "<alignment>\n";
      memset( buffer, 0, 512 );
      sprintf( buffer, "<tilea>%i %i</tilea>\n", it->first.t1x, it->first.t1y );
      data = data + buffer;
      memset( buffer, 0, 512 );
      sprintf( buffer, "<tileb>%i %i</tileb>\n", it->first.t2x, it->first.t2y );
      data = data + buffer;
      for( unsigned k = 0; k < it->second.size(); k++ )
        {
          data = data + "<transform";
          memset(buffer, 0, 512);
          int tx, ty;
          tx = (int)round( it->second[k].transx );
          ty = (int)round( it->second[k].transy );
          double vx, vy;
          vx = it->second[k].x_var;
          vy = it->second[k].y_var;
          sprintf( buffer, " x=\"%i\" y=\"%i\" varx=\"%e\" vary=\"%e\">%e</transform>\n",
                   tx, ty, vx, vy, it->second[k].score );
          data = data + buffer;
        }
      data = data + "</alignment>\n";
    }
  data = data + "</image>\n";
  return 0;
}

tile* tileimage::gettile( int x, int y )
{
  if( x >= (int)mytiles.size() )
    return NULL;
  if( y >= mytilessz[x] )
    return NULL;
  return &(mytiles[x][y]);
}

int tileimage::getsize( std::vector< int > &s)
{
  s = mytilessz;
  return 0;
}

int tileimage::init( dbimage *db )
{
  idb = db;

  std::vector< int > stackx, stacky;
  std::cout << "generating stacks\n";
  idb->rows( stackx );
  for( unsigned i = 0; i < stackx.size(); i++ )
    std::cout << stackx[i] << " ";
  std::cout << "\n";
  for( unsigned i = 0; i < stackx.size(); i++ )
    {
      idb->rowstacks( stackx[i], stacky );
      mytiles.push_back( new tile [stacky.size()] );
      mytilessz.push_back( (int)stacky.size() );
      tile *mt;
      mt = mytiles.back();
      for( unsigned j = 0; j < stacky.size(); j++ )
        {
          mt[j].init( stackx[i], stacky[j], idb );
          std::cout << stacky[j] << " ";
        }
      for( unsigned j = 0; j < stacky.size()-1; j++ )
        {
          std::vector< transformation > ts;
          ts.clear();
          mt[j].align( mt+(j+1), ts );
        }
      stacky.clear();
      std::cout << "\n";
    }
 
  return 0;
}

int tileimage::setalignments( int x1, int y1, int x2, int y2,
                              std::vector< transformation > &ts )
{
  tilepair tp;
  tp.t1x = x1;
  tp.t1y = y1;
  tp.t2x = x2;
  tp.t2y = y2;
  trans[tp] = ts;
  return 0;
}
