using iHuman.UnitySz.Framework.runtimehelper;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace runtimeeditor { 
	public class FolderDebugger : RuntimeDebuggerWindow
	{
		string currentFolder;
		string currentTip = "";
		List<string> fileLines = new();
		bool watchingFile = false;
		LogScrollView logScorllView = new LogScrollView();
		public override void OnInit()
		{
			this.drawFunc += Draw;
		}
		public override void OnOpen()
		{
			currentFolder = Application.persistentDataPath;
		}
		public void Draw(int id)
		{
			if (watchingFile)
			{
				if (GUILayout.Button("返回")) {
					watchingFile = false;
				}
				logScorllView.Draw(fileLines,false);
			}
			else { 
				if (GUILayout.Button("[Parent Folder]",this.Width(1))) {
					if (Path.GetFullPath(currentFolder) != Path.GetFullPath(Application.persistentDataPath)) {
						currentFolder = Path.GetDirectoryName(currentFolder);
					}
				}
				if (GUILayout.Button("[Clear Folder]",this.Width(1))) {
                    //if (Path.GetFullPath(currentFolder) != Path.GetFullPath(Application.persistentDataPath)) {
                    //                   Directory.Delete(currentFolder,true);
                    //	currentFolder = Path.GetDirectoryName(currentFolder);
                    //}
                    string[] subFiles = Directory.GetFiles(currentFolder);
                    string[] subDirs = Directory.GetDirectories(currentFolder);
                    foreach (string file in subFiles) { 
                        File.Delete(file);
                    }
                    foreach (string dir in subDirs) { 
                        Directory.Delete(dir,true);
                    }
				}
				GUILayout.Label($"[Current]:{currentFolder}");
				GUILayout.Label($"[Tip]:{currentTip}");
				string[] folders = Directory.GetDirectories(currentFolder);
				string[] files = Directory.GetFiles(currentFolder);
				foreach (string folder in folders) {
					string name = Path.GetFileName(folder);
					GUILayout.BeginHorizontal();
					GUILayout.Label(name,this.Width(0.5f));
					GUILayout.Label("[Folder]",this.Width(0.1f));
					if (GUILayout.Button("Open",this.Width(0.1f))) {
						currentFolder = folder;
					}
					if (GUILayout.Button("Delete",this.Width(0.1f))) {
						Directory.Delete(folder,true);
					}
                    GUILayout.EndHorizontal();
				}
				foreach (string file in files)
				{
					GUILayout.BeginHorizontal(Width(1f));
					string name = Path.GetFileName(file);
					GUILayout.Label(name,this.Width(0.5f));
					GUILayout.Label("[File]",this.Width(0.1f));
					if (GUILayout.Button("Delete",this.Width(0.1f))) {
						File.Delete(file);
					}
					if (GUILayout.Button("Watch",this.Width(0.1f))) {
						try
						{
							string all = File.ReadAllText(file);
							fileLines = new List<string>(all.Split('\n'));
							watchingFile = true;
						}
						catch (System.Exception e) {
							Debug.Log(e.Message);
						}
					}
                    if (GUILayout.Button("md5",this.Width(0.1f))){
                        try{
                            currentTip = Md5Util.GetHashCodeOfFile(file);
                        }
						catch (System.Exception e) {
							Debug.Log(e.Message);
						}
                    }
                    if (GUILayout.Button("size", this.Width(0.1f))) {
                        try{
                            long len = new System.IO.FileInfo(file).Length;
                            currentTip = $"size:{len}";
                        }
						catch (System.Exception e) {
							Debug.Log(e.Message);
						}
                    }
					GUILayout.EndHorizontal();
				}
			}
		}
	}
}
