#include "ManualLabelToolWidget.h"
#include <QApplication>
#include <QBitmap>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFont>
#include <QFontDatabase>
#include <QPixmap>
#include <QLocale>
#include <QSplashScreen>
#include <QString>
#include <QStringList>
#include <QStyle>
#include <QStyleFactory>
#include <QImageReader>
#include <QMessageBox>
#include <QStandardPaths>
#include <QScreen>
#include <QSurfaceFormat>
#include <QSettings>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include "qgsconfig.h"
#include "qgsapplication.h"
#include "qgssettings.h"
#include "qgsuserprofilemanager.h"

//整数类型主函数(整数类型统计参数个数,字符类型指针数组指向字符串参数)
int main(int argc, char** argv)
{
 
	QApplication app(argc, argv);
    bool myUseGuiFlag = true;
    QStringList preApplicationLogMessages;
    QStringList preApplicationWarningMessages;
	QString customizationfile;
	QString globalsettingsfile;
	QString mySnapshotFileName;
	QString configLocalStorageLocation;
	QString profileName;
    QString missingLastProfile;
    QString translationCode;



	{
        if (globalsettingsfile.isEmpty())
        {
            globalsettingsfile = getenv("QGIS_GLOBAL_SETTINGS_FILE");
        }

        if (globalsettingsfile.isEmpty())
        {
            QStringList startupPaths = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QStringLiteral("qgis_global_settings.ini"));
            if (!startupPaths.isEmpty())
            {
                globalsettingsfile = startupPaths.at(0);
            }
        }

        if (globalsettingsfile.isEmpty())
        {
            QString default_globalsettingsfile = QgsApplication::resolvePkgPath() + "/resources/qgis_global_settings.ini";
            if (QFile::exists(default_globalsettingsfile))
            {
                globalsettingsfile = default_globalsettingsfile;
            }
        }

        if (!globalsettingsfile.isEmpty())
        {
            if (!QgsSettings::setGlobalSettingsPath(globalsettingsfile))
            {
                preApplicationWarningMessages << QObject::tr("Invalid globalsettingsfile path: %1").arg(globalsettingsfile), QStringLiteral("QGIS");
            }
            else
            {
                preApplicationLogMessages << QObject::tr("Successfully loaded globalsettingsfile path: %1").arg(globalsettingsfile), QStringLiteral("QGIS");
            }
        }

        if (configLocalStorageLocation.isEmpty())
        {
            QSettings globalSettings(globalsettingsfile, QSettings::IniFormat);
            if (getenv("QGIS_CUSTOM_CONFIG_PATH"))
            {
                configLocalStorageLocation = getenv("QGIS_CUSTOM_CONFIG_PATH");
            }
            else if (globalSettings.contains(QStringLiteral("core/profilesPath")))
            {
                configLocalStorageLocation = globalSettings.value(QStringLiteral("core/profilesPath"), "").toString();
                QgsDebugMsgLevel(QStringLiteral("Loading profiles path from global config at %1").arg(configLocalStorageLocation), 1);
            }

            // If it is still empty at this point we get it from the standard location.
            if (configLocalStorageLocation.isEmpty())
            {
                configLocalStorageLocation = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
            }
        }

	}


    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    QCoreApplication::setOrganizationName(QgsApplication::QGIS_ORGANIZATION_NAME);
    QCoreApplication::setOrganizationDomain(QgsApplication::QGIS_ORGANIZATION_DOMAIN);
    QCoreApplication::setApplicationName(QgsApplication::QGIS_APPLICATION_NAME);
    QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false);



    {

        QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder(configLocalStorageLocation);
        QgsUserProfileManager manager(rootProfileFolder);

        // If profile name was not explicitly set, use the policy to determine which profile to use
        if (profileName.isEmpty())
        {
            // If no profiles exist, use the default profile
            if (manager.allProfiles().isEmpty())
            {
                profileName = manager.defaultProfileName();
            }
            else
            {
                switch (manager.userProfileSelectionPolicy())
                {
                    // Use the last closed profile (default behavior prior to QGIS 3.32)
                case Qgis::UserProfileSelectionPolicy::LastProfile:
                    profileName = manager.lastProfileName();
                    // If last used profile no longer exists, use the default profile
                    if (!manager.profileExists(profileName))
                    {
                        if (profileName != manager.defaultProfileName())
                        {
                            missingLastProfile = profileName;
                        }
                        profileName = manager.defaultProfileName();
                    }
                    break;

                // Use the default profile
                case Qgis::UserProfileSelectionPolicy::DefaultProfile:
                    profileName = manager.defaultProfileName();
                    break;
                }
            }
        }

        // Calling getProfile() will create the profile if it doesn't exist, and init the QgsSettings
        QgsUserProfile* profile = manager.getProfile(profileName, true);
        QString profileFolder = profile->folder();
        profileName = profile->name();

        QgsApplication::setTranslation(QLocale().name());

        //QgsApplication::setLocale(QLocale());

        QgsApplication::init(profileFolder);
    }
 
	ManualLabelToolWidget labeltoolwidget;
	labeltoolwidget.show();
	//等待用户操作
	//TestALL();
	return app.exec();
}
