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

namespace UnityEditor.HiveRemotePlay
{
    public static class HiveRemotePlayPostProcess {
        [PostProcessBuild(101)]
        public static void OnPostProcessBuild(BuildTarget target, string path)
        {
            if (target == BuildTarget.StandaloneWindows64) {
                
                string resultPath = Path.GetDirectoryName(path);
                // 3rd Party Plugin COPY - RemotePlayDll.dll
                string remoteplayPluginDll = Path.GetFullPath("Assets/HiveRemotePlay/Plugins/windows/RemotePlayDll.dll");
                
                string destinationDirectory = $"{resultPath}\\plugins";
                                
                if (File.Exists(remoteplayPluginDll)) {
                    if (!Directory.Exists(destinationDirectory)) {
                        Directory.CreateDirectory(destinationDirectory);
                    }

                    File.Copy(remoteplayPluginDll, $"{resultPath}\\plugins\\RemotePlayDll.dll", true);        
                } else {
                    Debug.LogWarning("PostProcessor not found remoteplayPluginDll File : " + remoteplayPluginDll);
                }
                // 3rd Party Plugin COPY - HiveRemoteStreamer.dll
                string remoteSteasmerDll = Path.GetFullPath("Assets/HiveRemotePlay/Plugins/windows/HiveRemoteStreamer.dll");
                if (File.Exists(remoteSteasmerDll)) {
                    File.Copy(remoteSteasmerDll, $"{resultPath}\\HiveRemoteStreamer.dll", true);        
                } else {
                    Debug.LogWarning("PostProcessor not found remoteplayPluginDll File : " + remoteSteasmerDll);
                }

                // 3rd Party Plugin COPY - HiveRemotePlay.png
                string remotePlayPng = Path.GetFullPath("Assets/HiveRemotePlay/Plugins/windows/HiveRemotePlay.png");
                if (File.Exists(remotePlayPng)) {
                    File.Copy(remotePlayPng, $"{resultPath}\\HiveRemotePlay.png", true);        
                } else {
                    Debug.LogWarning("PostProcessor not found remoteplayPluginDll File : " + remotePlayPng);
                }

                // 3rd Party Plugin COPY - HiveRemotePlayServiceMgr.exe
                string remotePlayServiceMgr = Path.GetFullPath("Assets/HiveRemotePlay/Plugins/windows/HiveRemotePlayServiceMgr.exe");
                if (File.Exists(remotePlayServiceMgr)) {
                    File.Copy(remotePlayServiceMgr, $"{resultPath}\\HiveRemotePlayServiceMgr.exe", true);        
                } else {
                    Debug.LogWarning("PostProcessor not found remoteplayPluginDll File : " + remotePlayServiceMgr);
                }

                // 3rd Party Plugin COPY - HiveVirtualInput.exe
                string remoteVitrualInput = Path.GetFullPath("Assets/HiveRemotePlay/Plugins/windows/HiveVirtualInput.exe");
                if (File.Exists(remoteVitrualInput)) {
                    File.Copy(remoteVitrualInput, $"{resultPath}\\HiveVirtualInput.exe", true);        
                } else {
                    Debug.LogWarning("PostProcessor not found remoteplayPluginDll File : " + remoteVitrualInput);
                }


                EditorUtility.ClearProgressBar();
            }
        }
    }
}
