//============================================================================
//
// This file is part of the Shapes Generative Model Library (SGML) project.
//
// Copyright (c) 2010-2014 - Evangelos Kalogerakis, Siddhartha Chaudhuri (authors of the code) / Stanford University, UMass-Amherst
//
// SGML 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 3 of the License, or
// (at your option) any later version.

// SGML 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 SGML.  If not, see <http://www.gnu.org/licenses/>.
//
//============================================================================

#include "S12_Plugin.hpp"
#include "S12_NetworkFactory.hpp"

namespace SNL {
namespace S12 {

static S12_Plugin * plugin = NULL;

/** DLL start routine. Installs plugin. */
extern "C" SNL_S12_API SNL::Plugin *
dllStartPlugin()
{
  plugin = new S12_Plugin();
  SNL::PluginManager::install(plugin);
  return plugin;
}

/** DLL stop routine. Uninstalls plugin. */
extern "C" SNL_S12_API void
dllStopPlugin()
{
  SNL::PluginManager::uninstall(plugin);
  delete plugin;
}

static std::string const S12_PLUGIN_NAME   =  "S12 Network";
static std::string const S12_NETWORK_NAME  =  "S12";

S12_Plugin::S12_Plugin()
: SNL::Plugin(S12_PLUGIN_NAME), factory(NULL), started(false)
{}

S12_Plugin::~S12_Plugin()
{
  uninstall();
}

bool
S12_Plugin::install()
{
  return true;
}

bool
S12_Plugin::startup()
{
  if (!started)
  {
    if (!factory)
      factory = new S12_NetworkFactory;

    SNL::NetworkManager::installFactory(S12_NETWORK_NAME, factory);
    started = true;
  }

  return true;
}

bool
S12_Plugin::shutdown()
{
  if (started)
  {
    factory->destroyAllNetworks();
    SNL::NetworkManager::uninstallFactory(S12_NETWORK_NAME);
    started = false;
  }

  return true;
}

bool
S12_Plugin::uninstall()
{
  if (shutdown())  // not currently dependent on presence of other plugins
  {
    delete factory;
    factory = NULL;

    return true;
  }
  else
    return false;
}

} // namespace S12
} // namespace SNL
