/*************************************************************************
 *    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 "ChemotaxisDictyPlugin.h"

#include "ChemotaxisDictyEnergy.h"


#include <CompuCell3D/Simulator.h>
#include <CompuCell3D/ClassRegistry.h>
#include <CompuCell3D/Potts3D/Potts3D.h>
#include <BasicUtils/BasicClassAccessor.h>
#include <CompuCell3D/plugins/SimpleClock/SimpleClockPlugin.h>

using namespace CompuCell3D;

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

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


#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;



void ChemotaxisDictyPlugin::init(Simulator *simulator) {
  sim = simulator;
  CompuCell3D::Potts3D *potts = simulator->getPotts();
  chemicalEnergy = new ChemotaxisDictyEnergy(potts,0,0);
  chemicalEnergy->setSimulatorPtr(simulator);
  potts->registerEnergyFunction(chemicalEnergy);

  SimpleClockPlugin * simpleClockPlugin=(SimpleClockPlugin*)(Simulator::pluginManager.get("SimpleClock"));
  chemicalEnergy->setSimpleClockAccessorPtr(simpleClockPlugin->getSimpleClockAccessorPtr());
  

}
///will initialize chemotactic field here - need to deffer this after all steppables which contain field had been pre-initialzied
void ChemotaxisDictyPlugin::extraInit(Simulator *simulator) {

   chemicalEnergy->initializeField();
   concentrationField = chemicalEnergy->getField();

}

void ChemotaxisDictyPlugin::field3DChange(const Point3D &pt, CellG *newCell, CellG *oldCell){
   if(!newCell){
      concentrationField->set(pt,0.0);/// in medium we assume concentration 0
   }
}


ChemotaxisDictyPlugin::ChemotaxisDictyPlugin() : chemicalEnergy(0) {
}

ChemotaxisDictyPlugin::~ChemotaxisDictyPlugin() {
  if (chemicalEnergy) delete chemicalEnergy;
}

void ChemotaxisDictyPlugin::readXML(XMLPullParser &in) {
  ASSERT_OR_THROW("ChemotaxisDicty plugin not initialized!", chemicalEnergy);
  chemicalEnergy->readXML(in);
}

void ChemotaxisDictyPlugin::writeXML(XMLSerializer &out) {
  ASSERT_OR_THROW("ChemotaxisDicty plugin not initialized!", chemicalEnergy);
  chemicalEnergy->writeXML(out);
}
