﻿using UnityEngine;
using UnityEditor;

namespace RememorySDK
{

    [ExecuteInEditMode]
    public partial class RememorySDKControlPanel : EditorWindow
    {

        public static RememorySDKControlPanel window;
        private readonly bool[] toolbar = new bool[3] { true, true, true };
        private Texture2D headerTexture = null;
        public const int SdkWindowWidth = 700;
        private int showPanel = 0;

        private readonly GUIContent[] toolbarLabels = new GUIContent[3]
        {
            new GUIContent("Account"),
            new GUIContent("Download"),
            new GUIContent("Settings")
        };

        [MenuItem("Rememory SDK/Show Control Panel")]
        private static void ShowWindow()
        {
            window = (RememorySDKControlPanel)EditorWindow.GetWindow(typeof(RememorySDKControlPanel));
            window.titleContent.text = "Rememory SDK";
            window.minSize = new Vector2(SdkWindowWidth, 600);
            window.maxSize = new Vector2(1000, 2000);
            window.Init();
            // window.Show();
        }

        void Init()
        {
            InitAccount();
        }


        void OnGUI()
        {
            if (window == null)
            {
                window = (RememorySDKControlPanel)EditorWindow.GetWindow(typeof(RememorySDKControlPanel));
            }

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();

            if (this.headerTexture == null)
            {
                headerTexture = Resources.Load("rememory_logo_header") as Texture2D;
            }

            if (this.headerTexture != null)
            {
                var textureWidth = (float)this.headerTexture.width;
                var textureHeight = (float)this.headerTexture.height;
                GUIStyle headerTexStyle = new GUIStyle();
                headerTexStyle.fixedHeight = 128;
                headerTexStyle.fixedWidth = 512;
                GUILayout.Box(this.headerTexture, headerTexStyle);
            }

            EditorGUILayout.Space();

            if (Application.isPlaying)
            {
                GUI.enabled = false;
                GUILayout.Space(20);
                EditorGUILayout.LabelField("Unity Application is running ...", LayoutUtil.titleGuiStyle, GUILayout.Width(SdkWindowWidth));
                GUI.enabled = true;
                GUILayout.EndVertical();
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                return;
            }

            showPanel = GUILayout.Toolbar(showPanel, toolbarLabels, toolbar, null, GUILayout.Width(500));

            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            switch (showPanel)
            {
                case 1:
                    ShowDownloadTab();
                    break;
                case 2:
                    ShowSettingsTab();
                    break;
                case 0:
                default:
                    ShowAccountTab();
                    break;
            }
        }
    }
}