////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////

#include <faiss/IndexFlat.h>
#include <faiss/AutoTune.h>
#include <faiss/MetaIndexes.h>
#include <faiss/index_io.h>
#include <faiss/gpu/GpuAutoTune.h>


#include "knowhere/common/exception.h"
#include "knowhere/adapter/faiss_adopt.h"
#include "knowhere/index/vector_index/idmap.h"


namespace zilliz {
namespace knowhere {

BinarySet IDMAP::Serialize() {
    if (!index_) {
        KNOWHERE_THROW_MSG("index not initialize");
    }

    std::lock_guard<std::mutex> lk(mutex_);
    return SerializeImpl();
}

void IDMAP::Load(const BinarySet &index_binary) {
    std::lock_guard<std::mutex> lk(mutex_);
    LoadImpl(index_binary);
}

DatasetPtr IDMAP::Search(const DatasetPtr &dataset, const Config &config) {
    if (!index_) {
        KNOWHERE_THROW_MSG("index not initialize");
    }

    auto k = config["k"].as<size_t>();
    //auto metric_type = config["metric_type"].as_string() == "L2" ?
    //                   faiss::METRIC_L2 : faiss::METRIC_INNER_PRODUCT;
    //index_->metric_type = metric_type;

    GETTENSOR(dataset)

    // TODO(linxj): handle malloc exception
    auto elems = rows * k;
    auto res_ids = (int64_t *) malloc(sizeof(int64_t) * elems);
    auto res_dis = (float *) malloc(sizeof(float) * elems);

    index_->search(rows, (float *) p_data, k, res_dis, res_ids);

    auto id_buf = MakeMutableBufferSmart((uint8_t *) res_ids, sizeof(int64_t) * elems);
    auto dist_buf = MakeMutableBufferSmart((uint8_t *) res_dis, sizeof(float) * elems);

    // TODO: magic
    std::vector<BufferPtr> id_bufs{nullptr, id_buf};
    std::vector<BufferPtr> dist_bufs{nullptr, dist_buf};

    auto int64_type = std::make_shared<arrow::Int64Type>();
    auto float_type = std::make_shared<arrow::FloatType>();

    auto id_array_data = arrow::ArrayData::Make(int64_type, elems, id_bufs);
    auto dist_array_data = arrow::ArrayData::Make(float_type, elems, dist_bufs);

    auto ids = std::make_shared<NumericArray<arrow::Int64Type>>(id_array_data);
    auto dists = std::make_shared<NumericArray<arrow::FloatType>>(dist_array_data);
    std::vector<ArrayPtr> array{ids, dists};

    return std::make_shared<Dataset>(array, nullptr);
}

void IDMAP::Add(const DatasetPtr &dataset, const Config &config) {
    if (!index_) {
        KNOWHERE_THROW_MSG("index not initialize");
    }

    std::lock_guard<std::mutex> lk(mutex_);
    GETTENSOR(dataset)

    // TODO: magic here.
    auto array = dataset->array()[0];
    auto p_ids = array->data()->GetValues<long>(1, 0);

    index_->add_with_ids(rows, (float *) p_data, p_ids);
}

int64_t IDMAP::Count() {
    return index_->ntotal;
}

int64_t IDMAP::Dimension() {
    return index_->d;
}

// TODO(linxj): return const pointer
float *IDMAP::GetRawVectors() {
    try {
        auto file_index = dynamic_cast<faiss::IndexIDMap *>(index_.get());
        auto flat_index = dynamic_cast<faiss::IndexFlat*>(file_index->index);
        return flat_index->xb.data();
    } catch (std::exception &e) {
        KNOWHERE_THROW_MSG(e.what());
    }
}

// TODO(linxj): return const pointer
int64_t *IDMAP::GetRawIds() {
    try {
        auto file_index = dynamic_cast<faiss::IndexIDMap *>(index_.get());
        return file_index->id_map.data();
    } catch (std::exception &e) {
        KNOWHERE_THROW_MSG(e.what());
    }
}

const char* type = "IDMap,Flat";
void IDMAP::Train(const Config &config) {
    auto metric_type = config["metric_type"].as_string() == "L2" ?
                       faiss::METRIC_L2 : faiss::METRIC_INNER_PRODUCT;
    auto dim = config["dim"].as<size_t>();

    auto index = faiss::index_factory(dim, type, metric_type);
    index_.reset(index);
}

VectorIndexPtr IDMAP::Clone() {
    std::lock_guard<std::mutex> lk(mutex_);

    auto clone_index = faiss::clone_index(index_.get());
    std::shared_ptr<faiss::Index> new_index;
    new_index.reset(clone_index);
    return std::make_shared<IDMAP>(new_index);
}

VectorIndexPtr IDMAP::CopyCpuToGpu(const int64_t &device_id, const Config &config) {
    if (auto res = FaissGpuResourceMgr::GetInstance().GetRes(device_id)){
        ResScope rs(device_id, res);
        auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), device_id, index_.get());

