using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using Microsoft.Win32;

namespace RevitEstimate.PlumbingModule
{
    public partial class PlumbingReportWindow : Window
    {
        private List<DsrEstimateItem> _reportItems;

        public PlumbingReportWindow(List<DsrEstimateItem> reportItems)
        {
            InitializeComponent();
            _reportItems = reportItems;
            GridReportPreview.ItemsSource = _reportItems;
        }

        private void BtnExportCSV_Click(object sender, RoutedEventArgs e)
        {
            if (_reportItems == null || _reportItems.Count == 0) return;

            SaveFileDialog saveDialog = new SaveFileDialog
            {
                Filter = "Excel Workbook (*.xls)|*.xls|CSV File (*.csv)|*.csv",
                FileName = "INT_PLUM_DSR_Estimate.xls"
            };

            if (saveDialog.ShowDialog() == true)
            {
                if (saveDialog.FileName.EndsWith(".xls"))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">");
                    sb.AppendLine("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><style>");
                    sb.AppendLine("table { border-collapse: collapse; width: 100%; font-family: Calibri, sans-serif; } th { background-color: #007ACC; color: white; border: 1px solid #000000; padding: 8px; } td { border: 1px solid #cccccc; padding: 6px; }");
                    sb.AppendLine("</style></head><body>");
                    sb.AppendLine("<h2>Internal Plumbing DSR Estimation</h2><table>");
                    sb.AppendLine("<tr><th>Group</th><th>Sr.No</th><th>DSR Code</th><th>Description</th><th>Remark</th><th>Unit</th><th>Qty</th><th>Rate(₹)</th><th>Amount(₹)</th></tr>");

                    foreach (var item in _reportItems)
                    {
                        sb.AppendLine($"<tr><td>{item.CategoryGroup}</td><td align=\"center\">{item.SrNo}</td><td align=\"center\">{item.DsrCode}</td><td>{item.Description}</td><td style=\"color:#555;font-style:italic;\">{item.Remark}</td><td align=\"center\">{item.Unit}</td><td align=\"right\">{item.Qty:N2}</td><td align=\"right\">{item.Rate:N2}</td><td align=\"right\"><b>{item.Amount:N2}</b></td></tr>");
                    }
                    sb.AppendLine("</table></body></html>");
                    File.WriteAllText(saveDialog.FileName, sb.ToString(), Encoding.UTF8);
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Group,Sr.No,DSR Code,Description,Remark,Unit,Qty,Rate,Amount");
                    foreach (var item in _reportItems)
                    {
                        string desc = $"\"{item.Description.Replace("\"", "\"\"")}\"";
                        string rmk = $"\"{item.Remark?.Replace("\"", "\"\"") ?? ""}\"";
                        sb.AppendLine($"\"{item.CategoryGroup}\",{item.SrNo},{item.DsrCode},{desc},{rmk},{item.Unit},{item.Qty},{item.Rate},{item.Amount}");
                    }
                    File.WriteAllText(saveDialog.FileName, sb.ToString(), Encoding.UTF8);
                }

                MessageBox.Show("Exported successfully!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                System.Diagnostics.Process.Start(saveDialog.FileName);
            }
        }
    }
}