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

namespace UnityEditor.HiveRemotePlay
{
    public static class HiveRemotePlayPostProcess {

        static void ProcessRemotePlayFileCopy(string resultPath) {
            // 3rd Party Plugin COPY - RemotePlayDll.dll
            string remoteplayPluginDll = Path.GetFullPath("Assets/HiveRemotePlay/Plugins/windows/RemotePlayDll.dll");

            string destinationDirectory = $"{resultPath}";
            string destinationRemoteDirectory = $"{resultPath}\\RemotePlay";

            if (File.Exists(remoteplayPluginDll)) {
                if (!Directory.Exists(destinationDirectory)) {
                    Directory.CreateDirectory(destinationDirectory);
                }

                if (!Directory.Exists(destinationRemoteDirectory)) {
                    Directory.CreateDirectory(destinationRemoteDirectory);
                }

                File.Copy(remoteplayPluginDll, $"{destinationDirectory}\\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, $"{destinationRemoteDirectory}\\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, $"{destinationRemoteDirectory}\\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, $"{destinationRemoteDirectory}\\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, $"{destinationRemoteDirectory}\\HiveVirtualInput.exe", true);
            } else {
                Debug.LogWarning("PostProcessor not found remoteplayPluginDll File : " + remoteVitrualInput);
            }


            EditorUtility.ClearProgressBar();
        }
        [HiveEditor.HivePostBuild(101)]
        public static void OnPostProcessBuild(HiveEditor.HivePostBuildContext ctx) {
            if (ctx.Summary.platform == BuildTarget.StandaloneWindows64) {
                ProcessRemotePlayFileCopy(ctx.PluginDeployPath);
            }
        }
    }
}
