#if UNITY_EDITOR
using ConjureOS.MetadataWindow;
using ConjureOS.CustomWindow;
using UnityEditor;
using UnityEngine;

namespace ConjureOS.UploaderWindow
{
    public class ConjureArcadeGameUploaderWindow : EditorWindow
    {
        private const string WindowName = "Arcade Game Uploader";

        // Game Metadata
        private static ConjureArcadeMetadata metadata;
        private ConjureArcadeMetadataValidator metadataValidator;
        private ConjureArcadeUploadProcessor uploadProcessor;

        private void OnEnable()
        {
            metadataValidator = new ConjureArcadeMetadataValidator();
            uploadProcessor = new ConjureArcadeUploadProcessor();
            titleContent = new GUIContent(WindowName);

            if (!metadata)
            {
                OnLoad();
            }
        }

        private static void OnLoad()
        {
            if (!metadata)
            {
                metadata = AssetDatabase.LoadAssetAtPath<ConjureArcadeMetadata>(ConjureArcadeMetadata.MetadataAssetPath);

                // Data exists
                if (metadata)
                {
                    return;
                }

                metadata = CreateInstance<ConjureArcadeMetadata>();

                AssetDatabase.CreateAsset(metadata, ConjureArcadeMetadata.MetadataAssetPath);
                AssetDatabase.Refresh();
            }
        }

        private void OnGUI()
        {
            // Add padding to the window
            int sectionSpace = 20;
            int uniformPadding = ConjureArcadeGUI.Style.UniformPadding;
            RectOffset padding = new RectOffset(uniformPadding, uniformPadding, 0, 0);
            Rect area = new Rect(
                padding.right,
                padding.top,
                position.width - (padding.left),
                position.height - (padding.top + padding.bottom));

            GUILayout.BeginArea(area);

            // Web Server Info section
            GUILayout.Label("WEB SERVER INFO", EditorStyles.boldLabel);
            // TODO Add server connection data / fields

            GUILayout.Space(sectionSpace);

            // Metadata section
            GUILayout.Label("METADATA", EditorStyles.boldLabel);
            GUILayout.Label("Be sure to have valid metadata before uploading the game to the web server.", EditorStyles.wordWrappedLabel);
            
            ShowValidationResultMessage();
            if (GUILayout.Button("Validate Metadata", GUILayout.Width(150)))
            {
                metadataValidator.ValidateMetadata(metadata);
            }

            GUILayout.Space(sectionSpace);

            // Build and Upload section
            GUILayout.Label("BUILD AND UPLOAD", EditorStyles.boldLabel);
            GUILayout.Label("When ready, you can build and upload the game to the web server.", EditorStyles.wordWrappedLabel);
            if (GUILayout.Button("Build & Upload", GUILayout.Width(150)))
            {
                uploadProcessor.BuildAndUpload(metadata, metadataValidator);
                GUIUtility.ExitGUI();
            }

            GUILayout.EndArea();
        }

        private void ShowValidationResultMessage()
        {
            MetadataValidationStateType validationState = metadataValidator.GetValidationStateType();

            switch (validationState)
            {
                case MetadataValidationStateType.NotVerified:
                    // No message is displayed
                    return;

                case MetadataValidationStateType.Validated:
                    // No error, means it's a success
                    GUILayout.Label(ConjureArcadeGUI.Message.MetadataConfirmedMessage, ConjureArcadeGUI.Style.SuccessStyle);
                    GUILayout.Space(3);
                    break;

                case MetadataValidationStateType.Failed:
                    // Errors were detected
                    string plural = (metadataValidator.ErrorCount > 1) ? "s" : "";
                    string formatedMessage =
                        string.Format(ConjureArcadeGUI.Message.MetadataFailedMessage, metadataValidator.ErrorCount, plural) +
                        string.Format(" Please, fix the error{0} in the Game Metadata window ('Arcade > Game Metadata Editor').", plural);
                    GUILayout.Label(formatedMessage, ConjureArcadeGUI.Style.ErrorStyle);
                    GUILayout.Space(3);
                    break;
            }
        }
    }
}
#endif