using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using RevitEstimate.Models;

namespace RevitEstimate
{
    // ==========================================
    // DATA STORAGE: Dono buttons ke beech data share karne ke liye
    // ==========================================
    public static class SessionData
    {
        public static ProjectLevels CurrentLevels { get; set; } = new ProjectLevels();
    }

    // ==========================================
    // BUTTON 1 COMMAND: Set Level (No License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class CmdSetLevel : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;

            LevelSetupWindow levelWindow = new LevelSetupWindow(doc);
            if (levelWindow.ShowDialog() == true)
            {
                SessionData.CurrentLevels = levelWindow.Levels;
                TaskDialog.Show("Success", "Project Levels have been configured successfully!");
            }

            return Result.Succeeded;
        }
    }

    // ==========================================
    // BUTTON 2 COMMAND: Generate Estimate (License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class CmdGenerateEstimate : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            if (!LicenseManager.IsLicensed())
            {
                ActivationWindow actWindow = new ActivationWindow();
                actWindow.ShowDialog();

                if (!actWindow.IsActivated)
                {
                    return Result.Cancelled;
                }
            }

            Document doc = commandData.Application.ActiveUIDocument.Document;
            EstimateWindow estimateWindow = new EstimateWindow(doc, SessionData.CurrentLevels);
            estimateWindow.ShowDialog();

            return Result.Succeeded;
        }
    }

    // ==========================================
    // BUTTON 3 COMMAND: Calculate Steel (License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class CmdCalculateReinforcement : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            if (!LicenseManager.IsLicensed())
            {
                ActivationWindow actWindow = new ActivationWindow();
                actWindow.ShowDialog();

                if (!actWindow.IsActivated)
                {
                    return Result.Cancelled;
                }
            }

            Document doc = commandData.Application.ActiveUIDocument.Document;
            SteelEstimateWindow steelWindow = new SteelEstimateWindow(doc);
            steelWindow.ShowDialog();

            return Result.Succeeded;
        }
    }

    // ==========================================
    // BUTTON: Plumbing Estimate Command (License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class PlumbingCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            if (!LicenseManager.IsLicensed())
            {
                ActivationWindow actWindow = new ActivationWindow();
                actWindow.ShowDialog();

                if (!actWindow.IsActivated)
                {
                    return Result.Cancelled;
                }
            }

            Document doc = commandData.Application.ActiveUIDocument.Document;
            var plumbingWindow = new RevitEstimate.PlumbingModule.PlumbingEstimateWindow(doc);
            plumbingWindow.ShowDialog();

            return Result.Succeeded;
        }
    }

    // ==========================================
    // BUTTON: Electrical Estimate Command (License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class ElectricalCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            if (!LicenseManager.IsLicensed())
            {
                ActivationWindow actWindow = new ActivationWindow();
                actWindow.ShowDialog();

                if (!actWindow.IsActivated)
                {
                    return Result.Cancelled;
                }
            }

            Document doc = commandData.Application.ActiveUIDocument.Document;
            var electricalWindow = new RevitEstimate.ElectricalModule.ElectricalEstimateWindow(doc);
            electricalWindow.ShowDialog();

            return Result.Succeeded;
        }
    }

    // ==========================================
    // BUTTON: About Plugin (No License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class CmdAbout : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            AboutWindow aboutWin = new AboutWindow();
            aboutWin.ShowDialog();
            return Result.Succeeded;
        }
    }

    // ==========================================
    // BUTTON: Buy / Activate License (No License Required)
    // ==========================================
    [Transaction(TransactionMode.Manual)]
    public class CmdBuyLicense : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            ActivationWindow actWin = new ActivationWindow();
            actWin.ShowDialog();
            return Result.Succeeded;
        }
    }
}