using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEditor;
using UnityEditor.Callbacks;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
using UnityEngine;

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

#if UNITY_IOS
		SetTeamIdAutoSign(path);
		ModifyFrameworks(path);
		ModifyBuildSettings(path);

		ModifyInfoPlist(path);

		AddLocalization(path);
		CopySplashImg(path);
#endif
	}

	private static void ModifyFrameworks(string _path)
	{
#if UNITY_IOS
		string projPath = PBXProject.GetPBXProjectPath(_path);
		PBXProject proj = new PBXProject();


		proj.ReadFromString(File.ReadAllText(projPath));
#if UNITY_2019_3_OR_NEWER
		string target = proj.GetUnityFrameworkTargetGuid();
#else
		string target = proj.TargetGuidByName("Unity-iPhone");
#endif

		proj.AddFrameworkToProject(target, "GLKit.framework", false);
		proj.AddFrameworkToProject(target, "CoreGraphics.framework", false);
		proj.AddFrameworkToProject(target, "SystemConfiguration.framework", false);
		proj.AddFrameworkToProject(target, "Photos.framework", false);
		proj.AddFrameworkToProject(target, "AppTrackingTransparency.framework", false);
		proj.AddFrameworkToProject(target, "StoreKit.framework", false);
		proj.AddFrameworkToProject(target, "CoreData.framework", false);
		proj.AddFrameworkToProject(target, "AVFoundation.framework", false);
		proj.AddFrameworkToProject(target, "Accelerate.framework", false);
		proj.AddFrameworkToProject(target, "AdSupport.framework", false);
		proj.AddFrameworkToProject(target, "AudioToolbox.framework", false);
		proj.AddFrameworkToProject(target, "CoreLocation.framework", false);
		proj.AddFrameworkToProject(target, "CoreMedia.framework", false);
		proj.AddFrameworkToProject(target, "CoreMotion.framework", false);
		proj.AddFrameworkToProject(target, "CoreTelephony.framework", false);
		proj.AddFrameworkToProject(target, "MobileCoreServices.framework", false);
		proj.AddFrameworkToProject(target, "SafariServices.framework", false);
		proj.AddFrameworkToProject(target, "VideoToolbox.framework", false);
		proj.AddFrameworkToProject(target, "WebKit.framework", false);



		string fileGuidSqlite = proj.AddFile("usr/lib/libxml2.tbd", "Libraries/libxml2.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libsqlite3.tbd", "Libraries/libsqlite3.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libc++.tbd", "Libraries/libc++.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libz.tbd", "Libraries/libz.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libresolv.tbd", "Libraries/libresolv.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libbz2.1.0.tbd", "Libraries/libbz2.1.0.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libresolv.9.tbd", "Libraries/libresolv.9.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);
		fileGuidSqlite = proj.AddFile("usr/lib/libc++abi.tbd", "Libraries/libc++abi.tbd", PBXSourceTree.Sdk);
		proj.AddFileToBuild(target, fileGuidSqlite);

		File.WriteAllText(projPath, proj.WriteToString());
#endif
	}

	private static void ModifyBuildSettings(string _path)
	{
#if UNITY_IOS
		string projPath = PBXProject.GetPBXProjectPath(_path);
		PBXProject proj = new PBXProject();
		proj.ReadFromString(File.ReadAllText(projPath));

#if UNITY_2019_3_OR_NEWER
		string target = proj.GetUnityFrameworkTargetGuid();
#else
		string target = proj.TargetGuidByName("Unity-iPhone");
#endif

		proj.SetBuildProperty(target, "CODE_SIGN_STYLE", "Automatic");
		proj.SetBuildProperty(target, "DEVELOPMENT_TEAM", "K8TAQ2544R");

		proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
		proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
		proj.SetBuildProperty(target, "GCC_C_LANGUAGE_STANDARD", "gnu11");


		File.WriteAllText(projPath, proj.WriteToString());
#endif
	}

	private static void SetTeamIdAutoSign(string _path)
	{
#if UNITY_IOS
		string projPath = PBXProject.GetPBXProjectPath(_path);
		PBXProject proj = new PBXProject();
		proj.ReadFromString(File.ReadAllText(projPath));

		string target = proj.GetUnityMainTargetGuid();

		proj.SetBuildProperty(target, "CODE_SIGN_STYLE", "Automatic");
		proj.SetBuildProperty(target, "DEVELOPMENT_TEAM", "K8TAQ2544R");

		File.WriteAllText(projPath, proj.WriteToString());
#endif
	}

	private static void ModifyInfoPlist(string _path)
	{
#if UNITY_IOS
		//2、修改Info.plist文件
		var plistPath = Path.Combine(_path, "Info.plist");
		var plist = new PlistDocument();
		plist.ReadFromFile(plistPath);
#if IOS_CN
		plist.root.SetString("NSLocationAlwaysUsageDescription", "为了更好的体验游戏");
		plist.root.SetString("NSLocationWhenInUseUsageDescription", "为了更好的体验游戏");
		plist.root.SetString("NSUserTrackingUsageDescription", "该标识符将用于向您投放个性化广告");
		plist.root.SetString("NSPhotoLibraryUsageDescription", "为了更好的体验游戏,请允许APP保存图片到您的相册");
#else
		plist.root.SetString ("NSLocationAlwaysUsageDescription", "for better experience the game");
		plist.root.SetString ("NSLocationWhenInUseUsageDescription", "for better experience the game");
		plist.root.SetString("NSUserTrackingUsageDescription", "This identifier will be used to deliver personalized ads to you.");
		plist.root.SetString("NSPhotoLibraryUsageDescription", "In order to experience the game better, please allow the APP to save pictures to your album.");
		
#endif
		File.WriteAllText(plistPath, plist.WriteToString());
#endif
	}



	static void AddLocalization(string path)
	{
#if !NO_SDK
		string pliststringPath = Application.dataPath + "/Plugins/ToukaGames/iOS/CommonPackage/lang/infoplist";
		NativeLocale.AddLocalizedStringsIOS(path, pliststringPath);
#endif
	}

	static void CopySplashImg(string path)
	{
		string splashImgPath = Application.dataPath + "/Plugins/ToukaGames/iOS/CommonPackage/LaunchScreen-iPhonePortrait.png";
		File.Copy(splashImgPath, path + "/LaunchScreen-iPhonePortrait.png", true);

		string splashImgPathIpad = Application.dataPath + "/Plugins/ToukaGames/iOS/CommonPackage/LaunchScreen-iPad.png";
		File.Copy(splashImgPathIpad, path + "/LaunchScreen-iPad.png", true);
	}

}