/**
 * Copyright (C) 2021 Saturneric
 *
 * This file is part of GpgFrontend.
 *
 * GpgFrontend 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.
 *
 * GpgFrontend 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 GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from
 * the gpg4usb project, which is under GPL-3.0-or-later.
 *
 * All the source code of GpgFrontend was modified and released by
 * Saturneric<eric@bktus.com> starting on May 12, 2021.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifndef GPGFRONTEND_GLOBALSETTINGSTATION_H
#define GPGFRONTEND_GLOBALSETTINGSTATION_H

#include <openssl/x509.h>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <nlohmann/json.hpp>

#include "GpgFrontendBuildInstallInfo.h"
#include "ui/GpgFrontendUI.h"

namespace vmime::security::cert {
class defaultCertificateVerifier;
class X509Certificate;
} // namespace vmime::security::cert

namespace GpgFrontend::UI {

/**
 * @brief
 *
 */
class GlobalSettingStation : public QObject {
  Q_OBJECT
public:
  /**
   * @brief Get the Instance object
   *
   * @return GlobalSettingStation&
   */
  static GlobalSettingStation &GetInstance();

  /**
   * @brief Construct a new Global Setting Station object
   *
   */
  GlobalSettingStation() noexcept;

  /**
   * @brief Destroy the Global Setting Station object
   *
   */
  ~GlobalSettingStation() noexcept override;

  /**
   * @brief
   *
   * @return libconfig::Setting&
   */
  libconfig::Setting &GetUISettings() noexcept { return ui_cfg_.getRoot(); }

  /**
   * @brief Get the App Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetAppDir() const { return app_path_; }

  /**
   * @brief Get the Log Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetLogDir() const {
    return app_log_path_;
  }

  /**
   * @brief Get the Standalone Database Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetStandaloneDatabaseDir() const {
    auto db_path = app_configure_path_ / "db";
    if (!boost::filesystem::exists(db_path)) {
      boost::filesystem::create_directory(db_path);
    }
    return db_path;
  }

  /**
   * @brief Get the Standalone Gpg Bin Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetStandaloneGpgBinDir() const {
    return app_resource_path_ / "gpg1.4" / "gpg";
  }

  /**
   * @brief Get the Locale Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetLocaleDir() const {
    return app_locale_path_;
  }

  /**
   * @brief Get the Resource Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetResourceDir() const {
    return app_resource_path_;
  }

  /**
   * @brief Get the Certs Dir object
   *
   * @return boost::filesystem::path
   */
  [[nodiscard]] boost::filesystem::path GetCertsDir() const {
    return app_resource_path_ / "certs";
  }

  /**
   * @brief Get the Cert Verifier object
   *
   * @return std::shared_ptr<
   * vmime::security::cert::defaultCertificateVerifier>
   */
  [[nodiscard]] std::shared_ptr<
      vmime::security::cert::defaultCertificateVerifier>
  GetCertVerifier() const;

  /**
   * @brief
   *
   * @param path
   */
  void AddRootCert(const boost::filesystem::path &path);

  /**
   * @brief Get the Root Certs object
   *
   * @return const std::vector<std::shared_ptr<X509>>&
   */
  const std::vector<std::shared_ptr<X509>> &GetRootCerts();

  /**
   * @brief
   *
   */
  void ResetRootCerts() { root_certs_.clear(); }

  /**
   * @brief
   *
   */
  void SyncSettings() noexcept;

  /**
   * @brief
   *
   * @param _key
   * @param value
   * @return std::string
   */

  std::string SaveDataObj(const std::string &_key, const nlohmann::json &value);

  /**
   * @brief Get the Data Object object
   *
   * @param _key
   * @return std::optional<nlohmann::json>
   */
  std::optional<nlohmann::json> GetDataObject(const std::string &_key);

  /**
   * @brief Get the Data Object By Ref object
   *
   * @param _ref
   * @return std::optional<nlohmann::json>
   */
  std::optional<nlohmann::json> GetDataObjectByRef(const std::string &_ref);

private:
  boost::filesystem::path app_path_ =
      qApp->applicationDirPath().toStdString(); ///< Program Location
  boost::filesystem::path app_data_path_ =
      QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
          .toStdString(); ///< Program Data Location
  boost::filesystem::path app_log_path_ =
      app_data_path_ / "logs"; ///< Program Data Location
  boost::filesystem::path app_data_objs_path_ =
      app_data_path_ / "objs"; ///< Object storage path

#ifdef LINUX_INSTALL_BUILD
  boost::filesystem::path app_resource_path_ =
      boost::filesystem::path(APP_LOCALSTATE_PATH) /
      "gpgfrontend"; ///< Program Data Location
#else
  boost::filesystem::path app_resource_path_ =
      RESOURCE_DIR_BOOST_PATH(app_path_); ///< Program Data Location
#endif

#ifdef LINUX_INSTALL_BUILD
  boost::filesystem::path app_locale_path_ =
      std::string(APP_LOCALE_PATH); ///< Program Data Location
#else
  boost::filesystem::path app_locale_path_ =
      app_resource_path_ / "locales"; ///< Program Data Location
#endif

  boost::filesystem::path app_configure_path_ =
      QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)
          .toStdString(); ///< Program Configure Location
  boost::filesystem::path app_secure_path_ =
      app_configure_path_ / "secure"; ///< Where sensitive information is stored
  boost::filesystem::path app_secure_key_path_ =
      app_secure_path_ / "app.key"; ///<
  boost::filesystem::path ui_config_dir_path_ =
      app_configure_path_ /
      "UserInterface"; ///< Configure File Directory Location
  boost::filesystem::path ui_config_path_ =
      ui_config_dir_path_ / "ui.cfg"; ///< UI Configure File Location

  libconfig::Config ui_cfg_;                      ///<
  std::vector<std::shared_ptr<X509>> root_certs_; ///<
  std::random_device rd_;                         ///<
  std::mt19937 mt_;                               ///<
  QByteArray hash_key_;                           ///<

  static std::unique_ptr<GlobalSettingStation> instance_; ///<

  /**
   * @brief
   *
   */
  void init_app_secure_key();

  /**
   * @brief
   *
   * @param len
   * @return std::string
   */
  std::string generate_passphrase(int len);
};
} // namespace GpgFrontend::UI

#endif // GPGFRONTEND_GLOBALSETTINGSTATION_H
