using NeoriginPCSDK;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using ThinkingData.Analytics;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Callbacks;
using UnityEngine;


public class PreBuildDllStripper : IPreprocessBuildWithReport
{
    public int callbackOrder => 0; // ִ˳
    public void OnPreprocessBuild(BuildReport report)
    {
        Debug.Log("ʼǰ DLL ...");
        //x86
        var channelconfig = Resources.Load<ChannelConfig>("ChannelConfig");
        {
            // ʾҵָ DLL ݵǰƽ̨Ƿ
            string x86dllPath = "Assets/SDKScripts/Plugins/PC/PlayPcSdk/x86/play_pc_sdk.dll";
            PluginImporter importer = AssetImporter.GetAtPath(x86dllPath) as PluginImporter;

            if (importer != null)
            {
                importer.SetCompatibleWithAnyPlatform(false);


                // ǰ׿ã
                if (channelconfig.currentChannel == ChannelType.GooglePlay)
                {
                    importer.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, true);
                    importer.SetPlatformData(BuildTarget.StandaloneWindows, "CPU", "x86");
                }
                else
                {
                    importer.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
                }

                importer.SaveAndReimport();
            }
            else
            {
                Debug.LogWarning($"Ҳx86dllPathļ: {x86dllPath}");
            }
        }
        //x64
        {
            string x64dllPath = "Assets/SDKScripts/Plugins/PC/PlayPcSdk/x86_64/play_pc_sdk.dll";
            PluginImporter importer = AssetImporter.GetAtPath(x64dllPath) as PluginImporter;

            if (importer != null)
            {
                importer.SetCompatibleWithAnyPlatform(false);


                // ǰ׿ã
                if (channelconfig.currentChannel == ChannelType.GooglePlay)
                {
                    importer.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, true);
                    importer.SetPlatformData(BuildTarget.StandaloneWindows64, "CPU", "x86_64");

                }
                else
                {
                    importer.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
                }

                importer.SaveAndReimport();
            }
            else
            {
                Debug.LogWarning($"Ҳx64dllPathļ: {x64dllPath}");
            }
        }
    }
}
public class GooglePostprocessBuild 
{

    [PostProcessBuildAttribute(88)]
    public static void OnPostProcessBuild(BuildTarget target, string targetPath)
    {
        var channelconfig = Resources.Load<ChannelConfig>("ChannelConfig");
        if (channelconfig.currentChannel== ChannelType.GooglePlay)
        {
            string manifestPath = Path.Combine(Application.dataPath, "SDKScripts/Plugins/PC/PlayPcSdk/manifest.xml");
            manifestPath = Path.GetFullPath(manifestPath); // 淶·
            manifestPath = manifestPath.Replace("/", "\\"); // ͳһΪ Windows ָ
            if (File.Exists(manifestPath))
            {
                // ȡĿ¼
                string buildFolder = Path.GetDirectoryName(targetPath);

                // exeļҪȡexeĿ¼
                if (target == BuildTarget.StandaloneWindows ||
                    target == BuildTarget.StandaloneWindows64)
                {
                    // exeļtargetPathexe·
                    // ҪȡexeڵĿ¼
                    if (Path.GetExtension(targetPath).ToLower() == ".exe")
                    {
                        buildFolder = Path.GetDirectoryName(targetPath);
                    }
                }

                string manifestTargetPath = Path.Combine(buildFolder, "manifest.xml");

                // ļѴڵ
                File.Copy(manifestPath, manifestTargetPath, true);
                Debug.Log($"Ѹ manifest.xml Ŀ¼: {manifestTargetPath}");
            }
            else
            {
                Debug.LogWarning($"Ҳmanifest.xmlļ: {manifestPath}");
            }
        }
    }
}