        std::shared_ptr<faiss::Index> device_index;
        device_index.reset(gpu_index);
        return std::make_shared<GPUIDMAP>(device_index, device_id, res);
    } else {
        KNOWHERE_THROW_MSG("CopyCpuToGpu Error, can't get gpu_resource");
    }
}

void IDMAP::Seal() {
    // do nothing
}

VectorIndexPtr GPUIDMAP::CopyGpuToCpu(const Config &config) {
    std::lock_guard<std::mutex> lk(mutex_);

    faiss::Index *device_index = index_.get();
    faiss::Index *host_index = faiss::gpu::index_gpu_to_cpu(device_index);

    std::shared_ptr<faiss::Index> new_index;
    new_index.reset(host_index);
    return std::make_shared<IDMAP>(new_index);
}

VectorIndexPtr GPUIDMAP::Clone() {
    auto cpu_idx = CopyGpuToCpu(Config());

    if (auto idmap = std::dynamic_pointer_cast<IDMAP>(cpu_idx)){
        return idmap->CopyCpuToGpu(gpu_id_, Config());
    }
    else {
        KNOWHERE_THROW_MSG("IndexType not Support GpuClone");
    }
}

BinarySet GPUIDMAP::SerializeImpl() {
    try {
        MemoryIOWriter writer;
        {
            faiss::Index *index = index_.get();
            faiss::Index *host_index = faiss::gpu::index_gpu_to_cpu(index);

            faiss::write_index(host_index, &writer);
            delete host_index;
        }
        auto data = std::make_shared<uint8_t>();
        data.reset(writer.data_);

        BinarySet res_set;
        res_set.Append("IVF", data, writer.rp);

        return res_set;
    } catch (std::exception &e) {
        KNOWHERE_THROW_MSG(e.what());
    }
}

void GPUIDMAP::LoadImpl(const BinarySet &index_binary) {
    auto binary = index_binary.GetByName("IVF");
    MemoryIOReader reader;
    {
        reader.total = binary->size;
        reader.data_ = binary->data.get();

        faiss::Index *index = faiss::read_index(&reader);

        if (auto res = FaissGpuResourceMgr::GetInstance().GetRes(gpu_id_) ){
            ResScope rs(gpu_id_, res);
            auto device_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id_, index);
            index_.reset(device_index);
        } else {
            KNOWHERE_THROW_MSG("Load error, can't get gpu resource");
        }

        delete index;
    }
}

VectorIndexPtr GPUIDMAP::CopyGpuToGpu(const int64_t &device_id, const Config &config) {
    auto cpu_index = CopyGpuToCpu(config);
    return std::static_pointer_cast<IDMAP>(cpu_index)->CopyCpuToGpu(device_id, config);
}

float *GPUIDMAP::GetRawVectors() {
    KNOWHERE_THROW_MSG("Not support");
}

int64_t *GPUIDMAP::GetRawIds() {
    KNOWHERE_THROW_MSG("Not support");
}

}
}
