/*
 * Copyright (C) 2016 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michi Henning <michi.henning@canonical.com>
 */

#include <lomiri/storage/qt/internal/RuntimeImpl.h>

#include "RegistryInterface.h"
#include <lomiri/storage/internal/dbusmarshal.h>
#include <lomiri/storage/internal/EnvVars.h>
#include <lomiri/storage/qt/internal/AccountImpl.h>
#include <lomiri/storage/qt/internal/AccountsJobImpl.h>
#include <lomiri/storage/qt/internal/StorageErrorImpl.h>
#include <lomiri/storage/qt/Downloader.h>
#include <lomiri/storage/qt/ItemJob.h>
#include <lomiri/storage/qt/ItemListJob.h>
#include <lomiri/storage/qt/Runtime.h>
#include <lomiri/storage/qt/Uploader.h>
#include <lomiri/storage/qt/VoidJob.h>
#include <lomiri/storage/registry/Registry.h>

#include <QDBusError>
#include <QDBusMetaType>

using namespace std;

namespace lomiri
{
namespace storage
{
namespace qt
{
namespace internal
{
namespace
{

void register_meta_types()
{
    qRegisterMetaType<lomiri::storage::qt::AccountsJob::Status>();
    qRegisterMetaType<lomiri::storage::qt::Account>();
    qRegisterMetaType<QList<lomiri::storage::qt::Account>>();
    qRegisterMetaType<lomiri::storage::qt::Downloader::Status>();
    qRegisterMetaType<lomiri::storage::qt::Item>();
    qRegisterMetaType<QList<lomiri::storage::qt::Item>>();
    qRegisterMetaType<lomiri::storage::qt::ItemJob::Status>();
    qRegisterMetaType<lomiri::storage::qt::ItemListJob::Status>();
    qRegisterMetaType<lomiri::storage::qt::Uploader::Status>();
    qRegisterMetaType<lomiri::storage::qt::VoidJob::Status>();

    qDBusRegisterMetaType<lomiri::storage::internal::ItemMetadata>();
    qDBusRegisterMetaType<QList<lomiri::storage::internal::ItemMetadata>>();

    qDBusRegisterMetaType<lomiri::storage::internal::AccountDetails>();
    qDBusRegisterMetaType<QList<lomiri::storage::internal::AccountDetails>>();
}

}

RuntimeImpl::RuntimeImpl()
    : RuntimeImpl(QDBusConnection::sessionBus())
{
}

RuntimeImpl::RuntimeImpl(QDBusConnection const& conn)
    : is_valid_(true)
    , conn_(conn)
    , registry_(new RegistryInterface(storage::registry::BUS_NAME,
                                      storage::registry::OBJECT_PATH,
                                      conn_))
{
    register_meta_types();
}

RuntimeImpl::~RuntimeImpl()
{
    shutdown();
}

bool RuntimeImpl::isValid() const
{
    return is_valid_;
}

StorageError RuntimeImpl::error() const
{
    return error_;
}

QDBusConnection RuntimeImpl::connection() const
{
    return conn_;
}

AccountsJob* RuntimeImpl::accounts() const
{
    QString const method = "Runtime::accounts()";

    if (!is_valid_)
    {
        QString msg = "Runtime::accounts(): Runtime was destroyed previously";
        return AccountsJobImpl::make_job(StorageErrorImpl::runtime_destroyed_error(msg));
    }

    auto reply = registry_->ListAccounts();
    auto This = const_pointer_cast<RuntimeImpl>(shared_from_this());
    return AccountsJobImpl::make_job(This, method, reply);
}

StorageError RuntimeImpl::shutdown()
{
    if (is_valid_)
    {
        is_valid_ = false;
        return StorageError();
    }
    error_ = StorageErrorImpl::runtime_destroyed_error("Runtime::shutdown(): Runtime was destroyed previously");
    return error_;
}

Account RuntimeImpl::make_test_account(QString const& bus_name,
                                       QString const& object_path,
                                       quint32 id,
                                       QString const& service_id,
                                       QString const& name)
{
    storage::internal::AccountDetails ad{bus_name, QDBusObjectPath(object_path), id, service_id, name, "", ""};
    return AccountImpl::make_account(shared_from_this(), ad);
}

}  // namespace internal
}  // namespace qt
}  // namespace storage
}  // namespace lomiri
