using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using UnityEngine;

public class PostProcessBuild_AddPod
{
    [PostProcessBuild]
    public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
    {
        if (target == BuildTarget.iOS)
        {
            // 定位到 Podfile 文件路径
            string podfilePath = Path.Combine(pathToBuiltProject, "Podfile");

            // 确保文件存在
            if (File.Exists(podfilePath))
            {
                // 读取 Podfile 文件内容
                string podfileContent = File.ReadAllText(podfilePath);

                // 检查是否已经包含 DataEyeSDK，避免重复添加
                if (!podfileContent.Contains("pod 'DataEyeSDK'"))
                {
                    // 在 `target 'UnityFramework'` 中增加 pod 指令
                    string targetPattern = "target 'UnityFramework' do";
                    if (podfileContent.Contains(targetPattern))
                    {
                        int insertIndex = podfileContent.IndexOf(targetPattern) + targetPattern.Length;
                        string customPod = "\n  pod 'DataEyeSDK'";
                        podfileContent = podfileContent.Insert(insertIndex, customPod);

                        // 写回 Podfile 文件
                        File.WriteAllText(podfilePath, podfileContent);

                        Debug.Log("Successfully added 'pod DataEyeSDK' to Podfile.");
                    }
                }
                else
                {
                    Debug.LogWarning("'DataEyeSDK' is already included in the Podfile.");
                }
            }
            else
            {
                Debug.LogError("Podfile not found in the Xcode project.");
            }
        }
    }
}
