/*
 * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
 *
 * SPDX-License-Identifier: Apache-2.0 OR MIT
 *
 */

#pragma once

#include <AtomToolsFramework/Communication/LocalServer.h>
#include <AtomToolsFramework/Communication/LocalSocket.h>
#include <AtomToolsFramework/Window/AtomToolsMainWindowNotificationBus.h>

#include <AzCore/Component/Entity.h>
#include <AzCore/Component/TickBus.h>
#include <AzCore/UserSettings/UserSettingsProvider.h>

#include <AzFramework/Application/Application.h>

#include <AzQtComponents/Application/AzQtApplication.h>
#include <AzQtComponents/Components/StyleManager.h>

#include <AzToolsFramework/API/AssetDatabaseBus.h>
#include <AzToolsFramework/API/EditorPythonConsoleBus.h>
#include <AzToolsFramework/Logger/TraceLogger.h>

#include <QTimer>

namespace AtomToolsFramework
{
    //! Base class for Atom tools to inherit from
    class AtomToolsApplication
        : public AzFramework::Application
        , public AzQtComponents::AzQtApplication
        , protected AzToolsFramework::AssetDatabase::AssetDatabaseRequestsBus::Handler
        , protected AzToolsFramework::EditorPythonConsoleNotificationBus::Handler
        , protected AZ::UserSettingsOwnerRequestBus::Handler
        , protected AtomToolsMainWindowNotificationBus::Handler
    {
    public:
        AZ_TYPE_INFO(AtomTools::AtomToolsApplication, "{A0DF25BA-6F74-4F11-9F85-0F99278D5986}");
        AZ_DISABLE_COPY_MOVE(AtomToolsApplication);

        using Base = AzFramework::Application;

        AtomToolsApplication(const char* targetName, int* argc, char*** argv);
        ~AtomToolsApplication();

        virtual bool LaunchLocalServer();

        // AzFramework::Application overrides...
        void CreateReflectionManager() override;
        void Reflect(AZ::ReflectContext* context) override;
        void RegisterCoreComponents() override;
        AZ::ComponentTypeList GetRequiredSystemComponents() const override;
        void CreateStaticModules(AZStd::vector<AZ::Module*>& outModules) override;
        const char* GetCurrentConfigurationName() const override;
        void StartCommon(AZ::Entity* systemEntity) override;
        void Tick() override;
        void Destroy() override;

    protected:
        // AtomsToolMainWindowNotificationBus::Handler overrides...
        void OnMainWindowClosing() override;

        // AssetDatabaseRequestsBus::Handler overrides...
        bool GetAssetDatabaseLocation(AZStd::string& result) override;

        // AZ::ComponentApplication overrides...
        void QueryApplicationType(AZ::ApplicationTypeQuery& appType) const override;

        // AZ::UserSettingsOwnerRequestBus::Handler overrides...
        void SaveSettings() override;

        // EditorPythonConsoleNotificationBus::Handler overrides...
        void OnTraceMessage(AZStd::string_view message) override;
        void OnErrorMessage(AZStd::string_view message) override;
        void OnExceptionMessage(AZStd::string_view message) override;

        //! List of filters for assets that need to be pre-built to run the application
        virtual AZStd::vector<AZStd::string> GetCriticalAssetFilters() const;

        virtual void LoadSettings();
        virtual void UnloadSettings();
        virtual void ConnectToAssetProcessor();
        virtual void CompileCriticalAssets();
        virtual void ProcessCommandLine(const AZ::CommandLine& commandLine);

        static void PyIdleWaitFrames(uint32_t frames);
        static void PyExit();

        AzToolsFramework::TraceLogger m_traceLogger;

        AZStd::unique_ptr<AzQtComponents::StyleManager> m_styleManager;

        //! Local user settings are used to store asset browser tree expansion state
        AZ::UserSettingsProvider m_localUserSettings;

        //! Are local settings loaded
        bool m_activatedLocalUserSettings = false;

        QTimer m_timer;

        AtomToolsFramework::LocalSocket m_socket;
        AtomToolsFramework::LocalServer m_server;

        const AZStd::string m_targetName;
        const AZ::Crc32 m_toolId = {};
    };
} // namespace AtomToolsFramework
