using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.AddressableAssets;

public class ModBuilderScript : EditorWindow
{
	private string ModName = "";
	private string Author = "";
	private string Description = "";
	public Sprite MyImage;
	private string Version = "1.0";
	private string Category = "Armor";


	[MenuItem("Schaken-Mods/Mod Builder/Open Window")]

	private static void OpenWindow() {
		ModBuilderScript window = GetWindow<ModBuilderScript>();

		// Set Title
		Texture2D icon = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Editor/Mod Builder/ModBuilder.png", typeof(Texture2D));

		GUIContent titleContent = new GUIContent("Mod Builder", icon);
		window.titleContent = titleContent;
		window.minSize = new Vector2(340, 410);
		window.maxSize = new Vector2(340, 410);

		window.Show();
	}

	private void OnGUI() {
		EditorGUIUtility.labelWidth = 45;
		EditorGUILayout.Space(10);

		EditorGUILayout.BeginVertical("box");

		EditorGUILayout.BeginHorizontal();
 		if (GUILayout.Button("Selected Category: "+Category)) {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Armor"), false, str => SetCategory("Armor"), "");
            menu.AddItem(new GUIContent("NPC"), false, str => SetCategory("NPC"), "");
            menu.AddItem(new GUIContent("Static Item"), false, str => SetCategory("Static Item"), "");
            menu.AddItem(new GUIContent("Weapon"), false, str => SetCategory("Weapon"), "");
            menu.AddDisabledItem(new GUIContent("Testing/SubCategory"));
            menu.ShowAsContext();
        }
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Mod Name: ", GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		ModName = GUILayout.TextField(ModName, 25, GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Version: ", GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		Version = GUILayout.TextField(Version, GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		EditorGUILayout.EndHorizontal();
		
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Author: ", GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		Author = GUILayout.TextField(Author, 25, GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		EditorGUILayout.EndHorizontal();
		
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Short Description: ", GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		Description = GUILayout.TextArea(Description, GUILayout.Height(100), GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		EditorGUILayout.EndHorizontal();
		
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Preview Picture: ", GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		MyImage = (Sprite)EditorGUILayout.ObjectField(MyImage, typeof(Sprite), false, GUILayout.Height(EditorGUIUtility.singleLineHeight), GUILayout.MaxWidth(160), GUILayout.MinWidth(160));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		Sprite s = (MyImage) ? MyImage as Sprite : (Sprite)AssetDatabase.LoadAssetAtPath("Assets/Textures/UI/Background.png", typeof(Sprite));
		EditorGUI.DrawTextureTransparent(new Rect(17, 225, 300, 160), s.texture);
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.Space(165);
		
		
		EditorGUILayout.BeginHorizontal();
		if (GUILayout.Button("Build Mod", GUILayout.MaxWidth(320), GUILayout.MinWidth(320)))
			{
				
				Debug.Log("Button pressed: "); //UnityEngine.Addressables.BuildPath
				string NewPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Mods\"+ModName+@"\";
				// I need to make the folder here!
				string ProcessingPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Mods\{ProcessFolder}";
				//string NewPath2 = UnityEditor.AddressableAssets.Settings.AddressableAssetSettings.kBuildPath;
				//UnityEditor.AddressableAssets.Settings.AddressableAssetSettings.kLocalBuildPathValue = NewPath;
				//string NewPath3 = UnityEngine.AddressableAssets.Addressables.BuildPath;
				//UnityEditor.AddressableAssets.Settings.AddressableAssetSettings.kBuildPath = NewPath;
				//Debug.Log(NewPath3);
				UnityEditor.AddressableAssets.Settings.AddressableAssetSettings.BuildPlayerContent();

				if (!Directory.Exists(NewPath))
					Directory.CreateDirectory(NewPath);

				DirectoryInfo d = new DirectoryInfo(ProcessingPath);
				foreach (var file in d.GetFiles()) {
				
					Debug.Log("File Found: "+file.Name);
					if (file.Name.Contains(".json")) {
						string A;
						using (var sr = new StreamReader(file.FullName)) {
							A = sr.ReadToEnd();
							A = A.Replace("{ProcessFolder}", ModName);
						}
						file.Delete();
						using (StreamWriter sw = File.CreateText(NewPath + "mod.json"))	{
							sw.WriteLine(A);
						}
						
					} else if (file.Name.Contains(".hash")) {
						Directory.Move(file.FullName, NewPath + "mod.hash");
					} else {
						Directory.Move(file.FullName, NewPath + file.Name);
					}
				}

				// build Info.info
				using (StreamWriter sw = File.CreateText(NewPath + "Info.fc1"))	{
					sw.WriteLine("Author:"+Author+";");
					sw.WriteLine("Description:"+Description+";");
					sw.WriteLine("Version:"+Version+";");
					sw.WriteLine("Created:"+DateTime.Now.ToString()+";");
					sw.WriteLine("Category:"+Category+";");
				}

				string OrigImage = AssetDatabase.GetAssetPath(MyImage);
				Debug.Log("OrigImage "+OrigImage);
				File.Copy(OrigImage, NewPath+"Image.png");

			}
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.EndVertical();

	}

	private void SetCategory(string A) {
		Category = A;
	}

	// Start is called before the first frame update
	void Start()
	{
		
	}

	// Update is called once per frame
	void Update()
	{
		
	}
}
