#region Header

// Copyright (c) 2026 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.

#endregion

using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR
namespace BlackBox
{
    public class BlackBoxRestartWindow : EditorWindow
    {
        private static readonly Vector2 WINDOW_SIZE = new(420, 300);
        private const string BLACKBOX_LOGO_ASSET = "Assets/Plugins/BlackBoxUnitySDK/BlackBox/Resources/blackbox_logo.png";
        private Texture2D _logoTexture;

        public static void Open()
        {
            var window = GetWindow<BlackBoxRestartWindow>(true, "BlackBox Helper Setup Complete");
            window.minSize = WINDOW_SIZE;
            window.maxSize = WINDOW_SIZE;
            window.Show();
        }

        private void OnEnable()
        {
            _logoTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(BLACKBOX_LOGO_ASSET);
        }

        private void OnGUI()
        {
            GUILayout.Space(16);

            DrawLogo();
            GUILayout.Space(12);

            DrawHeader();
            GUILayout.Space(12);

            DrawMessage();
            GUILayout.FlexibleSpace();

            DrawButtons();
            GUILayout.Space(12);
        }

        private void DrawLogo()
        {
            if (_logoTexture == null)
            {
                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.FlexibleSpace();
                    GUILayout.FlexibleSpace();
                }
                return;
            }

            float maxHeight = 80f;
            float scale = Mathf.Min(1f, maxHeight / _logoTexture.height);
            float drawWidth = _logoTexture.width * scale;
            float drawHeight = _logoTexture.height * scale;

            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.FlexibleSpace();
                GUILayout.Label(_logoTexture, GUILayout.Width(drawWidth), GUILayout.Height(drawHeight));
                GUILayout.FlexibleSpace();
            }
        }

        private void DrawHeader()
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.FlexibleSpace();
                GUILayout.Label("Setup Complete", EditorStyles.boldLabel);
                GUILayout.FlexibleSpace();
            }
        }

        private void DrawMessage()
        {
            EditorGUILayout.LabelField("BlackBox Helper & Issue Reporter have been downloaded successfully.", EditorStyles.centeredGreyMiniLabel);
            EditorGUILayout.Space(1);

            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.FlexibleSpace();
                var centeredLabel = new GUIStyle(EditorStyles.wordWrappedLabel)
                {
                    alignment = TextAnchor.MiddleCenter
                };
                EditorGUILayout.LabelField("Please restart the Unity Editor to complete the setup!",
                    centeredLabel,
                    GUILayout.MaxWidth(460)
                );
                GUILayout.FlexibleSpace();
            }

            EditorGUILayout.Space(3);

            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.FlexibleSpace();
                var centeredLabel = new GUIStyle(EditorStyles.wordWrappedLabel)
                {
                    alignment = TextAnchor.MiddleCenter,
                    fontStyle = FontStyle.Bold
                };
                EditorGUILayout.LabelField("Go to \"Tools → BlackBox → Properties\" to setup your configuration.",
                    centeredLabel,
                    GUILayout.MaxWidth(460)
                );
                GUILayout.FlexibleSpace();
            }
        }

        private void DrawButtons()
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.FlexibleSpace();

                if (GUILayout.Button("Restart Now", GUILayout.Height(30), GUILayout.Width(110)))
                {
                    Close();
                    EditorApplication.OpenProject(Application.dataPath.Replace("/Assets", ""));
                }

                GUILayout.Space(12);

                if (GUILayout.Button("Later", GUILayout.Height(30), GUILayout.Width(100)))
                {
                    Close();
                }

                GUILayout.FlexibleSpace();
            }
        }
    }
}
#endif