/****************************************************************************
 *   This file is part of the aGrUM/pyAgrum library.                        *
 *                                                                          *
 *   Copyright (c) 2005-2026 by                                             *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *                                                                          *
 *   The aGrUM/pyAgrum library is free software; you can redistribute it    *
 *   and/or modify it under the terms of either :                           *
 *                                                                          *
 *    - the GNU Lesser General Public License as published by               *
 *      the Free Software Foundation, either version 3 of the License,      *
 *      or (at your option) any later version,                              *
 *    - the MIT license (MIT),                                              *
 *    - or both in dual license, as here.                                   *
 *                                                                          *
 *   (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html)    *
 *                                                                          *
 *   This aGrUM/pyAgrum library is distributed in the hope that it will be  *
 *   useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,          *
 *   INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
 *   FOR A PARTICULAR PURPOSE  AND NONINFRINGEMENT. IN NO EVENT SHALL THE   *
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,        *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  *
 *   OTHER DEALINGS IN THE SOFTWARE.                                        *
 *                                                                          *
 *   See LICENCES for more details.                                         *
 *                                                                          *
 *   SPDX-FileCopyrightText: Copyright 2005-2026                            *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *   SPDX-License-Identifier: LGPL-3.0-or-later OR MIT                      *
 *                                                                          *
 *   Contact  : info_at_agrum_dot_org                                       *
 *   homepage : http://agrum.gitlab.io                                      *
 *   gitlab   : https://gitlab.com/agrumery/agrum                           *
 *                                                                          *
 ****************************************************************************/

#include <chrono>
#include <format>

#include <agrum/base/graphicalModels/graphicalModel.h>

#ifdef GUM_NO_INLINE
#  include <agrum/base/graphicalModels/graphicalModel_inl.h>
#endif /* GUM_NO_INLINE */

namespace gum {
  GraphicalModel::GraphicalModel() { GUM_CONSTRUCTOR(GraphicalModel) }

  GraphicalModel::GraphicalModel(const GraphicalModel& from) {
    GUM_CONS_CPY(GraphicalModel)
    _propertiesMap_ = from._propertiesMap_;
  }

  GraphicalModel::GraphicalModel(GraphicalModel&& from) noexcept :
      _propertiesMap_(std::move(from._propertiesMap_)) {
    GUM_CONS_MOV(GraphicalModel);
  }

  GraphicalModel::~GraphicalModel() { GUM_DESTRUCTOR(GraphicalModel); }

  GraphicalModel& GraphicalModel::operator=(const GraphicalModel& source) {
    if (this != &source) {
      _propertiesMap_ = source._propertiesMap_;
      GUM_OP_CPY(GraphicalModel);
    }
    return *this;
  }

  GraphicalModel& GraphicalModel::operator=(GraphicalModel&& source) noexcept {
    if (this != &source) {
      _propertiesMap_ = std::move(source._propertiesMap_);
      GUM_OP_MOV(GraphicalModel);
    }
    return *this;
  }

  void GraphicalModel::updateMetaData() {
    auto const time = std::chrono::time_point_cast< std::chrono::milliseconds >(
        std::chrono::system_clock::now());
    auto const currentdate = std::format("{:%Y-%m-%d %T}", time);

    const std::string currentSoftware = "aGrUM " GUM_VERSION;
    if (auto software = _propertiesMap_.tryGet("software")) {
      *software = currentSoftware;
    } else {
      _propertiesMap_.insert("software", currentSoftware);
    }

    if (!_propertiesMap_.tryGet("creation")) { _propertiesMap_.insert("creation", currentdate); }

    if (auto lastModification = _propertiesMap_.tryGet("lastModification")) {
      *lastModification = currentdate;
    } else {
      _propertiesMap_.insert("lastModification", currentdate);
    }
  }

  NodeSet GraphicalModel::nodeset(const std::vector< std::string >& names) const {
    NodeSet res;
    for (const auto& name: names) {
      res.insert(idFromName(name));
    }
    return res;
  }

  std::string GraphicalModel::spaceCplxToString(double dSize, int dim, Size usedMem) {
    std::string result;
    if (dSize > 6) result = std::format("domainSize: 10^{:.6g}", dSize);
    else result = std::format("domainSize: {}", (long long)std::round(std::pow(10.0, dSize)));

    result += std::format(", dim: {}, mem: ", dim);

    if (const Size go = usedMem / (1024 * 1024 * 1024); go > 0) result += std::format("{}Go ", go);
    if (const Size mo = (usedMem / (1024 * 1024)) % 1024; mo > 0)
      result += std::format("{}Mo ", mo);
    if (const Size ko = (usedMem / 1024) % 1024; ko > 0) result += std::format("{}Ko ", ko);
    result += std::format("{}o", usedMem % 1024);
    return result;
  }
}   // namespace gum
