/*
 * Copyright 2015 Rockchip Electronics 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.
 */
 
#define MODULE_TAG "CameraReader"
 
// #include "mpp_log.h"
// #include "mpp_mem.h"
#include "Camera2Reader.h"
#include <dirent.h>

#include <iomanip>
#include <string>
#include <sstream>
 
Camera2Reader::Camera2Reader(const char *device, int bufcnt, int width, int height, int fmt)
    : mBufcnt(bufcnt),
      mWidth(width),
      mHeight(height),
      mFmt(fmt)
{
    strcpy(mDevice, device);
}
Camera2Reader::Camera2Reader(int cameraId) : mCameraId(cameraId)
{
}

int Camera2Reader::Init(const char *device, int bufcnt, int width, int height, int format)
{
    return 0;
}
 
// Free a context to capture frames from <fname>.
// Returns NULL on error.
int Camera2Reader::Deinit()
{
    return 0;
}
 
bool Camera2Reader::Open(const char* path, const char* fileName)
{
    mPath.assign(path);
    mFileName.assign(fileName);

    ACameraIdList *cameraIdList = NULL;
    ACameraMetadata *cameraMetadata = NULL;
 
    const char *selectedCameraId = NULL;
    camera_status_t camera_status = ACAMERA_OK;
    ACameraManager *cameraManager = ACameraManager_create();
 
    camera_status = ACameraManager_getCameraIdList(cameraManager, &cameraIdList);
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to get camera id list (reason: %d)\n", camera_status);
        return false;
    }
 
    if (cameraIdList->numCameras < 1 ) {
        LOGI("No camera device detected.\n");
        return false;
    }

    if (cameraIdList->numCameras <= mCameraId ) {
        LOGI("No required camera device %d detected.\n", mCameraId);
        return false;
    }
 
    selectedCameraId = cameraIdList->cameraIds[mCameraId];
 
    LOGI("Trying to open Camera2 (id: %s, num of camera : %d)\n", selectedCameraId,
         cameraIdList->numCameras);
 
    camera_status = ACameraManager_getCameraCharacteristics(cameraManager, selectedCameraId,
                                                            &cameraMetadata);
 
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to get camera meta data of ID:%s\n", selectedCameraId);
    }
 
    deviceStateCallbacks.onDisconnected = camera_device_on_disconnected;
    deviceStateCallbacks.onError = camera_device_on_error;
 
    camera_status = ACameraManager_openCamera(cameraManager, selectedCameraId,
                                              &deviceStateCallbacks, &cameraDevice);
 
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to open camera device (id: %s)\n", selectedCameraId);
    }
 
    camera_status = ACameraDevice_createCaptureRequest(cameraDevice, TEMPLATE_PREVIEW,
                                                       &captureRequest);
 
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to create preview capture request (id: %s)\n", selectedCameraId);
    }
 
    ACaptureSessionOutputContainer_create(&captureSessionOutputContainer);
 
    captureSessionStateCallbacks.onReady = capture_session_on_ready;
    captureSessionStateCallbacks.onActive = capture_session_on_active;
    captureSessionStateCallbacks.onClosed = capture_session_on_closed;
 
    ACameraMetadata_free(cameraMetadata);
    ACameraManager_deleteCameraIdList(cameraIdList);
    ACameraManager_delete(cameraManager);

    media_status_t status;
    // status = AImageReader_new(1920, 1080, AIMAGE_FORMAT_YUV_420_888, 5, &mAImageReader);
    status = AImageReader_new(1920, 1080, AIMAGE_FORMAT_JPEG, 5, &mAImageReader);
    if (status != AMEDIA_OK)
    {
        LOGI("AImageReader_new error\n");
        return false;
    }
 
    AImageReader_ImageListener listener{
            .context = this,
            .onImageAvailable = OnImageCallback,
    };
    AImageReader_setImageListener(mAImageReader, &listener);
 
    //ANativeWindow *mNativeWindow;
    status = AImageReader_getWindow(mAImageReader, &theNativeWindow);
    if (status != AMEDIA_OK)
    {
        LOGI("AImageReader_getWindow error\n");
        return false;
    }
 
    LOGI("Surface is prepared in %p.\n", theNativeWindow);
 
    ACameraOutputTarget_create(theNativeWindow, &cameraOutputTarget);
    ACaptureRequest_addTarget(captureRequest, cameraOutputTarget);
 
    ACaptureSessionOutput_create(theNativeWindow, &sessionOutput);
    ACaptureSessionOutputContainer_add(captureSessionOutputContainer, sessionOutput);
 
    ACameraDevice_createCaptureSession(cameraDevice, captureSessionOutputContainer,
                                       &captureSessionStateCallbacks, &captureSession);
 
    // ACameraCaptureSession_setRepeatingRequest(captureSession, NULL, 1, &captureRequest, NULL);
    ACameraCaptureSession_capture(captureSession, NULL, 1, &captureRequest, NULL);
    LOGI("Surface is prepared in here.\n");
    return true;
}
 
