
// Workaround to access protected functions
#ifndef protected
#  define protected public
#endif

// default includes
#include <shiboken.h>
#include <sbkpep.h>
#include <sbkpepbuffer.h>

#define PYSIDE6_COMOPT_COMPRESS 1

#ifndef QT_NO_VERSION_TAGGING
#  define QT_NO_VERSION_TAGGING
#endif
#include <QtCore/QDebug>
#include <pysideqenum.h>
#include <pysideutils.h>
#include <feature_select.h>
QT_WARNING_DISABLE_DEPRECATED


// module include
#include "akonadicore_python.h"

// main header
#include "akonadi_exception_wrapper.h"

// Argument includes
#include <QString>
#include <exceptionbase.h>
#include <qbytearray.h>

#include <cctype>
#include <cstring>
#include <iterator>
#include <type_traits>
#include <typeinfo>

using namespace Akonadi;

extern "C" {
static PyTypeObject *Sbk_Akonadi_Exception_TypeF(void)
{
    return SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX].type;
}
} // extern "C"

// Native ---------------------------------------------------------

void ExceptionWrapper::resetPyMethodCache()
{
    std::fill(m_PyMethodCache.begin(), m_PyMethodCache.end(), nullptr);
}

ExceptionWrapper::ExceptionWrapper(const ::QByteArray & what) : Akonadi::Exception(what)
{
}

ExceptionWrapper::ExceptionWrapper(const ::QString & what) : Akonadi::Exception(what)
{
}

ExceptionWrapper::ExceptionWrapper(const char * what) : Akonadi::Exception(what)
{
}

::QByteArray ExceptionWrapper::type() const
{
    static const char *funcName = "type";
    static PyObject *nameCache[2] = {};
    Shiboken::GilState gil(false);
    Shiboken::AutoDecRef pyOverride(Sbk_GetPyOverride(this, Sbk_Akonadi_Exception_TypeF(), gil, funcName, m_PyMethodCache[0], nameCache));
    if (pyOverride.isNull()) {
        return this->::Akonadi::Exception::type();
    }
    return sbk_o_type("Exception", funcName, gil, pyOverride);
}

::QByteArray ExceptionWrapper::sbk_o_type([[maybe_unused]] const char *ownerClassName, [[maybe_unused]] const char *funcName, [[maybe_unused]] Shiboken::GilState &gil, [[maybe_unused]] const Shiboken::AutoDecRef &pyOverride)
{
#if defined(PYPY_VERSION)
    Shiboken::AutoDecRef pyArgs(PyTuple_New(0));
#endif

#if !defined(PYPY_VERSION)
    Shiboken::AutoDecRef pyResult(PyObject_CallNoArgs(pyOverride));
#else
    Shiboken::AutoDecRef pyResult(PyObject_Call(pyOverride, pyArgs, nullptr));
#endif
    if (pyResult.isNull()) {
        // An error happened in python code!
        Shiboken::Errors::storePythonOverrideErrorOrPrint(ownerClassName, funcName);
        return {};
    }
    // Check return type
    Shiboken::Conversions::PythonToCppConversion pythonToCpp =
        Shiboken::Conversions::pythonToCppValueConversion(Shiboken::Module::get(SbkPySide6_QtCoreTypeStructs[SBK_QByteArray_IDX]), pyResult);
    if (!pythonToCpp) {
        Shiboken::Warnings::warnInvalidReturnValue(ownerClassName, funcName, PepType_GetFullyQualifiedNameStr(Shiboken::SbkType< QByteArray >()), Py_TYPE(pyResult)->tp_name);
        return {};
    }
    ::QByteArray cppResult;
    pythonToCpp(pyResult, &cppResult);
    return cppResult;
}

const char * ExceptionWrapper::what() const noexcept
{
    static const char *funcName = "what";
    static PyObject *nameCache[2] = {};
    Shiboken::GilState gil(false);
    Shiboken::AutoDecRef pyOverride(Sbk_GetPyOverride(this, Sbk_Akonadi_Exception_TypeF(), gil, funcName, m_PyMethodCache[1], nameCache));
    if (pyOverride.isNull()) {
        return this->::Akonadi::Exception::what();
    }
    return sbk_o_what("Exception", funcName, gil, pyOverride);
}

