﻿#define COMMUNITY_LABS_SDK
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;

namespace VRCSDK2
{
    [InitializeOnLoad]
    public class VRC_SdkSplashScreen : EditorWindow
    {

        static VRC_SdkSplashScreen()
        {
            EditorApplication.update -= DoSplashScreen;
            EditorApplication.update += DoSplashScreen;
        }

        private static void DoSplashScreen()
        {
            EditorApplication.update -= DoSplashScreen;
            if (!EditorPrefs.HasKey("VRCSDK_ShowSplashScreen"))
            {
                EditorPrefs.SetBool("VRCSDK_ShowSplashScreen", true);
            }
            if (EditorPrefs.GetBool("VRCSDK_ShowSplashScreen"))
                OpenSplashScreen();
        }

        private static GUIStyle vrcSdkHeader;
        private static GUIStyle vrcHeaderLearnMoreButton;
        private static Vector2 changeLogScroll;
        [MenuItem("VRChat SDK/Splash Screen", false, 500)]
        public static void OpenSplashScreen()
        {
            GetWindow<VRC_SdkSplashScreen>(true);
        }
        
        public static void Open()
        {
            OpenSplashScreen();
        }

        public void OnEnable()
        {
            titleContent = new GUIContent("VRChat SDK");

            maxSize = new Vector2(400, 500);
            minSize = maxSize;

            vrcSdkHeader = new GUIStyle
            {
                normal =
                    {
#if COMMUNITY_LABS_SDK
                        background = Resources.Load("vrcSdkHeaderWithCommunityLabs") as Texture2D,
#else
                        background = Resources.Load("vrcSdkHeader") as Texture2D,
#endif
                        textColor = Color.white
                    },
                fixedHeight = 200
            };
        }

        public void OnGUI()
        {
            GUILayout.Box("", vrcSdkHeader);
#if COMMUNITY_LABS_SDK
            vrcHeaderLearnMoreButton = EditorStyles.miniButton;
            vrcHeaderLearnMoreButton.normal.textColor = Color.black;
            vrcHeaderLearnMoreButton.fontSize = 12;
            vrcHeaderLearnMoreButton.border = new RectOffset(10, 10, 10, 10);
            Texture2D texture = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Texture2D>("UI/Skin/UISprite.psd");
            vrcHeaderLearnMoreButton.normal.background = texture;
            vrcHeaderLearnMoreButton.active.background = texture;
            if (GUI.Button(new Rect(20, 140, 180, 40), "Please Read", vrcHeaderLearnMoreButton))
            {
                Application.OpenURL(CommunityLabsConstants.COMMUNITY_LABS_DOCUMENTATION_URL);
            }
#endif
            GUILayout.Space(4);
            GUILayout.BeginHorizontal();
            GUI.backgroundColor = Color.gray;
            if (GUILayout.Button("SDK Docs"))
            {
                Application.OpenURL("https://docs.vrchat.com/");
            }
            if (GUILayout.Button("VRChat FAQ"))
            {
                Application.OpenURL("https://www.vrchat.net/developerfaq");
            }
            if (GUILayout.Button("Help Center"))
            {
                Application.OpenURL("https://vrchat.groovehq.com/knowledge_base/categories/technical-issues-vrchat-sdk");
            }
            if(GUILayout.Button("Examples"))
            {
                Application.OpenURL("https://docs.vrchat.com/docs/vrchat-kits");
            }
            GUI.backgroundColor = Color.white;
            GUILayout.EndHorizontal();
            GUILayout.Space(4);

            changeLogScroll = GUILayout.BeginScrollView(changeLogScroll);
            GUILayout.Label(
    @"Changelog:
2018.2.2
Changes
-New triggers added to 'Example - Triggers - 2.unity' included 
    with the SDK
Fixes
-VRCWorld prefab had the UpdateTimeInMS set to 10ms even 
    though the slider value in inspector is capped to 33ms 
    as minimum value. The default is now set to 33ms.

2018.2.1
- Added Bufferone support for actions where it was missing
   --SetLayer
   --SetWebpanelVolume
   --AddHealth
   --AddDamage
   --SetComponentActive
   --SetMaterial
   --ActivateCustomTrigger

 - Added MIDI Driver(opens any available midi - input)
 - Added OSC Driver(input port 9000)
 - Added VRC_MidiNoteIn component for custom triggering note input
 - Added VRC_OscButtonIn component for custom triggering osc button input
 - Added drag and drop support to the trigger references list

 - Fixed ParticleCollision Trigger
 - Fixed some trigger editor issues
 - Fixed enable / disablekinematic action(objectsync)
 - Fixed enable / disablegravity action(objectsync)"
            );
            GUILayout.EndScrollView();

            GUILayout.FlexibleSpace();

            GUILayout.BeginHorizontal();

            GUILayout.FlexibleSpace();
            EditorPrefs.SetBool("VRCSDK_ShowSplashScreen", GUILayout.Toggle(EditorPrefs.GetBool("VRCSDK_ShowSplashScreen"), "Show at Startup"));

            GUILayout.EndHorizontal();
        }

    }
}