using ProjectPatientAccounting.Repositories;
using Unity;
namespace ProjectPatientAccounting.Forms;

public partial class FormPatientDiagnosises : Form
{
    private readonly IUnityContainer _container;
    private readonly IPatientDiagnosisRepository _patientDiagnosisRepository;

    public FormPatientDiagnosises(IUnityContainer container, IPatientDiagnosisRepository patientDiagnosisRepository)
    {
        InitializeComponent();

        _container = container ??
            throw new ArgumentNullException(nameof(container));
        _patientDiagnosisRepository = patientDiagnosisRepository ??
            throw new ArgumentNullException(nameof(patientDiagnosisRepository));
    }

    private void FormPatientDiagnosises_Load(object sender, EventArgs e)
    {
        try
        {
            LoadList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Ошибка при загрузке",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void LoadList() => dataGridViewPatientDiagnosises.DataSource =
        _patientDiagnosisRepository.ReadDiagnosises();

    private bool TryGetIdentifierFromSelectedRow(out int id)
    {
        id = 0;
        if (dataGridViewPatientDiagnosises.SelectedRows.Count < 1)
        {
            MessageBox.Show("Нет выбранной записи", "Ошибка",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
        id = Convert.ToInt32(dataGridViewPatientDiagnosises.SelectedRows[0].Cells["Id"].Value);
        return true;
    }

    private void ButtonAdd_Click(object sender, EventArgs e)
    {
        try
        {
            _container.Resolve<FormPatientDiagnosis>().ShowDialog();
            LoadList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Ошибка при добавлении",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void ButtonUpd_Click(object sender, EventArgs e)
    {
        if (!TryGetIdentifierFromSelectedRow(out var findId))
        {
            return;
        }
        try
        {
            var form = _container.Resolve<FormPatientDiagnosis>();
            form.Id = findId;
            form.ShowDialog();
            LoadList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Ошибка при изменении",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void ButtonDel_Click(object sender, EventArgs e)
    {
        if (!TryGetIdentifierFromSelectedRow(out var findId))
        {
            return;
        }
        if (MessageBox.Show("Удалить запись?", "Удаление",
        MessageBoxButtons.YesNo) != DialogResult.Yes)
        {
            return;
        }
        try
        {
            _patientDiagnosisRepository.DeleteDiagnosis(findId);
            LoadList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Ошибка при удалении",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