const char * ExceptionWrapper::sbk_o_what([[maybe_unused]] const char *ownerClassName, [[maybe_unused]] const char *funcName, [[maybe_unused]] Shiboken::GilState &gil, [[maybe_unused]] const Shiboken::AutoDecRef &pyOverride) noexcept
{
#if defined(PYPY_VERSION)
    Shiboken::AutoDecRef pyArgs(PyTuple_New(0));
#endif

#if !defined(PYPY_VERSION)
    Shiboken::AutoDecRef pyResult(PyObject_CallNoArgs(pyOverride));
#else
    Shiboken::AutoDecRef pyResult(PyObject_Call(pyOverride, pyArgs, nullptr));
#endif
    if (pyResult.isNull()) {
        // An error happened in python code!
        Shiboken::Errors::storePythonOverrideErrorOrPrint(ownerClassName, funcName);
        return nullptr;
    }
    // Check return type
    Shiboken::Conversions::PythonToCppConversion pythonToCpp =
        Shiboken::Conversions::pythonToCppConversion(Shiboken::Conversions::PrimitiveTypeConverter<const char *>(), pyResult);
    if (!pythonToCpp) {
        Shiboken::Warnings::warnInvalidReturnValue(ownerClassName, funcName, PepType_GetFullyQualifiedNameStr(Shiboken::SbkType< char >()), Py_TYPE(pyResult)->tp_name);
        return nullptr;
    }
    const char* cppResult;
    pythonToCpp(pyResult, &cppResult);
    return cppResult;
}

ExceptionWrapper::~ExceptionWrapper()
{
    auto *wrapper = Shiboken::BindingManager::instance().retrieveWrapper(this, Sbk_Akonadi_Exception_TypeF());
    Shiboken::Object::destroy(wrapper, this);
}

// Target ---------------------------------------------------------

extern "C" {
static int
Sbk_Akonadi_Exception_Init(PyObject *self, PyObject *args, [[maybe_unused]] PyObject *kwds)
{
    auto *sbkSelf = reinterpret_cast<SbkObject *>(self);
    PySide::Feature::Select(self);
    if (Shiboken::Object::isUserType(self) && !Shiboken::ObjectType::canCallConstructor(self->ob_type, Shiboken::SbkType< ::Akonadi::Exception >()))
        return -1;

    ExceptionWrapper *cptr{};
    Shiboken::AutoDecRef errInfo{};
    Shiboken::PythonContextMarker pcm;
    int overloadId = -1;
    [[maybe_unused]] Shiboken::Conversions::PythonToCppConversion pythonToCpp[1];
    const Py_ssize_t numArgs = PyTuple_Size(args);
    SBK_UNUSED(numArgs)
    PyObject *pyArgs[] = {nullptr};

    // invalid argument lengths

    if (PyArg_UnpackTuple(args, "Exception", 1, 1, &(pyArgs[0])) == 0)
        return -1;


    // Overloaded function decisor
    // 0: Exception::Exception(const QByteArray&)
    // 1: Exception::Exception(const QString&)
    // 2: Exception::Exception(const char*)
    if (numArgs == 1
        && (pythonToCpp[0] = Shiboken::Conversions::pythonToCppConversion(Shiboken::Conversions::PrimitiveTypeConverter<const char *>(), (pyArgs[0])))) {
        overloadId = 2; // Exception::Exception(const char*)
    } else if (numArgs == 1
        && (pythonToCpp[0] = Shiboken::Conversions::pythonToCppConversion(SbkPySide6_QtCoreTypeConverters[SBK_QString_IDX], (pyArgs[0])))) {
        overloadId = 1; // Exception::Exception(const QString&)
    } else if (numArgs == 1
        && (pythonToCpp[0] = Shiboken::Conversions::pythonToCppReferenceConversion(Shiboken::Module::get(SbkPySide6_QtCoreTypeStructs[SBK_QByteArray_IDX]), (pyArgs[0])))) {
        overloadId = 0; // Exception::Exception(const QByteArray&)
    }

    // Function signature not found.
    if (overloadId == -1)
        return Shiboken::returnWrongArguments_MinusOne(args, "__init__", SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX]);


    // PyMI support
    Shiboken::callInheritedInit(self, args, kwds, SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX]);
    if (Shiboken::Errors::occurred() != nullptr)
        return -1;

    // Call function/method
    switch (overloadId) {
        case 0: // Exception(const QByteArray &what)
        {
            if (!Shiboken::Object::isValid(pyArgs[0]))
                return -1;
            ::QByteArray cppArg0_local;
            ::QByteArray *cppArg0 = &cppArg0_local;
            if (pythonToCpp[0].isValue())
                pythonToCpp[0](pyArgs[0], &cppArg0_local);
            else
                pythonToCpp[0](pyArgs[0], &cppArg0);


            if (Shiboken::Errors::occurred() == nullptr) {
                // Exception::Exception(const QByteArray&)
                cptr = new ExceptionWrapper(*cppArg0);
            }
            break;
        }
        case 1: // Exception(const QString &what)
        {
            ::QString cppArg0;
            pythonToCpp[0](pyArgs[0], &cppArg0);

            if (Shiboken::Errors::occurred() == nullptr) {
                // Exception::Exception(const QString&)
                cptr = new ExceptionWrapper(cppArg0);
            }
            break;
        }
        case 2: // Exception(const char *what)
        {
            const char* cppArg0;
            pythonToCpp[0](pyArgs[0], &cppArg0);

            if (Shiboken::Errors::occurred() == nullptr) {
                // Exception::Exception(const char*)
                cptr = new ExceptionWrapper(cppArg0);
            }
            break;
        }
    }

    if (Shiboken::Errors::occurred() != nullptr || !Shiboken::Object::setCppPointer(sbkSelf, Shiboken::SbkType< Akonadi::Exception >(), cptr)) {
        delete cptr;
        return -1;
    }
    if (cptr == nullptr)
        return Shiboken::returnWrongArguments_MinusOne(args, "__init__", SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX]);

    Shiboken::Object::setValidCpp(sbkSelf, true);
    Shiboken::Object::setHasCppWrapper(sbkSelf, true);
    Shiboken::BindingManager::instance().registerWrapper(sbkSelf, cptr);


    return 1;
}

