#include <errno.h>
#include <time.h>

#include <optional>

#include <Common/ProfileEvents.h>
#include <Common/Stopwatch.h>

#include <Common/Exception.h>
#include <Common/CurrentMetrics.h>

#include <IO/ReadBufferFromHDFS.h>
#include <IO/WriteHelpers.h>

#include <common/logger_useful.h>


namespace ProfileEvents
{
    extern const Event ReadBufferFromFileDescriptorRead;
    extern const Event ReadBufferFromFileDescriptorReadFailed;
    extern const Event ReadBufferFromFileDescriptorReadBytes;
    extern const Event Seek;
}

namespace CurrentMetrics
{
    extern const Metric Read;
}

namespace DB
{

namespace ErrorCodes
{
    extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
    extern const int ARGUMENT_OUT_OF_BOUND;
    extern const int CANNOT_SEEK_THROUGH_FILE;
    extern const int CANNOT_SELECT;
}


std::string ReadBufferFromHDFS::getFileName() const
{
    return "(fd = " + toString(fd) + ")";
}


bool ReadBufferFromHDFS::nextImpl()
{
    size_t bytes_read = 0;
    while (!bytes_read)
    {
        ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorRead);

        std::optional<Stopwatch> watch;
        if (profile_callback)
            watch.emplace(clock_type);

        ssize_t res = 0;
        {
            CurrentMetrics::Increment metric_increment{CurrentMetrics::Read};
            // Reading
            size_t read_count = 0;
            if(isptr == nullptr)  throw Exception("BufferHDFS : Null InputStream pointer was given",ErrorCodes::ARGUMENT_OUT_OF_BOUND);
            isptr->PositionRead(internal_buffer.begin(), internal_buffer.size(), pos_in_file, &read_count);
            res = read_count;
            LOG_DEBUG(&Logger::get("StorageHDFS"), " BufferHDFS read : " << read_count); // for debug
            //res = ::read(fd, internal_buffer.begin(), internal_buffer.size());
        }

        if (!res)
            break;

        if (-1 == res && errno != EINTR)
        {
            ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorReadFailed);
            throwFromErrno("Cannot read from file " + getFileName(), ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR);
        }

        if (res > 0)
            bytes_read += res;

        if (profile_callback)
        {
            ProfileInfo info;
            info.bytes_requested = internal_buffer.size();
            info.bytes_read = res;
            info.nanoseconds = watch->elapsed();
            profile_callback(info);
        }
    }

    pos_in_file += bytes_read;

    if (bytes_read)
    {
        ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorReadBytes, bytes_read);
        working_buffer.resize(bytes_read);
    }
    else {
        is_last_read_success = false;
        return false;
    }

    return true;
}


/// If 'offset' is small enough to stay in buffer after seek, then true seek in file does not happen.
off_t ReadBufferFromHDFS::doSeek(off_t offset, int whence)
{
    off_t new_pos = offset;
    if (whence == SEEK_CUR)
        new_pos = pos_in_file - (working_buffer.end() - pos) + offset;
    else if (whence != SEEK_SET)
        throw Exception("ReadBufferFromHDFS::seek expects SEEK_SET or SEEK_CUR as whence", ErrorCodes::ARGUMENT_OUT_OF_BOUND);

    /// Position is unchanged.
    if (new_pos + (working_buffer.end() - pos) == pos_in_file) // which means offset = 0
        return new_pos;

    if (hasPendingData() && new_pos <= pos_in_file && new_pos >= pos_in_file - static_cast<off_t>(working_buffer.size()))
    {
        /// Position is still inside buffer.
        pos = working_buffer.begin() + (new_pos - (pos_in_file - working_buffer.size()));
        return new_pos;
    }
    else
    {
        ProfileEvents::increment(ProfileEvents::Seek);
        pos = working_buffer.end();
        pos_in_file = new_pos;
        return new_pos;
        // what to seek?
        //off_t res = lseek(fd, new_pos, SEEK_SET);
        /*off_t res = -1;
        if (-1 == res)
            throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
        */
    }
}


/// Assuming file descriptor supports 'select', check that we have data to read or wait until timeout.
bool ReadBufferFromHDFS::poll(size_t timeout_microseconds)
{
    static_cast<void>(timeout_microseconds);
    return is_last_read_success;

    /*fd_set fds;
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    timeval timeout = { time_t(timeout_microseconds / 1000000), suseconds_t(timeout_microseconds % 1000000) };

    int res = select(1, &fds, 0, 0, &timeout);

    if (-1 == res)
        throwFromErrno("Cannot select", ErrorCodes::CANNOT_SELECT);
    */

}

}
