/*
         *  2020-11-20 Jiang Jiahao
         *  该脚本中参数为DEMO参数,项目组根据实际参数修改
         *  导出工程后核对配置或依赖是否正确,根据需要修改脚本
         */
        public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)
        {
            Debug.Log("build IOS");

            if (BuildTarget == BuildTarget.iOS)
            {
                // 获得工程路径
                string projPath = PBXProject.GetPBXProjectPath(path);
                UnityEditor.iOS.Xcode.PBXProject proj = new PBXProject();
                proj.ReadFromString(File.ReadAllText(projPath));

                // 2019.3以上有多个target
#if UNITY_2019_3_OR_NEWER
                string unityFrameworkTarget = proj.GetUnityFrameworkTargetGuid();
                string target = proj.GetUnityMainTargetGuid();
#else
                string unityFrameworkTarget = proj.TargetGuidByName("Unity-iPhone");
                string target = proj.TargetGuidByName("Unity-iPhone");
#endif
                if (target == null)
                {
                    Debug.Log("target is null ?");
                    return;
                }

                proj.AddFrameworkToProject(unityFrameworkTarget, "AdServices.framework", true);
                proj.AddFrameworkToProject(unityFrameworkTarget, "iAd.framework", false);


                // 添加资源文件,注意文件路径
                var resourcePath = Path.Combine(path, "TDSGlobalResource");

                string parentFolder = Directory.GetParent(Application.dataPath).FullName;
                if (Directory.Exists(resourcePath))
                {
                    Directory.Delete(resourcePath, true);
                }

                Directory.CreateDirectory(resourcePath);

                string remotePackagePath = TDSFileHelper.FilterFile(parentFolder + "/Library/PackageCache/", "com.tds.global@");

                string localPackagePath = TDSFileHelper.FilterFile(parentFolder, "TDSGlobal");

                string tdsResourcePath = remotePackagePath != null? remotePackagePath + "/Plugins/iOS/Resource" : localPackagePath + "/Plugins/iOS/Resource";

                Debug.Log("tdsGlobalResource:" + tdsResourcePath);

                if (Directory.Exists(tdsResourcePath))
                {
                    //使用UPM接入
                    TDSFileHelper.CopyAndReplaceDirectory(tdsResourcePath, resourcePath);
                }
                // 复制资源文件夹到工程目录
                // 复制Assets的plist到工程目录
                File.Copy(parentFolder + "/Assets/Plugins/iOS/Resource/TDSGlobal-Info.plist", resourcePath + "/TDSGlobal-Info.plist");

                //获取bundleId
                var bundleId = GetValueFromPlist(resourcePath + "/TDSGlobal-Info.plist", "bundle_id");

                List <string> names = new List <string>();
                names.Add("TDSGlobalSDKResources.bundle");
                names.Add("LineSDKResource.bundle");
                names.Add("GoogleSignIn.bundle");
                names.Add("TDSGlobal-Info.plist");
                foreach (var name in names)
                {
                    proj.AddFileToBuild(target, proj.AddFile(Path.Combine(resourcePath, name), Path.Combine(resourcePath, name), PBXSourceTree.Source));
                }

                Debug.Log("添加resource成功");
                Debug.Log("bundleId:" + bundleId);
                // rewrite to file
                File.WriteAllText(projPath, proj.WriteToString());
                SetPlist(path, resourcePath + "/TDSGlobal-Info.plist", bundleId);
                SetScriptClass(path);
                Debug.Log("测试打包成功");
            }
        }
        /*
         *  2020-11-20 Jiang Jiahao
         *  该脚本中参数为DEMO参数,项目组根据实际参数修改
         *  导出工程后核对配置或依赖是否正确,根据需要修改脚本
         */
        public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)
        {
            if (BuildTarget == BuildTarget.iOS)
            {
                // 获得工程路径
                string projPath = PBXProject.GetPBXProjectPath(path);
                UnityEditor.iOS.Xcode.PBXProject proj = new PBXProject();
                proj.ReadFromString(File.ReadAllText(projPath));

                // 2019.3以上有多个target
#if UNITY_2019_3_OR_NEWER
                string unityFrameworkTarget = proj.GetUnityFrameworkTargetGuid();
                string target = proj.GetUnityMainTargetGuid();
#else
                string unityFrameworkTarget = proj.TargetGuidByName("Unity-iPhone");
                string target = proj.TargetGuidByName("Unity-iPhone");
#endif
                if (target == null)
                {
                    Debug.Log("target is null ?");
                    return;
                }

                // 编译配置
                proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
                proj.AddBuildProperty(unityFrameworkTarget, "OTHER_LDFLAGS", "-ObjC");
                // Swift编译选项
                proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO"); //bitcode  NO
                proj.SetBuildProperty(target, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "YES");
                proj.SetBuildProperty(target, "SWIFT_VERSION", "5.0");
                proj.SetBuildProperty(target, "CLANG_ENABLE_MODULES", "YES");
                proj.SetBuildProperty(unityFrameworkTarget, "ENABLE_BITCODE", "NO"); //bitcode  NO
                proj.SetBuildProperty(unityFrameworkTarget, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "YES");
                proj.SetBuildProperty(unityFrameworkTarget, "SWIFT_VERSION", "5.0");
                proj.SetBuildProperty(unityFrameworkTarget, "CLANG_ENABLE_MODULES", "YES");

                // add extra framework(s)
                // 参数: 目标targetGUID, framework,是否required:fasle->required,true->optional
                proj.AddFrameworkToProject(unityFrameworkTarget, "CoreTelephony.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "QuartzCore.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "Security.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "WebKit.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "Photos.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "AdSupport.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "AssetsLibrary.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "AVKit.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "AuthenticationServices.framework", true);
                proj.AddFrameworkToProject(unityFrameworkTarget, "LocalAuthentication.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "SystemConfiguration.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "Accelerate.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "SafariServices.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "AVFoundation.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "MobileCoreServices.framework", false);
                proj.AddFrameworkToProject(unityFrameworkTarget, "AppTrackingTransparency.framework", true);
                proj.AddFrameworkToProject(unityFrameworkTarget, "CoreMotion.framework", false);
                // 动态库
                Debug.Log("添加framework成功");

                // 添加 tbd
                // 参数: 目标targetGUID, tdbGUID
                proj.AddFileToBuild(unityFrameworkTarget, proj.AddFile("usr/lib/libc++.tbd", "libc++.tbd", "libsqlite3.0.tbd", "libz.tbd", "libresolv.tbd", PBXSourceTree.Sdk));

                Debug.Log("添加tbd成功");


                // 添加资源文件,注意文件路径
                var    resourcePath = Path.Combine(path, "TDSResource");
                string parentFolder = Directory.GetParent(Application.dataPath).FullName;

                Debug.Log("resourcePath:" + resourcePath);
                Debug.Log("parentFolder:" + parentFolder);

                if (Directory.Exists(resourcePath))
                {
                    Directory.Delete(resourcePath, true);
                }

                Directory.CreateDirectory(resourcePath);

                string remotePackagePath = TDSFileHelper.FilterFile(parentFolder + "/Library/PackageCache/", "com.tds.sdk");

                string localPacckagePath = TDSFileHelper.FilterFile(parentFolder, "TapSDK");

                string tdsResourcePath = remotePackagePath != null? remotePackagePath + "/Plugins/IOS/Resource" : localPacckagePath + "/Plugins/IOS/Resource";

                Debug.Log("tdsResourcePath:" + tdsResourcePath);

                if (Directory.Exists(tdsResourcePath))
                {
                    TDSFileHelper.CopyAndReplaceDirectory(tdsResourcePath, resourcePath);
                }
                // 复制资源文件夹到工程目录
                // 复制Assets的plist到工程目录
                if (File.Exists(parentFolder + "/Assets/Plugins/IOS/Resource/TDS-Info.plist"))
                {
                    File.Copy(parentFolder + "/Assets/Plugins/IOS/Resource/TDS-Info.plist", resourcePath + "/TDS-Info.plist");
                }

                List <string> names = new List <string>();
                names.Add("TDS-Info.plist");
                names.Add("TDSCommonResource.bundle");
                names.Add("TDSMomentResource.bundle");
                foreach (var name in names)
                {
                    proj.AddFileToBuild(target, proj.AddFile(Path.Combine(resourcePath, name), Path.Combine(resourcePath, name), PBXSourceTree.Source));
                }

                Debug.Log("添加resource成功");

                // rewrite to file
                File.WriteAllText(projPath, proj.WriteToString());
                SetPlist(path, resourcePath + "/TDS-Info.plist");
                SetScriptClass(path);
                Debug.Log("测试打包成功");
                return;
            }
        }