using System;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

#if UNITY_ANDROID

public partial class ToukaAnalyticsBuildPostProcessor_Android
{
	[PostProcessBuild]
	public static void OnPostprocessBuild (BuildTarget buildTarget, string path) {
		if (buildTarget != BuildTarget.Android) return;

#if UNITY_ANDROID
		CopyFiles(path);
#endif
	}

#if UNITY_ANDROID
	private static void CopyFiles(string _path) {
		string xmlPath = Application.dataPath + "/Plugins/ToukaGames/Android/res/xml/";
		string desPath = _path + "/launcher/src/main/res/xml/";
		CopyFolder2(xmlPath, desPath);
	}

	/// <summary>
	/// 复制文件夹及文件
	/// </summary>
	/// <param name="sourceFolder">原文件路径</param>
	/// <param name="destFolder">目标文件路径</param>
	/// <returns></returns>
	private static int CopyFolder2(string sourceFolder, string destFolder)
	{
		try
		{
			string folderName = System.IO.Path.GetFileName(sourceFolder);
			string destfolderdir = System.IO.Path.Combine(destFolder, folderName);
			string[] filenames = System.IO.Directory.GetFileSystemEntries(sourceFolder);
			foreach (string file in filenames)// 遍历所有的文件和目录
			{
                if (file.EndsWith(".meta"))
                {
					continue;
                }

				if (System.IO.Directory.Exists(file))
				{
					string currentdir = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file));
					if (!System.IO.Directory.Exists(currentdir))
					{
						System.IO.Directory.CreateDirectory(currentdir);
					}
					CopyFolder2(file, destfolderdir);
				}
				else
				{
					string srcfileName = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file));
					if (!System.IO.Directory.Exists(destfolderdir))
					{
						System.IO.Directory.CreateDirectory(destfolderdir);
					}
					System.IO.File.Copy(file, srcfileName);
				}
			}

			return 1;
		}
		catch (Exception e)
		{
			return 0;
		}

	}
#endif
}
#endif