using Kbg.NppPluginNET.PluginInfrastructure;
using MarkdownRenderer;
using System;
using System.Diagnostics;
using System.Reflection;

namespace NppMarkdownRenderer
{
    public static class AnyCpuBridgeMain
    {
        static AnyCpuBridgeMain()
        {
            try
            {
                InitReferenceAssembly();
            }
            catch (Exception exception)
            {
                System.Windows.Forms.MessageBox.Show($"Plugin init fail:{PluginName},{exception}");
                throw;
            }
        }

        private static void InitReferenceAssembly()
        {
            MarkdownRendererMain.Init();
        }

        public static readonly string PluginName = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductName;

        public static void CommandMenuInit()
        {
            PluginBridge.SetupPluginMenu();
        }

        public static void SetToolBarIcon()
        {
            PluginBridge.SetupToolBarIcon();
        }

        public static void OnNotification(ScNotification notification)
        {
            if (false == PluginBridge.Enable) return;

            //Check for updates
            if (notification.Header.Code == (uint)SciMsg.SCN_UPDATEUI)
            {
                //Update the view
                if (0 != (notification.Updated & (uint)SciMsg.SC_UPDATE_V_SCROLL)) PluginBridge.UpdateScroll();
            }
            else if (notification.Header.Code == (uint)NppMsg.NPPN_BUFFERACTIVATED)
            {
                //Update the scintilla handle in all cases to keep track of which instance is active
                PluginBridge.FlushState();
                PluginBridge.UpdateText();
                PluginBridge.UpdateScroll();
            }
            else if (notification.Header.Code == (uint)SciMsg.SCN_MODIFIED
                     && (notification.ModificationType & ((uint)SciMsg.SC_MOD_INSERTTEXT | (uint)SciMsg.SC_MOD_DELETETEXT)) != 0)
            {
                //Track if any text modifications have been made
                PluginBridge.UpdateText();
            }
        }

        public static void PluginCleanUp()
        {
            PluginBridge.Exit();
        }
    }
}