static PyObject *Sbk_Akonadi_ExceptionFunc_type(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    [[maybe_unused]] auto *cppSelf = reinterpret_cast< ::Akonadi::Exception *>(Shiboken::Conversions::cppPointer(Shiboken::Module::get(SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX]), reinterpret_cast<SbkObject *>(self)));
    PyObject *pyResult{};
    Shiboken::PythonContextMarker pcm;

    // Call function/method
    {

        // Exception::type()const->QByteArray
        QByteArray cppResult = Shiboken::Object::hasCppWrapper(reinterpret_cast<SbkObject *>(self))
            ? const_cast<const ::Akonadi::Exception *>(cppSelf)->::Akonadi::Exception::type()
            : const_cast<const ::Akonadi::Exception *>(cppSelf)->type();
        pyResult = Shiboken::Conversions::copyToPython(Shiboken::Module::get(SbkPySide6_QtCoreTypeStructs[SBK_QByteArray_IDX]), &cppResult);
    }

    return Sbk_ReturnFromPython_Result(pyResult);
}

static PyObject *Sbk_Akonadi_ExceptionFunc_what(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    [[maybe_unused]] auto *cppSelf = reinterpret_cast< ::Akonadi::Exception *>(Shiboken::Conversions::cppPointer(Shiboken::Module::get(SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX]), reinterpret_cast<SbkObject *>(self)));
    PyObject *pyResult{};
    Shiboken::PythonContextMarker pcm;

    // Call function/method
    {

        // Exception::what()const->const char*
        const char * cppResult = Shiboken::Object::hasCppWrapper(reinterpret_cast<SbkObject *>(self))
            ? const_cast<const ::Akonadi::Exception *>(cppSelf)->::Akonadi::Exception::what()
            : const_cast<const ::Akonadi::Exception *>(cppSelf)->what();
        pyResult = Shiboken::Conversions::copyToPython(Shiboken::Conversions::PrimitiveTypeConverter<const char *>(), cppResult);
    }

    return Sbk_ReturnFromPython_Result(pyResult);
}


static const char *Sbk_Akonadi_Exception_PropertyStrings[] = {
    nullptr // Sentinel
};

static PyMethodDef Sbk_Akonadi_Exception_methods[] = {
    {"type", reinterpret_cast<PyCFunction>(Sbk_Akonadi_ExceptionFunc_type), METH_NOARGS, nullptr},
    {"what", reinterpret_cast<PyCFunction>(Sbk_Akonadi_ExceptionFunc_what), METH_NOARGS, nullptr},
    {nullptr, nullptr, 0, nullptr} // Sentinel
};

