/*
 * Copyright (C) 2026 UBports Foundation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Vendor.h"

#include "LocalVendor.h"
#include "NextcloudVendor.h"

#include <lomiri/storage/provider/DownloadJob.h>
#include <lomiri/storage/provider/Exceptions.h>
#include <lomiri/storage/provider/Item.h>
#include <lomiri/storage/provider/UploadJob.h>

#include <atomic>
#include <tuple>

using namespace std;
using namespace lomiri::storage::provider;

Vendor::~Vendor() = default;

string Vendor::next_remote_name(string const& prefix)
{
    static atomic<unsigned> counter{0};
    return prefix + to_string(counter++);
}

shared_ptr<Vendor> Vendor::for_context(Context const& ctx)
{
    if (LocalVendor::is_valid_context(ctx)) return make_shared<LocalVendor>(ctx);
    if (NextcloudVendor::is_valid_context(ctx)) return make_shared<NextcloudVendor>(ctx);

    throw InvalidArgumentException("No vendor can serve this account");
}

template <typename T>
void Vendor::with_fs(shared_ptr<RcloneClient> const& client,
                     shared_ptr<boost::promise<T>> const& promise,
                     WithFsCallback body)
{
    get_conn_string(client, [promise, body](string const& fs,
                                            boost::exception_ptr const& error) {
        if (error)
        {
            promise->set_exception(error);
            return;
        }

        try
        {
            body(fs);
        }
        catch (...)
        {
            promise->set_exception(boost::current_exception());
        }
    });
}

template void Vendor::with_fs(shared_ptr<RcloneClient> const&,
                              shared_ptr<boost::promise<ItemList>> const&,
                              WithFsCallback);
template void Vendor::with_fs(shared_ptr<RcloneClient> const&,
                              shared_ptr<boost::promise<tuple<ItemList, string>>> const&,
                              WithFsCallback);
template void Vendor::with_fs(shared_ptr<RcloneClient> const&,
                              shared_ptr<boost::promise<Item>> const&,
                              WithFsCallback);
template void Vendor::with_fs(shared_ptr<RcloneClient> const&,
                              shared_ptr<boost::promise<unique_ptr<UploadJob>>> const&,
                              WithFsCallback);
template void Vendor::with_fs(shared_ptr<RcloneClient> const&,
                              shared_ptr<boost::promise<unique_ptr<DownloadJob>>> const&,
                              WithFsCallback);
template void Vendor::with_fs(shared_ptr<RcloneClient> const&,
                              shared_ptr<boost::promise<void>> const&,
                              WithFsCallback);
