/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: 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.
 *
 *    MoleCuilder 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 MoleCuilder.  If not, see .
 */
/*
 * SetRandomNumbersDistribution.cpp
 *
 *  Created on: Jan 01, 2011
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Log.hpp"
#include "molecule.hpp"
#include "CodePatterns/Verbose.hpp"
#include "World.hpp"
#include "RandomNumbers/RandomNumberDistributionFactory.hpp"
#include "RandomNumbers/RandomNumberDistribution.hpp"
#include "RandomNumbers/RandomNumberDistribution_Parameters.hpp"
#include 
#include 
#include 
#include "SetRandomNumbersDistributionAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "SetRandomNumbersDistributionAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr CommandSetRandomNumbersDistributionAction::performCall() {
  // note down old type
  std::string oldtype = RandomNumberDistributionFactory::getInstance().getCurrentTypeName();
  LOG(1, "Current distribution is " << oldtype);
  // set the new default
  RandomNumberDistributionFactory::getInstance().setCurrentType(params.distribution_type.get());
  LOG(0, "STATUS: Distribution of random number generator is now: "
      << RandomNumberDistributionFactory::getInstance().getCurrentTypeName());
  // note down old parameters
  RandomNumberDistribution_Parameters *oldparameters =
      RandomNumberDistributionFactory::getInstance().getPrototype().getParameterSet();
  // set each parameter (that is not -1);
  {
    std::stringstream input(params.parameters.get());
    RandomNumberDistribution_Parameters *currentparameters =
        RandomNumberDistributionFactory::getInstance().getPrototype().getParameterSet();
    input >> *currentparameters;  // add new values on top
    if (!currentparameters->isDefault()) {
      LOG(1, "Changing prototype's parameters.");
      RandomNumberDistributionFactory::getInstance().manipulatePrototype(*currentparameters);
    }
    delete currentparameters;
  }
  {
    RandomNumberDistribution_Parameters *currentparameters =
        RandomNumberDistributionFactory::getInstance().getPrototype().getParameterSet();
    LOG(1, "Its parameters are: " << *currentparameters);
    delete  currentparameters;
  }
  std::stringstream output;
  output << *oldparameters;
  CommandSetRandomNumbersDistributionState *newstate =
      new CommandSetRandomNumbersDistributionState(oldtype,output.str(),params);
  delete oldparameters;
  return ActionState::ptr(newstate);
}
ActionState::ptr CommandSetRandomNumbersDistributionAction::performUndo(ActionState::ptr _state) {
  CommandSetRandomNumbersDistributionState *state = assert_cast(_state.get());
  // note down old type for redo
  std::string newtype = RandomNumberDistributionFactory::getInstance().getCurrentTypeName();
  LOG(1, "Newly set distribution was " << newtype);
  // note down old parameters for redo
  RandomNumberDistribution_Parameters *newparameters =
      RandomNumberDistributionFactory::getInstance().getPrototype().getParameterSet();
  // set the new default
  RandomNumberDistributionFactory::getInstance().setCurrentType(state->old_distribution_type);
  LOG(0, "STATUS: Distribution of random number generator is again: "
      << RandomNumberDistributionFactory::getInstance().getCurrentTypeName());
  // set each parameter back (that is not -1);
  {
    std::stringstream input(state->old_parameters);
    RandomNumberDistribution_Parameters *currentparameters =
        RandomNumberDistributionFactory::getInstance().getPrototype().getParameterSet();
    input >> *currentparameters;
    if (!currentparameters->isDefault()) {
      LOG(1, "Changing prototype's parameters.");
      RandomNumberDistributionFactory::getInstance().manipulatePrototype(*currentparameters);
    }
    delete currentparameters;
  }
  {
    RandomNumberDistribution_Parameters *currentparameters =
        RandomNumberDistributionFactory::getInstance().getPrototype().getParameterSet();
    LOG(1, "Its parameters are: " << *currentparameters);
    delete  currentparameters;
  }
  std::stringstream output;
  output << *newparameters;
  CommandSetRandomNumbersDistributionState *newstate =
      new CommandSetRandomNumbersDistributionState(newtype,output.str(),params);
  delete newparameters;
  return ActionState::ptr(newstate);
}
ActionState::ptr CommandSetRandomNumbersDistributionAction::performRedo(ActionState::ptr _state){
  return performUndo(_state);
}
bool CommandSetRandomNumbersDistributionAction::canUndo() {
  return true;
}
bool CommandSetRandomNumbersDistributionAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */