﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.Loaderx
{
    [CustomPropertyDrawer(typeof(BundleProperties), true)]
    class BundlePropertiesDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.LabelField(position, new GUIContent("Properties"), EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            DrawBundleSource(position, property, label);
            EditorGUILayout.Space();
            DrawBundleBase(position, property, label);
            EditorGUILayout.Space();
            DrawBundleReleaseStrategy(position, property, label);
            EditorGUILayout.Space();
            DrawBundleDebug(position, property, label);
            EditorGUI.indentLevel--;
        }

        private void DrawBundleSource(Rect position, SerializedProperty property, GUIContent label)
        {
            var bundleSource = property.FindPropertyRelative("m_BundleSource");
            var bundleSourceVal = (BundleSource) bundleSource.enumValueIndex;
            EditorGUILayout.LabelField("DataSource Settings(数据源设置)", EditorStyles.largeLabel);
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(bundleSource);
            switch (bundleSourceVal)
            {
                case BundleSource.Web:
                    EditorGUILayout.PropertyField(property.FindPropertyRelative("m_BundleSourceWeb"));
                    break;
                case BundleSource.StreamingAssets:
                    EditorGUILayout.PropertyField(property.FindPropertyRelative("m_BundleSourceStreaming"));
                    break;
                case BundleSource.FileDebug:
                    EditorGUILayout.PropertyField(property.FindPropertyRelative("m_BundleSourceFileDebug"));
                    break;
            }

            EditorGUI.indentLevel--;
        }

        private void DrawBundleBase(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUILayout.LabelField("Base Settings(基础设置)", EditorStyles.largeLabel);
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(property.FindPropertyRelative("m_ProjectBasePath"));
            EditorGUILayout.PropertyField(property.FindPropertyRelative("m_BundleCache"));
            EditorGUI.indentLevel--;
        }

        private void DrawBundleReleaseStrategy(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUILayout.LabelField("Resource Release Strategy(资源释放策略)", EditorStyles.largeLabel);
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(property.FindPropertyRelative("m_BundleReleaseStrategy"));
            EditorGUI.indentLevel--;
        }

        private void DrawBundleDebug(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUILayout.LabelField("Debug Settings(调试设置)", EditorStyles.largeLabel);
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(property.FindPropertyRelative("m_BundleDebug"));
            EditorGUI.indentLevel--;
        }
    }
}
#endif