using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using RevitEstimate.Models;

namespace RevitEstimate.Calculators
{
    public class Item12Calculator : IItemCalculator
    {
        private ProjectLevels _levels;

        public Item12Calculator(ProjectLevels levels = null)
        {
            _levels = levels ?? new ProjectLevels();
        }

        public List<EstimateRowModel> Calculate(List<Element> collector, out double totalQty)
        {
            totalQty = 0.0;
            var rows = new List<EstimateRowModel>();

            Document doc = collector.FirstOrDefault()?.Document;
            if (doc == null) return rows;

            // Collect all Rooms in the model for inner dimensions
            var rooms = new FilteredElementCollector(doc)
                .OfCategory(BuiltInCategory.OST_Rooms)
                .WhereElementIsNotElementType()
                .Cast<Room>()
                .Where(r => r.Area > 0)
                .ToList();

            foreach (var room in rooms)
            {
                string roomName = room.Name ?? "Room";
                GetRoomInternalDimensions(room, out double rLen, out double rWid);

                // Area = Length x Width (Sqm, No Height/Depth)
                double qty = rLen * rWid;
                totalQty += qty;

                rows.Add(new EstimateRowModel
                {
                    SNo = "",
                    ItemOfWork = $"   -> Room Inner Area (Item 12): {roomName} [{room.Number}]",
                    NosOfFlats = "1",
                    NosOfInstance = "1",
                    Length = rLen.ToString("F2"),
                    Width = rWid.ToString("F2"),
                    HeightDepth = "", // No height/depth
                    Qty = qty.ToString("F2"),
                    Unit = "Sqm",
                    Remarks = $"Room inner dimensions area (L={rLen:F2}m x W={rWid:F2}m)."
                });
            }

            if (rows.Count == 0)
            {
                rows.Add(new EstimateRowModel
                {
                    SNo = "",
                    ItemOfWork = "   -> [No rooms found for Item 12]",
                    NosOfFlats = "-",
                    NosOfInstance = "0",
                    Length = "-",
                    Width = "-",
                    HeightDepth = "-",
                    Qty = "0.00",
                    Unit = "-",
                    Remarks = "No rooms detected."
                });
            }

            return rows;
        }

        private void GetRoomInternalDimensions(Room room, out double lengthMeters, out double widthMeters)
        {
            lengthMeters = 4.0; widthMeters = 3.0; // Fallback dimensions
            BoundingBoxXYZ box = room.get_BoundingBox(null);
            if (box != null)
            {
                lengthMeters = UnitUtils.ConvertFromInternalUnits(Math.Abs(box.Max.X - box.Min.X), UnitTypeId.Meters);
                widthMeters = UnitUtils.ConvertFromInternalUnits(Math.Abs(box.Max.Y - box.Min.Y), UnitTypeId.Meters);
            }
        }
    }
}