static int Sbk_Akonadi_Exception_setattro(PyObject *self, PyObject *name, PyObject *value)
{
    PySide::Feature::Select(self);
    if (value != nullptr && PyCallable_Check(value) != 0) {
        auto plain_inst = reinterpret_cast< ::Akonadi::Exception *>(Shiboken::Conversions::cppPointer(Shiboken::Module::get(SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX]), reinterpret_cast<SbkObject *>(self)));
        auto *inst = dynamic_cast<ExceptionWrapper *>(plain_inst);
        if (inst != nullptr)
            inst->resetPyMethodCache();
    }
    return PyObject_GenericSetAttr(self, name, value);
}

} // extern "C"

static int Sbk_Akonadi_Exception_traverse(PyObject *self, visitproc visit, void *arg)
{
    auto traverseProc = reinterpret_cast<traverseproc>(PyType_GetSlot(SbkObject_TypeF(), Py_tp_traverse));
    ;
    return traverseProc(self, visit, arg);
}
static int Sbk_Akonadi_Exception_clear(PyObject *self)
{
    auto clearProc = reinterpret_cast<inquiry>(PyType_GetSlot(SbkObject_TypeF(), Py_tp_clear));
    ;
    return clearProc(self);
}

// Class Definition -----------------------------------------------
extern "C" {

static PyType_Slot Sbk_Akonadi_Exception_slots[] = {
    {Py_tp_base,        nullptr}, // inserted by introduceWrapperType
    {Py_tp_dealloc,     reinterpret_cast<void *>(&SbkDeallocWrapper)},
    {Py_tp_setattro,    reinterpret_cast<void *>(Sbk_Akonadi_Exception_setattro)},
    {Py_tp_traverse,    reinterpret_cast<void *>(Sbk_Akonadi_Exception_traverse)},
    {Py_tp_clear,       reinterpret_cast<void *>(Sbk_Akonadi_Exception_clear)},
    {Py_tp_methods,     reinterpret_cast<void *>(Sbk_Akonadi_Exception_methods)},
    {Py_tp_init,        reinterpret_cast<void *>(Sbk_Akonadi_Exception_Init)},
    {Py_tp_new,         reinterpret_cast<void *>(SbkObject_tp_new)},
    {0, nullptr}
};
static PyType_Spec Sbk_Akonadi_Exception_spec = {
    "1:AkonadiCore.Akonadi.Exception",
    sizeof(SbkObject),
    0,
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC,
    Sbk_Akonadi_Exception_slots
};

} //extern "C"

// Type conversion functions.

// Python to C++ pointer conversion - returns the C++ object of the Python wrapper (keeps object identity).
static void Exception_PythonToCpp_Exception_PTR(PyObject *pyIn, void *cppOut)
{
    Shiboken::Conversions::pythonToCppPointer(Sbk_Akonadi_Exception_TypeF(), pyIn, cppOut);
}
static PythonToCppFunc is_Exception_PythonToCpp_Exception_PTR_Convertible(PyObject *pyIn)
{
    if (pyIn == Py_None)
        return Shiboken::Conversions::nonePythonToCppNullPtr;
    if (PyObject_TypeCheck(pyIn, Sbk_Akonadi_Exception_TypeF()))
        return Exception_PythonToCpp_Exception_PTR;
    return {};
}

// C++ to Python pointer conversion - tries to find the Python wrapper for the C++ object (keeps object identity).
static PyObject *Exception_PTR_CppToPython_Exception(const void *cppIn)
{
    auto *pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppIn, Sbk_Akonadi_Exception_TypeF()));
    if (pyOut) {
        Py_INCREF(pyOut);
        return pyOut;
    }
    auto *tCppIn = reinterpret_cast<const ::Akonadi::Exception *>(cppIn);
    const char *typeName = typeid(*tCppIn).name();
    return Shiboken::Object::newObjectForPointer(Sbk_Akonadi_Exception_TypeF(), const_cast<void *>(cppIn), false, typeName);

}

// The signatures string for the functions.
// Multiple signatures have their index "n:" in front.
#if PYSIDE6_COMOPT_COMPRESS == 0
static const char *Akonadi_Exception_SignatureStrings[] = {
    "2:AkonadiCore.Akonadi.Exception(self,what:typing.Union[PySide6.QtCore.QByteArray, char*])",
    "1:AkonadiCore.Akonadi.Exception(self,what:QString)",
    "0:AkonadiCore.Akonadi.Exception(self,what:char*)",
    "AkonadiCore.Akonadi.Exception.type(self)->PySide6.QtCore.QByteArray",
    "AkonadiCore.Akonadi.Exception.what(self)->char*",
    nullptr}; // Sentinel
