﻿using JLGames.RocketDriver.Actions.Service;
using JLGames.RocketDriver.Actions.Utils;
using JLGames.RocketDriver.Editor.Windows;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace JLGames.RocketDriver.Editor
{
    static class RocketDriverInitialization
    {
        /// <summary>
        /// After the script is compiled
        /// InitializeOnLoadMethod cannot be set with priority
        /// The higher the priority value in DidReloadScripts, the lower the priority
        /// The execution mechanism of the two is the same, the higher the priority value in InitializeOnLoadMethod > DidReloadScripts
        ///
        /// 脚本编译后
        /// InitializeOnLoadMethod 不能带优先级设置
        /// DidReloadScripts中的优先级数值越大，优先级越低
        /// 两者执行机制一致， InitializeOnLoadMethod > DidReloadScripts中的优先级数值越大
        /// </summary>
        [DidReloadScripts(100)]
        private static void OnDidReloadScripts()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode)
                return;

            if (Application.isBatchMode)
                return;

            // Use time judgment to avoid repeated execution
            // 使用时间判断避免重复执行
            if (EditorApplication.timeSinceStartup < 30)
            {
                // Late after the inspector update is complete
                // 迟到检视面板更新完成之后
                EditorApplication.delayCall += () => { WindowsUtil.ShowGettingStarted(true); };
            }
        }

        /// <summary>
        /// 双击资源
        /// </summary>
        /// <param name="id"></param>
        /// <param name="line"></param>
        [OnOpenAsset(0)]
        private static bool OpenAsset(int id, int line)
        {
            var o = EditorUtility.InstanceIDToObject(id);
            var type = o.GetType();
            if (type == typeof(ServiceSettings))
            {
                return true;
            }

            return false;
        }
    }
}