using Kbg.NppPluginNET.PluginInfrastructure;
using System;
using System.Runtime.InteropServices;

namespace NppMarkdownRenderer
{
    public class ManagedExportsBridge
    {
        private static IntPtr PtrPluginName = IntPtr.Zero;

        public static bool IsUnicode()
        {
            return true;
        }

        public static void SetInfo(NppData notepadPlusData)
        {
            PluginBase.nppData = notepadPlusData;
            AnyCpuBridgeMain.CommandMenuInit();
        }

        public static IntPtr GetFuncsArray(ref int nbF)
        {
            nbF = PluginBase._funcItems.Items.Count;
            return PluginBase._funcItems.NativePointer;
        }

        public static uint MessageProc(uint message, IntPtr wParam, IntPtr lParam)
        {
            return 1;
        }

        public static IntPtr GetName()
        {
            if (PtrPluginName == IntPtr.Zero)
                PtrPluginName = Marshal.StringToHGlobalUni(AnyCpuBridgeMain.PluginName);
            return PtrPluginName;
        }

        public static void BeNotified(IntPtr notifyCode)
        {
            var notification = (ScNotification)Marshal.PtrToStructure(notifyCode, typeof(ScNotification));
            if (notification.Header.Code == (uint)NppMsg.NPPN_TBMODIFICATION)
            {
                PluginBase._funcItems.RefreshItems();
                AnyCpuBridgeMain.SetToolBarIcon();
            }
            else if (notification.Header.Code == (uint)NppMsg.NPPN_SHUTDOWN)
            {
                AnyCpuBridgeMain.PluginCleanUp();
                Marshal.FreeHGlobal(PtrPluginName);
            }
            else
            {
                AnyCpuBridgeMain.OnNotification(notification);
            }
        }
    }
}