using Contracts.BindingModels;
using Contracts.BusinessLogic;
using Contracts.SearchModel;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

namespace Forms
{
    public partial class FormStatus : Form
    {
        private readonly ILogger _logger;
        private readonly IStatusLogic _logic;
        private int? _id;
        public int Id { set { _id = value; } }

        public FormStatus(ILogger<FormCompany> logger,  IStatusLogic statusLogic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = statusLogic;
        }

        private void FormComponent_Load(object sender, EventArgs e)
        {

            try
            {
                
                    if (_id.HasValue)
                    {
                        _logger.LogInformation("Получение company");

                        var view = _logic.ReadElement(new StatusSM
                        {
                            Id = _id.Value
                        });

                        if (view != null)
                        {
                            TitleTextBox.Text = view.Title;
                            
                        }
                    }



                
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения company");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            }
            
            
        }
       
        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TitleTextBox.Text))
            {
                MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            _logger.LogInformation("Сохранение компонента");

            try
            {
                var model = new StatusBM
                {
                    Id = _id ?? 0,
                    Title = TitleTextBox.Text,
                  
                };

                var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);

                if (!operationResult)
                {
                    throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
                }

                MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка сохранения компонента");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void StatuscomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}