/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * License); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#ifndef COMMON_TSBLOCK_TUPLE_DESC_H
#define COMMON_TSBLOCK_TUPLE_DESC_H

#include <stddef.h>
#include <stdint.h>

#include <string>
#include <vector>

#include "utils/db_utils.h"
#include "utils/errno_define.h"

namespace common {

/*
 * Anyway, a default length of text type is needed for easy calculation,
 * and it will be changed to a configurable item later
 */
#define DEFAULT_RESERVED_SIZE_OF_TEXT 16
#define TEXT_LEN 4
#define DEFAULT_RESERVED_SIZE_OF_STRING 16
#define STRING_LEN 4

extern uint32_t get_len(TSDataType type);

// Describe merged information of multiple timeseries
class TupleDesc {
   public:
    TupleDesc() {}
    virtual ~TupleDesc() {}

    FORCE_INLINE void reset() {
        column_list_.clear();
        time_column_index_ = -1;
    }

    FORCE_INLINE void push_back(ColumnSchema schema) {
        if (schema.column_category_ == ColumnCategory::TIME) {
            ASSERT(time_column_index_ == -1);
            time_column_index_ = static_cast<int>(column_list_.size());
        }
        column_list_.push_back(schema);
    }

    FORCE_INLINE uint32_t get_column_count() const {
        return column_list_.size();
    }

    FORCE_INLINE ColumnSchema& operator[](uint32_t index) {
        ASSERT(index < column_list_.size());
        return column_list_[index];
    }

    FORCE_INLINE ColumnSchema& get_column_schema(uint32_t index) {
        ASSERT(index < column_list_.size());
        return column_list_[index];
    }

    FORCE_INLINE common::TSDataType get_column_type(uint32_t index) {
        return column_list_[index].data_type_;
    }

    FORCE_INLINE common::ColumnCategory get_column_category(
        const uint32_t index) const {
        return column_list_[index].column_category_;
    }

    FORCE_INLINE int get_time_column_index() const {
        return time_column_index_;
    }

    FORCE_INLINE std::string get_column_name(uint32_t index) {
        return column_list_[index].column_name_;
    }

    FORCE_INLINE void remove_column(uint32_t idx) {
        ASSERT(idx < column_list_.size());

        const bool removing_time =
            (static_cast<int>(idx) == time_column_index_);

        column_list_.erase(column_list_.begin() + idx);

        if (removing_time) {
            time_column_index_ = -1;
        } else if (time_column_index_ != -1 &&
                   static_cast<int>(idx) < time_column_index_) {
            --time_column_index_;
        }
    }

    // get the single row len, ignore nulls and select-list memory for the
    // moment
    uint32_t get_single_row_len(int* erro_code);

    bool equal_to(const TupleDesc& that) const {
        if (column_list_.size() != that.column_list_.size()) {
            return false;
        }
        for (uint32_t i = 0; i < column_list_.size(); i++) {
            const ColumnSchema& this_col_schema = column_list_[i];
            const ColumnSchema& that_col_schema = that.column_list_[i];
            if (this_col_schema != that_col_schema) {
                return false;
            }
        }
        if (time_column_index_ != that.time_column_index_) {
            return false;
        }
        return true;
    }

    void clone_from(TupleDesc* that) {
        ASSERT(column_list_.size() == 0);
        column_list_ = that->column_list_;  // deep copy
        time_column_index_ = that->time_column_index_;
    }

#ifdef DEBUG
    std::string debug_string()  // for debug
    {
        std::stringstream out;
        for (size_t i = 0; i < column_list_.size(); ++i) {
            out << column_list_[i].debug_string() << std::endl;
        }
        return out.str();
    }
#endif
   private:
    std::vector<ColumnSchema> column_list_;
    int time_column_index_ = -1;
};

}  // namespace common

#endif  // COMMON_TSBLOCK_TUPLE_DESC_H
