using ProjectPatientAccounting.Entities.Enums;
using ProjectPatientAccounting.Entities;
using ProjectPatientAccounting.Repositories;
namespace ProjectPatientAccounting.Forms;

public partial class FormPatientDiagnosis : Form
{
    private readonly IPatientDiagnosisRepository _patientDiagnosisRepository;
    private int? _patientDiagnosisId;

    public int Id
    {
        set

        {
            try
            {
                var diagnosis = _patientDiagnosisRepository.ReadDiagnosisById(value);
                if (diagnosis == null)
                {
                    throw new
                    InvalidDataException(nameof(diagnosis));
                }
                foreach (PatientDiagnosisStatus elem in Enum.GetValues(typeof(PatientDiagnosisStatus)))
                {
                    if ((elem & diagnosis.PatientDiagnosisStatus) != 0)
                    {
                        checkedListBoxStatusDiagnosis.SetItemChecked(checkedListBoxStatusDiagnosis.Items.IndexOf(
                        elem), true);
                    }
                }
                textBoxNameDiagnosis.Text = diagnosis.Name;
                numericUpDownCodeDiagnosis.Value = diagnosis.DiagnosisCode;
                _patientDiagnosisId = value;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }

    public FormPatientDiagnosis(IPatientDiagnosisRepository patientDiagnosisRepository)
    {
        InitializeComponent();

        _patientDiagnosisRepository = patientDiagnosisRepository ??
            throw new ArgumentNullException(nameof(_patientDiagnosisRepository));

        foreach (var elem in Enum.GetValues(typeof(PatientDiagnosisStatus)))
        {
            checkedListBoxStatusDiagnosis.Items.Add(elem);
        }
    }

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

            if (_patientDiagnosisId.HasValue)
            {
                _patientDiagnosisRepository.UpdateDiagnosis(CreateDiagnosis(_patientDiagnosisId.Value));
            }
            else
            {
                _patientDiagnosisRepository.CreateDiagnosis(CreateDiagnosis(0));
            }

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

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

    private PatientDiagnosis CreateDiagnosis(int id)
    {
        PatientDiagnosisStatus patientDiagnosisStatus = PatientDiagnosisStatus.None;
        foreach (var elem in checkedListBoxStatusDiagnosis.CheckedItems)
        {
            patientDiagnosisStatus |= (PatientDiagnosisStatus)elem;
        }
        return PatientDiagnosis.CreateEntity(id, textBoxNameDiagnosis.Text,
        Convert.ToInt32(numericUpDownCodeDiagnosis.Value), patientDiagnosisStatus);
    }
}