#else
static constexpr size_t Akonadi_Exception_SignatureByteSize = 131;
static constexpr uint8_t Akonadi_Exception_SignatureBytes[131] = {
    0x78, 0xda, 0x33, 0xb2, 0x72, 0xcc, 0xce, 0xcf, 0x4b, 0x4c, 0xc9, 0x74, 0xce, 0x2f, 0x4a, 0xd5,
    0x83, 0xb2, 0xf5, 0x5c, 0x2b, 0x92, 0x53, 0x0b, 0x4a, 0x32, 0xf3, 0xf3, 0x34, 0x8a, 0x53, 0x73,
    0xd2, 0x74, 0xca, 0x33, 0x12, 0x4b, 0xac, 0x4a, 0x2a, 0x0b, 0x32, 0xf3, 0xd2, 0xf5, 0x42, 0xf3,
    0x80, 0xc2, 0xd1, 0x01, 0x95, 0xc1, 0x99, 0x29, 0xa9, 0x66, 0x7a, 0x81, 0x25, 0x60, 0x7d, 0x81,
    0x4e, 0x95, 0x25, 0xa9, 0x8e, 0x45, 0x45, 0x89, 0x95, 0x3a, 0x0a, 0xc9, 0x19, 0x89, 0x45, 0x5a,
    0xb1, 0x9a, 0x5c, 0x86, 0x44, 0x9b, 0x1c, 0x18, 0x5c, 0x52, 0x04, 0x34, 0x5a, 0x93, 0xcb, 0x80,
    0x68, 0x2d, 0x60, 0x4b, 0x34, 0xb9, 0xf0, 0x2a, 0xd7, 0x03, 0xba, 0x38, 0x15, 0xac, 0x47, 0x53,
    0xd7, 0x0e, 0xa7, 0x83, 0x09, 0x98, 0x01, 0xb2, 0x0d, 0x66, 0x06, 0xd8, 0x52, 0x2e, 0x00, 0xfa,
    0x5f, 0x6d, 0x75
};
#endif

PyTypeObject *init_Akonadi_Exception(PyObject *enclosingClass)
{
    auto &typeStruct = SbkAkonadiCoreTypeStructs[SBK_Akonadi_Exception_IDX];
    if (typeStruct.type != nullptr)
        return typeStruct.type;

    auto *base1 = SbkObject_TypeF();
    Shiboken::AutoDecRef Sbk_Akonadi_Exception_Type_bases(PyTuple_Pack(1, base1));

    PyTypeObject *pyType = Shiboken::ObjectType::introduceWrapperType(
        enclosingClass,
        "Exception",
        "Akonadi::Exception*",
        &Sbk_Akonadi_Exception_spec,
        &Shiboken::callCppDestructor< Akonadi::Exception >,
        Sbk_Akonadi_Exception_Type_bases.object(),
        Shiboken::ObjectType::WrapperFlags::InnerClass);
#if PYSIDE6_COMOPT_COMPRESS == 0
    InitSignatureStrings(pyType, Akonadi_Exception_SignatureStrings);
#else
    InitSignatureBytes(pyType, Akonadi_Exception_SignatureBytes, Akonadi_Exception_SignatureByteSize);
#endif
    SbkObjectType_SetPropertyStrings(pyType, Sbk_Akonadi_Exception_PropertyStrings);
    typeStruct.type = pyType;

    // Register Converter
    SbkConverter *converter = Shiboken::Conversions::createConverter(pyType,
        Exception_PythonToCpp_Exception_PTR,
        is_Exception_PythonToCpp_Exception_PTR_Convertible,
        Exception_PTR_CppToPython_Exception);

    Shiboken::Conversions::registerConverterName(converter, "Akonadi::Exception");
    Shiboken::Conversions::registerConverterName(converter, "Akonadi::Exception*");
    Shiboken::Conversions::registerConverterName(converter, "Akonadi::Exception&");
    Shiboken::Conversions::registerConverterAlias(converter, "Exception");
    Shiboken::Conversions::registerConverterAlias(converter, "Exception*");
    Shiboken::Conversions::registerConverterAlias(converter, "Exception&");
    Shiboken::Conversions::registerConverterName(converter, typeid(::Akonadi::Exception).name());
    Shiboken::Conversions::registerConverterName(converter, typeid(ExceptionWrapper).name());

    qRegisterMetaType< ::Akonadi::Exception *>();

    return pyType;
}
