// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qohosdeviceinfo_p.h"
#include <QtCore/private/qohoslogger_p.h>

QT_BEGIN_NAMESPACE

namespace QOhosDeviceInfo {

namespace {

bool isSupportedDeviceType(RecognizedDeviceType recognizedDeviceType)
{
    switch (recognizedDeviceType) {
    case RecognizedDeviceType::_2in1:
    case RecognizedDeviceType::tablet:
        return true;
    case RecognizedDeviceType::phone:
        return false;
    }
    return false;
}

}

static QMap<Type, QVariant> s_deviceInfo = {};

void init(QMap<Type, QVariant> devinfo)
{
    s_deviceInfo = devinfo;
}

QVariant getProperty(Type prop)
{
    auto result = s_deviceInfo.value(prop);
    if (!result.isValid())
        qOhosWarning(QtForOhos)
                << "QOhosDeviceInfo::getProperty cannot obtain value from DeviceInfo map";
    return result;
}

QOhosOptional<RecognizedDeviceType> tryGetRecognizedDeviceType()
{
    static const QMap<QString, RecognizedDeviceType> deviceTypeNameMapping = {
        {"2in1", RecognizedDeviceType::_2in1},
        {"tablet", RecognizedDeviceType::tablet},
        {"phone", RecognizedDeviceType::phone},
    };

    auto deviceTypeName = getProperty(Type::deviceType).toString();
    auto deviceTypeIt = deviceTypeNameMapping.constFind(deviceTypeName);
    auto deviceType = deviceTypeIt != deviceTypeNameMapping.constEnd()
        ? makeQOhosOptional(deviceTypeIt.value())
        : makeEmptyQOhosOptional();

    return deviceType;
}

Q_CORE_EXPORT bool isTablet()
{
    return tryGetRecognizedDeviceType() == QOhosDeviceInfo::RecognizedDeviceType::tablet;
}

Q_CORE_EXPORT bool is2in1()
{
    return tryGetRecognizedDeviceType() == QOhosDeviceInfo::RecognizedDeviceType::_2in1;
}

Q_CORE_EXPORT bool isPhone()
{
    return tryGetRecognizedDeviceType() == RecognizedDeviceType::phone;
}

Q_CORE_EXPORT int sdkApiVersion()
{
    return getProperty(Type::sdkApiVersion).toInt();
}

Q_CORE_EXPORT bool isCurrentDeviceSupported()
{
    auto deviceType = tryGetRecognizedDeviceType();
    return deviceType.hasValue() && isSupportedDeviceType(deviceType.value());
}

}

QT_END_NAMESPACE
