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

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

        public Item11Calculator(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;

            // 1. Outer Walls Layout Footprint (Using same bounding box + 5.0m offset logic as Item 1)
            var walls = collector.Where(e => e.Category != null && e.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Walls).ToList();
            if (walls.Count > 0)
            {
                double buildingLength = 10.0;
                double buildingWidth = 8.0;

                try
                {
                    BoundingBoxXYZ totalBox = null;
                    foreach (var wall in walls)
                    {
                        BoundingBoxXYZ box = wall.get_BoundingBox(null);
                        if (box != null)
                        {
                            if (totalBox == null) totalBox = box;
                            else
                            {
                                totalBox.Min = new XYZ(Math.Min(totalBox.Min.X, box.Min.X), Math.Min(totalBox.Min.Y, box.Min.Y), Math.Min(totalBox.Min.Z, box.Min.Z));
                                totalBox.Max = new XYZ(Math.Max(totalBox.Max.X, box.Max.X), Math.Max(totalBox.Max.Y, box.Max.Y), Math.Max(totalBox.Max.Z, box.Max.Z));
                            }
                        }
                    }

                    if (totalBox != null)
                    {
                        buildingLength = UnitUtils.ConvertFromInternalUnits(Math.Abs(totalBox.Max.X - totalBox.Min.X), UnitTypeId.Meters);
                        buildingWidth = UnitUtils.ConvertFromInternalUnits(Math.Abs(totalBox.Max.Y - totalBox.Min.Y), UnitTypeId.Meters);
                    }
                }
                catch (Exception) { }

                double finalLength = buildingLength + 5.0; // +5m offset
                double finalWidth = buildingWidth + 5.0;   // +5m offset

                // Qty = Length x Width (Area in Sqm, No Height/Depth, No Foundations)
                double qty = finalLength * finalWidth;
                totalQty += qty;

                rows.Add(new EstimateRowModel
                {
                    SNo = "",
                    ItemOfWork = "   -> Outer Building Layout Area (With 5m Offset)",
                    NosOfFlats = "1",
                    NosOfInstance = "1",
                    Length = finalLength.ToString("F2"),
                    Width = finalWidth.ToString("F2"),
                    HeightDepth = "", // Blank / No Height
                    Qty = qty.ToString("F2"),
                    Unit = "Sqm",
                    Remarks = $"Outer layout length + 5m ({finalLength:F2}m), width + 5m ({finalWidth:F2}m), no height/depth, no foundations."
                });
            }

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

            return rows;
        }
    }
}