ACameraCaptureSession_stateCallbacks *Camera2Reader::GetSessionListener()
{
    static ACameraCaptureSession_stateCallbacks sessionListener = {
        .context = this,
        .onClosed = Camera2Reader::capture_session_on_closed,
        .onReady = Camera2Reader::capture_session_on_ready,
        .onActive = Camera2Reader::capture_session_on_active,
    };
    return &sessionListener;
}
 
void Camera2Reader::ImageCallback(AImageReader *reader)
{
    int32_t format;
    AImage *image = nullptr;
    media_status_t status = AImageReader_acquireNextImage(reader, &image);
    LOGI("ImageCallback\n");
    if (status == AMEDIA_OK && image)
    {
        LOGI("ImageCallback\n");
        AImage_delete(image);
    }
}
void Camera2Reader::OnImageCallback(void *ctx, AImageReader *reader)
{
    Camera2Reader* pThis = reinterpret_cast<Camera2Reader*>(ctx);
    AImage *image = nullptr;
    media_status_t status = AImageReader_acquireNextImage(reader, &image);
    if (status == AMEDIA_OK && image)
    {
        WriteFile(pThis, image, pThis->mPath);
        // image.
        AImage_delete(image);
        pThis->Deinit();
        delete pThis;
    }
}
 
bool Camera2Reader::readyToRun()
{
    if (!Init(mDevice, mBufcnt, mWidth, mHeight, mFmt))
    {
        LOGI("Init false\n");
        return false;
    }
 
    return Open("", "");
}
void Camera2Reader::start()
{
    //run();
}
 
void Camera2Reader::stop()
{
    //threadStop();
}
 
bool Camera2Reader::threadLoop()
{
    usleep(1000);
    return true;
}
 
Camera2Reader::~Camera2Reader()
{
    LOGI("~CameraReader\n");
}
 
void *Camera2Reader::readImage(int chn)
{
    return NULL;
}


void Camera2Reader::WriteFile(Camera2Reader* pThis, AImage *image, const std::string& path)
{
    // static const char *kFileName = "capture";
    int planeCount;
    media_status_t status = AImage_getNumberOfPlanes(image, &planeCount);

    LOGI("Info: getNumberOfPlanes() planeCount = %d", planeCount);
    if (!(status == AMEDIA_OK && planeCount == 1))
    {
        LOGE("Error: getNumberOfPlanes() planeCount = %d", planeCount);
        return;
    }

    uint8_t *data = nullptr;
    int len = 0;
    AImage_getPlaneData(image, 0, &data, &len);

    DIR *dir = opendir(path.c_str());
    if (dir)
    {
        closedir(dir);
    }
    else
    {
        std::string cmd = "mkdir -p ";
        cmd += path;
        system(cmd.c_str());
    }

    std::string fileName = path + pThis->mFileName;

    FILE *file = fopen(fileName.c_str(), "wb");
    if (file && data && len)
    {
        fwrite(data, 1, len, file);
        fclose(file);

        LOGE("Capture: %s", fileName.c_str());
    }
    else
    {
        if (file)
            fclose(file);
    }
}
