/*************************************************************************
 *    CompuCell - A software framework for multimodel simulations of     *
 * biocomplexity problems Copyright (C) 2003 University of Notre Dame,   *
 *                             Indiana                                   *
 *                                                                       *
 * This program is free software; IF YOU AGREE TO CITE USE OF CompuCell  *
 *  IN ALL RELATED RESEARCH PUBLICATIONS according to the terms of the   *
 *  CompuCell GNU General Public License RIDER you can redistribute it   *
 * and/or modify it under the terms of the GNU General Public License as *
 *  published by the Free Software Foundation; either version 2 of the   *
 *         License, or (at your option) any later version.               *
 *                                                                       *
 * This program is distributed in the hope that it will be useful, but   *
 *      WITHOUT ANY WARRANTY; without even the implied warranty of       *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
 *             General Public License for more details.                  *
 *                                                                       *
 *  You should have received a copy of the GNU General Public License    *
 *     along with this program; if not, write to the Free Software       *
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.        *
 *************************************************************************/

#include "AmoebaeFieldInitializer.h"
#include <CompuCell3D/Potts3D/CellInventory.h>

#include <CompuCell3D/Simulator.h>
#include <CompuCell3D/Potts3D/Cell.h>
#include <CompuCell3D/Potts3D/Potts3D.h>
#include <CompuCell3D/Field3D/Point3D.h>
#include <CompuCell3D/Field3D/Dim3D.h>
//#include <CompuCell3D/Field3D/Field3D.h>
using namespace CompuCell3D;

#include <XMLCereal/XMLPullParser.h>
#include <XMLCereal/XMLSerializer.h>

#include <BasicUtils/BasicString.h>
#include <BasicUtils/BasicException.h>



#include <string>

#include <math.h>

#include <iostream>
using namespace std;


AmoebaeFieldInitializer::AmoebaeFieldInitializer() :
  potts(0) {}



void AmoebaeFieldInitializer::init(Simulator *simulator) {
 
  potts = simulator->getPotts();
 
}


void AmoebaeFieldInitializer::initializeCellTypes(){

   
   
//    cellInventoryPtr=& potts->getCellInventory();
//    
//    CellInventory::cellInventoryIterator cInvItr;
//    ///loop over all the cells in the inventory
//    CellG * cell;
// 
//    for(cInvItr=cellInventoryPtr->cellInventoryBegin() ; cInvItr !=cellInventoryPtr->cellInventoryEnd() ;++cInvItr ){
// 
//       cell=*cInvItr;
//       cell->type=1;
//       
// 
//    }

}


void AmoebaeFieldInitializer::createCellField(){

  Field3D<CellG *> *cellFieldG = potts->getCellFieldG();
 
   unsigned int token = 0;
   double distance;
   Point3D pt=position;
   Point3D n;


   
   CellG *cell = potts->createCellG(pt);
   
   CellG * nCell=0;

   cell->type=1;
   while (true) {
      n = cellFieldG->getNeighbor(pt, token, distance, false);
      
      if (distance > radius) break;
      
      cellFieldG->set(n, cell);
         
         
   }

   pt=Point3D(40,40,40);
   cell = potts->createCellG(pt);
   cell->type=2;
   distance =0;
   token =0;
   while (true) {
      n = cellFieldG->getNeighbor(pt, token, distance, false);
      
      if (distance > radius) break;
      
      cellFieldG->set(n, cell);
         
         
   }
   
   
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AmoebaeFieldInitializer::start() {

   cerr<<"INITIALIZE AMOEBAE CELL FIELD"<<endl;
   //BlobFieldInitializer::start();
   createCellField();
   initializeCellTypes();
      
}






void AmoebaeFieldInitializer::readXML(XMLPullParser &in) {
  in.skip(TEXT);

  while (in.check(START_ELEMENT)) {
    if (in.getName() == "Radius") {
      radius = BasicString::parseUInteger(in.matchSimple());

    }
    else if (in.getName() == "Position") {
 
      position.readXML(in);      
      in.matchSimple();

    }
    else {
      throw BasicException(string("Unexpected element '") + in.getName() + 
			   "'!", in.getLocation());
    }

    in.skip(TEXT);
  }
}

void AmoebaeFieldInitializer::writeXML(XMLSerializer &out) {
}
