/*
 * Copyright (c) 2026 Huawei Device Co., Ltd.
 * Licensed 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.
 */
#include "PCMFileReader.h"

PCMFileReader::PCMFileReader()
{}

PCMFileReader::~PCMFileReader()
{
    Close();
}

int32_t PCMFileReader::Open(
    const std::string &filePath, int32_t sampleRate, int32_t channelCount, int32_t bitsPerSample)
{
    filePath_ = filePath;
    sampleRate_ = sampleRate;
    channelCount_ = channelCount;
    bitsPerSample_ = bitsPerSample;
    bytesPerSample_ = bitsPerSample / 8; // 8:bits to byte
    frameSize_ = channelCount_ * bytesPerSample_;
    file_.open(filePath_, std::ios::binary | std::ios::in);
    if (!file_.is_open()) {
        AVCODEC_SAMPLE_LOGE("Failed to open PCM file: %{public}s", filePath.c_str());
        return -1;
    }
    file_.clear();
    file_.seekg(0, std::ios::end);
    if (file_.fail()) {
        AVCODEC_SAMPLE_LOGE("Failed to seek to end of file: %{public}s", filePath.c_str());
        file_.close();
        return -1;
    }
    fileSize_ = file_.tellg();
    file_.clear();
    file_.seekg(0, std::ios::beg);
    currentPosition_ = 0;
    if (fileSize_ <= 0) {
        AVCODEC_SAMPLE_LOGE("File size is zero or negative: %{public}lld", (long long)fileSize_);
    }
    return 0;
}

int32_t PCMFileReader::Read(uint8_t *buffer, int32_t bytesToRead)
{
    if (!file_.is_open()) {
        AVCODEC_SAMPLE_LOGE("File is not open");
        return -1;
    }
    if (buffer == nullptr || bytesToRead <= 0) {
        AVCODEC_SAMPLE_LOGE(
            "Invalid parameters: buffer=%{public}s, bytesToRead=%{public}d", buffer ? "valid" : "null", bytesToRead);
        return -1;
    }
    if (fileSize_ <= 0) {
        AVCODEC_SAMPLE_LOGE("File size is %{public}lld, cannot read", (long long)fileSize_);
        return -1;
    }
    if (currentPosition_ >= fileSize_) {
        AVCODEC_SAMPLE_LOGD("Already at end of file, pos=%{public}lld, size=%{public}lld",
            (long long)currentPosition_,
            (long long)fileSize_);
        return 0;
    }
    file_.read(reinterpret_cast<char *>(buffer), bytesToRead);
    int32_t bytesRead = file_.gcount();
    if (bytesRead < bytesToRead) {
        if (file_.eof()) {
            AVCODEC_SAMPLE_LOGD("Reached end of file, bytesRead=%{public}d", bytesRead);
        } else {
            AVCODEC_SAMPLE_LOGE("Read error: expected %{public}d bytes, got %{public}d", bytesToRead, bytesRead);
        }
    }
    currentPosition_ += bytesRead;
    return bytesRead;
}

void PCMFileReader::Close()
{
    if (file_.is_open()) {
        file_.close();
        AVCODEC_SAMPLE_LOGI("Closed PCM file: %{public}s", filePath_.c_str());
    }
    currentPosition_ = 0;
}

void PCMFileReader::Reset()
{
    if (file_.is_open()) {
        file_.clear();
        file_.seekg(0, std::ios::beg);
        currentPosition_ = 0;
        AVCODEC_SAMPLE_LOGI("Reset PCM file position to beginning, size=%{public}lld", (long long)fileSize_);
    }
}

bool PCMFileReader::IsEOF() const
{
    if (fileSize_ <= 0) {
        return true;
    }
    return currentPosition_ >= fileSize_;
}