using Unity;
using LDBproject.Repositories;
using LDBproject.Reports;
using LDBproject.Entities;

namespace LDBproject.AdditionalForms
{
    public partial class OrdersReportF : Form
    {
        private readonly IUnityContainer _container;
        private readonly IOrderRep _orderRep;

        public OrdersReportF(IUnityContainer container, IOrderRep orderRep)
        {
            InitializeComponent();
            _container = container ?? throw new ArgumentNullException(nameof(container));
            _orderRep = orderRep ?? throw new ArgumentNullException(nameof(orderRep));

            var ordersInfo = _orderRep.GetOrdersInfo().ToList();

            if (ordersInfo.Any())
            {
                ComboBoxB.DataSource = ordersInfo;
                ComboBoxB.DisplayMember = "DisplayInfo";
                ComboBoxB.ValueMember = "OrderID"; // Assuming the data model has this property
            }
            else
            {
                MessageBox.Show("No Orders Found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }
        private void SelectFilePathBtn_Click(object sender, EventArgs e)
        {
            var sfd = new SaveFileDialog() { Filter = "Excel Files | *.xlsx" };
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                FilePathTb.Text = sfd.FileName;
            }
        }

        private void MakeReportBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = FilePathTb.Text;
                if (string.IsNullOrWhiteSpace(filePath))
                {
                    MessageBox.Show("Please select a file path.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (ComboBoxB.SelectedItem == null)
                {
                    MessageBox.Show("Please select an order.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                DateTime startDate = BeginDTP.Value;
                DateTime endDate = FinDTP.Value;

                if (startDate >= endDate)
                {
                    MessageBox.Show("Start date must be before end date.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                // Correctly get the selected OrderInfo object
                if (ComboBoxB.SelectedItem is not Order selectedOrderInfo)
                {
                    MessageBox.Show("Invalid order selection.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                // Resolve TableReport and pass the dates
                var tableReport = _container.Resolve<TableReport>();

                if (tableReport.CreateTable(filePath, startDate, endDate))
                {
                    MessageBox.Show("< Chart PDF report generated successfully >", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred while generating the report: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}