using Autodesk.Revit.UI;
using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Media.Imaging;

namespace RevitEstimate
{
    [Guid("8434EBF8-E525-407E-BBC3-45CA9A0DCC07")]
    public class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication app)
        {
            string tabName = "Revit Estimate";
            try { app.CreateRibbonTab(tabName); } catch { }

            RibbonPanel panel = app.CreateRibbonPanel(tabName, "Estimation Tools");
            string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;

            // ==========================================
            // BUTTON 1: Set Level
            // ==========================================
            PushButtonData btnSetLevelData = new PushButtonData(
                "CmdSetLevel",
                "Set\nLevel",
                thisAssemblyPath,
                "RevitEstimate.CmdSetLevel"
            );
            PushButton pbSetLevel = panel.AddItem(btnSetLevelData) as PushButton;
            pbSetLevel.ToolTip = "Click to configure project levels before generating the estimate.";

            // ==========================================
            // BUTTON 2: Generate Estimate
            // ==========================================
            PushButtonData btnGenerateData = new PushButtonData(
                "CmdGenerateEstimate",
                "Generate\nEstimate",
                thisAssemblyPath,
                "RevitEstimate.CmdGenerateEstimate"
            );
            PushButton pbGenerate = panel.AddItem(btnGenerateData) as PushButton;
            pbGenerate.ToolTip = "Click to generate the detailed cost estimation report.";

            // ==========================================
            // BUTTON 3: Plumbing Estimation
            // ==========================================
            PushButtonData btnPlumbingData = new PushButtonData(
                "CmdPlumbingEstimate",
                "Plumbing\nEstimate",
                thisAssemblyPath,
                "RevitEstimate.PlumbingModule.PlumbingCommand"
            );
            PushButton pbPlumbing = panel.AddItem(btnPlumbingData) as PushButton;
            pbPlumbing.ToolTip = "Open Advanced MEP Plumbing Estimation & DSR Module.";

            // ==========================================
            // BUTTON 4: Electrical Estimation
            // ==========================================
            PushButtonData btnElectricalData = new PushButtonData(
                "CmdElectricalEstimate",
                "Electrical\nEstimate",
                thisAssemblyPath,
                "RevitEstimate.ElectricalModule.ElectricalCommand"
            );
            PushButton pbElectrical = panel.AddItem(btnElectricalData) as PushButton;
            pbElectrical.ToolTip = "Open Advanced MEP Electrical Fittings Estimation & BIM Module.";

            // ==========================================
            // BUTTON 5: Calculate Steel
            // ==========================================
            PushButtonData btnSteelData = new PushButtonData(
                "CmdCalculateReinforcement",
                "Calculate\nSteel",
                thisAssemblyPath,
                "RevitEstimate.CmdCalculateReinforcement"
            );
            PushButton pbSteel = panel.AddItem(btnSteelData) as PushButton;
            pbSteel.ToolTip = "Calculate reinforcement (Steel) for Foundation, Column, Slab, Beam, and Stairs.";

            // ==========================================
            // BUTTON 6: About Panel (NEWLY ADDED)
            // ==========================================
            PushButtonData btnAboutData = new PushButtonData(
                "CmdAbout",
                "About\nPlugin",
                thisAssemblyPath,
                "RevitEstimate.CmdAbout"
            );
            PushButton pbAbout = panel.AddItem(btnAboutData) as PushButton;
            pbAbout.ToolTip = "View detailed information about Revit Estimate Panel and Pooja Enterprises.";

            // ==========================================
            // LOGO SETUP FOR ALL BUTTONS
            // ==========================================
            try
            {
                Uri uriLevel = new Uri("pack://application:,,,/RevitEstimate;component/Images/icon_level.png");
                pbSetLevel.LargeImage = new BitmapImage(uriLevel);

                Uri uriCivil = new Uri("pack://application:,,,/RevitEstimate;component/Images/icon_civil.png");
                pbGenerate.LargeImage = new BitmapImage(uriCivil);
                pbSteel.LargeImage = new BitmapImage(uriCivil); // Reusing civil icon for Steel temporarily

                Uri uriPlumbing = new Uri("pack://application:,,,/RevitEstimate;component/Images/icon_plumbing.png");
                pbPlumbing.LargeImage = new BitmapImage(uriPlumbing);

                Uri uriElectrical = new Uri("pack://application:,,,/RevitEstimate;component/Images/icon_electrical.png");
                pbElectrical.LargeImage = new BitmapImage(uriElectrical);

                // Reusing level or civil icon for About button (or you can add a new icon if available)
                pbAbout.LargeImage = new BitmapImage(uriLevel);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication app)
        {
            return Result.Succeeded;
        }
    }
}