using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace uBIMEarthTools
{
    public class Ribbon : Autodesk.Revit.UI.IExternalApplication
    {
        static string AddInPath = typeof(Ribbon).Assembly.Location;
        public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            AppDomain.CurrentDomain.TypeResolve += CurrentDomain_TypeResolve;
            CreateRibbonSamplePanel(application);
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            AppDomain.CurrentDomain.TypeResolve -= CurrentDomain_TypeResolve;
            return Result.Succeeded;
        }

        private void CreateRibbonSamplePanel(UIControlledApplication application)
        {
            application.CreateRibbonTab("uBIM三维地质");
            RibbonPanel sPanel = application.CreateRibbonPanel("uBIM三维地质", "uBIM三维地质");
            PushButtonData ToggleBackground_ButtonData = new PushButtonData("数据导出", "数据导出", AddInPath, "uBIMEarthTools.Commands.DwgDataExportCommand");
            ToggleBackground_ButtonData.Image = Icon(uBIMEarthTools.Properties.Resources.contract);
            ToggleBackground_ButtonData.LargeImage = Icon(uBIMEarthTools.Properties.Resources.contract);
            sPanel.AddItem(ToggleBackground_ButtonData);

            PushButtonData ReLoadLink_ButtonData = new PushButtonData("模型生成", "模型生成", AddInPath, "uBIMEarthTools.Commands.GeologyModelCommand");
            ReLoadLink_ButtonData.Image = Icon(uBIMEarthTools.Properties.Resources.pyramid);
            ReLoadLink_ButtonData.LargeImage = Icon(uBIMEarthTools.Properties.Resources.pyramid);
            sPanel.AddItem(ReLoadLink_ButtonData);

            ReLoadLink_ButtonData = new PushButtonData("桩长校核", "桩长校核", AddInPath, "uBIMEarthTools.Commands.DesignDataEntry.DesignDataEntryCommand");
            ReLoadLink_ButtonData.Image = Icon(uBIMEarthTools.Properties.Resources.pyramid);
            ReLoadLink_ButtonData.LargeImage = Icon(uBIMEarthTools.Properties.Resources.pyramid);
            sPanel.AddItem(ReLoadLink_ButtonData);

            ReLoadLink_ButtonData = new PushButtonData("开挖土方量统计", "开挖土方量统计", AddInPath, "uBIMEarthTools.Commands.MassEarthwork.MassCutEverythingCommand");
            ReLoadLink_ButtonData.Image = Icon(uBIMEarthTools.Properties.Resources.pyramid);
            ReLoadLink_ButtonData.LargeImage = Icon(uBIMEarthTools.Properties.Resources.pyramid);
            sPanel.AddItem(ReLoadLink_ButtonData);

        }

        private BitmapSource Icon(System.Drawing.Bitmap bitmap)
        {
            BitmapSource bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            return bitSrc;
        }
        private Assembly CurrentDomain_TypeResolve(object sender, ResolveEventArgs args)
        {
            AssemblyName assemblyName = new AssemblyName(args.Name);
            if (assemblyName.Name.Split(',').First().EndsWith(".resources"))
            {
                return null;
            }

            if (args.RequestingAssembly == null)
            {
                return null;
            }

            string filePath = Path.Combine(Path.GetDirectoryName(args.RequestingAssembly.Location), $"{assemblyName.Name}.dll");
            return File.Exists(filePath) ? Assembly.LoadFile(filePath) : null;
        }

        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            AssemblyName assemblyName = new AssemblyName(args.Name);
            if (assemblyName.Name.Split(',').First().EndsWith(".resources"))
            {
                return null;
            }

            if (args.RequestingAssembly == null)
            {
                return null;
            }

            string filePath = Path.Combine(Path.GetDirectoryName(args.RequestingAssembly.Location), $"{assemblyName.Name}.dll");
            return File.Exists(filePath) ? Assembly.LoadFile(filePath) : null;
        }
    }
}
