using GameEditorPlugin.Win32.Editor;
using GameEditorPlugin.Win32.Runtime;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CommonAI.Zone.ZoneEditor;

namespace CommonAIEditor.Plugins.Win32
{


    public partial class Win32ScenePlugin : ISceneEditorPlugin
    {
        private Win32EditorPanel panel;

        public Win32ScenePlugin()
        {
            panel = new Win32EditorPanel(this);
        }


        //------------------------------------------------------------------------------

        public bool EnableRight { get { return true; } }

        public event PluginMessageHandler OnGetPluginMessage;
        public event CallAddSceneObject CallAddObject;
        public event CallResetSceneObject CallResetObject;

        public Control AsControl()
        {
            return panel;
        }
        
        public void SendMessage(object dt)
        {
            //string xml = EditorMessageDecoder.EncodeMessage(dt);
            //object data = EditorMessageDecoder.DecodeMessage(xml);
            panel.OnMsgReceived(dt);
        }

        class Win32EditorPanel : EditorDisplayPanel
        {
            private readonly Win32ScenePlugin plugin;

            public Win32EditorPanel(Win32ScenePlugin plugin)
            {
                this.plugin = plugin;
            }

            public override void SendToEditor(object data)
            {
                if (plugin.OnGetPluginMessage != null)
                {
                    plugin.OnGetPluginMessage.Invoke(data);
                }
            }
            protected override T CallAddObjectData<T>(Action<T> callback)
            {
                if (plugin.CallAddObject != null)
                {
                    return plugin.CallAddObject.Invoke(typeof(T), (t) => { callback.Invoke(t as T); }) as T;
                }
                return null;
            }
            protected override T CallResetObjectData<T>(string name, Action<T> callback)
            {
                if (plugin.CallResetObject != null)
                {
                    return plugin.CallResetObject.Invoke(name, (t) => { if (t is T) { callback.Invoke(t as T); } }) as T;
                }
                return null;
            }
        }

    }

    public class Win32EditorGamePlugin : IGameEditorPlugin
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);

        public string Name { get { return "Win32"; } }
        public string PluginDLL { get { return EditorPlugin.PluginDLL; } set { } }
        public string DataRoot { get { return Editor.EditorRootDir; } set { } }


        public ISceneEditorPlugin CreateScenePlugin(IGameEditorPlugin default_plugin)
        {
            return new Win32ScenePlugin();
        }
        public bool AcceptResource(IGameEditorPlugin d, string filename)
        {
            return true;
        }
        public void RunModelView(IGameEditorPlugin d, string[] filepath)
        {

        }
        public void RunTest(IGameEditorPlugin d, string data_dir, int sceneID, int actorTemplateID)
        {
            FormRuntimeGameLocal test = new FormRuntimeGameLocal(new DirectoryInfo(data_dir), sceneID);
            test.Show();
        }
        public void RunLocalPlay(IGameEditorPlugin d, DirectoryInfo data_dir, int sceneID, bool recorder)
        {
            new FormRuntimeGameLocal(data_dir, sceneID, recorder).Show();
        }
        public void RunServerPlay(IGameEditorPlugin d, DirectoryInfo data_dir, int sceneID)
        {
            new GameEditorPluginServer.Server.FormServer(data_dir, sceneID).Show();
        }
    }
}