/*
* -------------------------------------------------------------------------
*  This file is part of the Vision SDK project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* Vision SDK is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
*           http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
 * Description: Vstack op preload from json config.
 * Author: MindX SDK
 * Create: 2023
 * History: NA
 */

#include "OpLoaderVstack.h"

namespace {
    constexpr size_t NHWC_SHAPE_SIZE = 4;
    constexpr size_t HW_HWC_DIMENSION_ZERO = 0;
    constexpr size_t NHWC_DIMENSION_ONE = 1;
}

namespace MxBase {
    OpLoaderVstack::OpLoaderVstack(): OpLoader() {}

    OpLoaderVstack::OpLoaderVstack(std::string opType): OpLoader(opType) {}

    APP_ERROR OpLoaderVstack::CheckOpCustom(std::string inputShape, std::string outputShape)
    {
        auto inputShapeVecs = GetShapeVecs(inputShape);
        auto outputShapeVecs = GetShapeVecs(outputShape);
        if (outputShapeVecs.size() != 1) {
            LogError << "CheckOpCustom: Check Vstack op output vec num is not 1, please check!"
                     << GetErrorInfo(APP_ERR_COMM_INVALID_PARAM);
            return APP_ERR_COMM_INVALID_PARAM;
        }

        auto numOutDim = outputShapeVecs[0].size();
        if ((numOutDim < HW_DIM) || (numOutDim > NHWC_DIM)) {
            LogError << "CheckOpCustom: Check Vstack op output dimension should be 2 or 4, please check!"
                     << GetErrorInfo(APP_ERR_COMM_INVALID_PARAM);
            return APP_ERR_COMM_INVALID_PARAM;
        }

        size_t heightDim = HW_HWC_DIMENSION_ZERO;
        auto shape = inputShapeVecs[0];
        if (shape.size() == NHWC_SHAPE_SIZE) {
            heightDim = NHWC_DIMENSION_ONE;
        }
        int64_t heightSum = 0;
        for (auto inShape: inputShapeVecs) {
            for (size_t i = 0; i < inShape.size(); ++i) {
                if (i == static_cast<uint64_t>(heightDim)) {
                    heightSum += inShape[heightDim];
                } else if (inShape[i] != outputShapeVecs[0][i]) {
                    LogError << "CheckGeneralOpParams: Input Tensors have different shapes(except dimension of height)!"
                             << GetErrorInfo(APP_ERR_COMM_INVALID_PARAM);
                    return APP_ERR_COMM_INVALID_PARAM;
                }
            }
        }
        auto numOutHeight = outputShapeVecs[0][heightDim];
        if (numOutHeight != heightSum) {
            LogError << "CheckOpCustom: Check Vstack op input and output vec channel(" << heightSum << ","
                     << numOutHeight << ") num do not match, please check!"
                     << GetErrorInfo(APP_ERR_COMM_INVALID_PARAM);
            return APP_ERR_COMM_INVALID_PARAM;
        }

        return APP_ERR_OK;
    }

    APP_ERROR OpLoaderVstack::OpCreateParamAttr(aclopAttr *attr, const JsonPtr &jsonPtr, size_t index)
    {
        const size_t maxShapeSize = 4;
        const size_t minShapeSize = 2;
        std::string inputShape;
        auto ret = jsonPtr.GetInputShapeByIndex(index, inputShape);
        if (ret != APP_ERR_OK) {
            LogError << "OpCreateParamAttr: Vstack inputShape is invalid, please check."
                     << GetErrorInfo(ret);
            return ret;
        }
        std::vector<std::vector<int64_t>> shapeVecs = GetShapeVecs(inputShape);
        for (auto shape: shapeVecs) {
            if (shape.size() > maxShapeSize || shape.size() < minShapeSize) {
                LogError << "OpCreateParamAttr: Vstack input shape incorrect, please check the config file."
                         << GetErrorInfo(ret);
                return ret;
            }
        }
        int64_t num = static_cast<int64_t>(shapeVecs.size());
        auto inputShapeVecs = GetShapeVecs(inputShape);
        size_t heightDim = HW_HWC_DIMENSION_ZERO;
        if (inputShapeVecs.size() == NHWC_SHAPE_SIZE) {
            heightDim = NHWC_DIMENSION_ONE;
        }
        int64_t concatDim = static_cast<int64_t>(heightDim);
        LogDebug << "set attr N: " << num << " attr concat_dim: " << concatDim;
        aclopSetAttrInt(attr, "N", num);
        aclopSetAttrInt(attr, "concat_dim", concatDim);
        return APP_ERR_OK;
    }
}