using RealEstateTransactions.Entities;
using RealEstateTransactions.Repositories;

namespace RealEstateTransactions.Forms
{
    public partial class FormPreSalesService : Form
    {
        private readonly IPreSalesServicesRepository _repository;

        public FormPreSalesService(IPreSalesServicesRepository repository, IApartmentRepository apartmentRepository)
        {
            InitializeComponent();
            _repository = repository ?? throw new ArgumentNullException(nameof(repository));

            comboBoxApartmentId.DataSource = apartmentRepository.ReadApartments();
            comboBoxApartmentId.DisplayMember = "Id";
            comboBoxApartmentId.ValueMember = "Id";
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxApartmentId.SelectedItem == null || string.IsNullOrWhiteSpace(textBoxName.Text))
                {
                    throw new Exception("Имеются незаполненные поля");
                }

                _repository.CreatePreSalesService(PreSalesServices.CreatePreSalesServices(0, (int)comboBoxApartmentId.SelectedValue!,
                    textBoxName.Text, (float)numericUpDownCost.Value));

                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e) => Close();
    }
}