﻿using UnityEditor;
using UnityEngine;

public class AssetBundleEditorWindow : EditorWindow{
	private bool _isProcessing = false;

	#if UNITY_EDITOR_OSX
	private static readonly BuildTarget[] OUTPUT_BUILD_TARGETS = { BuildTarget.iOS };
	#else
	private static readonly BuildTarget[] OUTPUT_BUILD_TARGETS = { BuildTarget.Android, BuildTarget.StandaloneWindows };
	#endif

	[MenuItem("Tools/版本工具/资源包管理工具")]
	public static void ShowWindow(){
		GetWindow<AssetBundleEditorWindow>("资源管理工具");
	}

	void OnGUI(){
		EditorGUI.BeginDisabledGroup(_isProcessing);
		GUILayout.BeginVertical();

		GUILayout.Label("三国项目资源包管理工具", EditorStyles.largeLabel);

		if (EditorGUILayout.Foldout(true, "资源设置")){
			GUI.backgroundColor = Color.gray;

			//是否启用资源包模式
			string text = "禁用资源包模式（仅用于编辑器环境下测试）";
			if (GUILayout.Button("清空本地缓存", GUILayout.Height(30))){
				_BeginProcess();
				EditorApplication.delayCall = ()=>{
					Caching.CleanCache();
					_EndProcess();
				};
			}

			if (GUILayout.Button("清空用户设置", GUILayout.Height(30))){
				_BeginProcess();
				EditorApplication.delayCall = () => {
					PlayerPrefs.DeleteAll();
					_EndProcess();
				};
			}

			if (GUILayout.Button("清空持久化数据", GUILayout.Height(30))){
				_BeginProcess();
				EditorApplication.delayCall = () => {
					EditorUtil.ClearPersistentData();
					_EndProcess();
				};
			}

			if (GUILayout.Button("清空所有的BundleName", GUILayout.Height(30))){
				_BeginProcess();
				EditorApplication.delayCall = () => {
					//AssetBundleNameSetting.CleanAllBundleName();
					_EndProcess();
				};
			}

			if (GUILayout.Button("更新资源的BundleName", GUILayout.Height(30))){
				_BeginProcess();
				EditorApplication.delayCall = () => {
					AssetBundleNameSetting.UpdateAssetBundles();
					_EndProcess();
				};
			}
		}

		GUILayout.Space(10);
		if (EditorGUILayout.Foldout(true, "导出资源包")){
			foreach (var buildTarget in OUTPUT_BUILD_TARGETS) {
				if (GUILayout.Button(PlatformKit.GetPlatformName(buildTarget), GUILayout.Height(30))) {
					var target = buildTarget;
					_BeginProcess();

					EditorApplication.delayCall = () => {
						AssetBundleNameSetting.UpdateAssetBundles();
						AssetBundleBuilder.BuildAssetBundles(target);
						_EndProcess();
					};
				}
			}
		}


		GUILayout.Space(10);
		if (EditorGUILayout.Foldout(true, "生成程序")) {
			foreach (var buildTarget in OUTPUT_BUILD_TARGETS) {
				if (GUILayout.Button(PlatformKit.GetPlatformName(buildTarget), GUILayout.Height(30))) {
					var target = buildTarget;
					_BeginProcess();

					EditorApplication.delayCall = ()=>{
						AssetBundleNameSetting.UpdateAssetBundles();
						AssetBundleBuilder.BuildApplication(target);
						_EndProcess();
					};
				}
			}
		}

		GUILayout.EndVertical();
		EditorGUI.EndDisabledGroup();
	}


	private void _BeginProcess(){
		_isProcessing = true;
		ShowNotification(new GUIContent("正在处理，请稍候..."));
	}

	private void _EndProcess(){
		_isProcessing = false;
		RemoveNotification();
	}

}
