#include "tile.h"

tile::tile()
{
  idb = NULL;
  adb = NULL;
}

tile::~tile()
{
}

int tile::init( char xcoor, char ycoor, 
                dbimage *db )
{
  int x;
  x = tolower( xcoor ) - 'a';
  char cb[2];
  cb[1] = 0;
  cb[0] = ycoor;
  int y;
  y = strtol( cb, NULL, 10 ) -1;
  init( x, y, db );
  return 0;
}

int tile::init( int xcoor, int ycoor, 
                dbimage *db )
{
  idb = db;
  adb = NULL;
  tidx = xcoor;
  tidy = ycoor;
  top.clear();
  bottom.clear();
  left.mytile = NULL;
  right.mytile = NULL;
  return 0;
}

int tile::init( int a, dbalnimage *alndb )
{
  aid = a;
  adb = alndb;
  idb = NULL;
  top.clear();
  bottom.clear();
  left.mytile = NULL;
  right.mytile = NULL;
  return 0;
}

int tile::getimages( std::vector< std::pair<int, int> > &ids,
                     std::vector< image2d *> &ims )
{
  image2d *im;
  im = NULL;
  int ret;
  for( unsigned i = 0; i < ids.size(); i++ )
    {
      if( im == NULL )
        im = new image2d;
      ret = idb->getimage( tidx, tidy,
                           ids[i].first, ids[i].second, *im);
      if( ret == 0 )
        {
          ims.push_back( im );
          im = NULL;
        } 
      else
        ims.push_back( NULL );
    }
}

int tile::getimages( std::vector< int > &ids,
                     std::vector< image2d *> &ims )
{
  image2d *im;
  im = NULL;
  int ret;
  for( unsigned i = 0; i < ids.size(); i++ )
    {
      if( im == NULL )
        im = new image2d;
      ret = idb->getimage( tidx, tidy,
                           ids[i], 0, *im);
      if( ret == 0 )
        {
          ims.push_back( im );
          im = NULL;
        } 
      else
        ims.push_back( NULL );
    }
  
  for( unsigned i = 0; i < ids.size(); i++ )
    {
      if( im == NULL )
        im = new image2d;
      ret = idb->getimage( tidx, tidy,
                           ids[i], 1, *im);
      if( ret == 0 )
        {
          ims.push_back( im );
          im = NULL;
        } 
      else
        ims.push_back( NULL );
    }

  for( unsigned i = 0; i < ids.size(); i++ )
    {
      if( im == NULL )
        im = new image2d;
      ret = idb->getimage( tidx, tidy,
                           ids[i], 2, *im);
      if( ret == 0 )
        {
          ims.push_back( im );
          im = NULL;
        } 
      else
        ims.push_back( NULL );
    }
  
  return 0;
}

int tile::getids( std::vector< int > &slice,
                  std::vector< int > &channel )
{
  if( idb == NULL )
    {
      adb->getslices( aid, slice );
      adb->getchannels( aid, channel );
    }
  else
    {
      idb->getslices( slice );
      idb->getchannels( channel );
    }
  return 0;
}

int tile::getmap( image2d *im )
{
  if( adb != NULL )
    adb->get_aligned_mask( aid, im );
  else
    {
      std::cout << "setting all to 1\n";
      idb->getimage( tidx, tidy, 0, 0, *im );
      im->setall( 1 );
    }
  return 0;
}

int tile::getimage( int slice, int channel, image2d *im )
{
  int ret;
  if( idb == NULL )
    ret = adb->get_aligned_image( aid, slice, channel, im );
  else
    ret = idb->getimage( tidx, tidy, slice, channel, *im );
  return ret;
}

int tile::getsize( int &height, int &width )
{
  int ret;
  image2d im;
  if( idb == NULL )
    ret = adb->get_aligned_image( aid, 0, 0, &im );
  else
    ret = idb->getimage( tidx, tidy, 0, 0, im );
  if( ret != 0)
    {
      std::cout << "error get tile image\n";
    }
  height = im.getheight();
  width = im.getwidth();
  return 0;
}

int tile::getheight( )
{
  int ret;
  image2d im;
  if( idb == NULL )
    ret = adb->get_aligned_image( aid, 0, 0, &im );
  else
    ret = idb->getimage( tidx, tidy, 0, 0, im );
  if( ret != 0)
    {
      std::cout << "error get tile image\n";
    }
  ret = im.getheight();
  if( ret <= 0)
    {
      std::cout << "error get tile height\n";
    }
  return ret;
}

int tile::getwidth( )
{
  int ret;
  image2d im;
  if( idb == NULL )
    ret = adb->get_aligned_image( aid, 0, 0, &im );
  else
    ret = idb->getimage( tidx, tidy, 0, 0, im );
  if( ret != 0)
    {
      std::cout << "error get tile image\n";
    }
  ret = im.getwidth();
  if( ret <= 0)
    {
      std::cout << "error get tile width\n";
    }
  return ret;
}

int tile::align( tile *ot,
                 std::vector< transformation > &ts )
{
  ot = NULL;
  std::cout << "in tile::align ts " << ts.size() << "\n";
  return 0;
}


