using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;

namespace SushiBar
{
    public partial class FormSushiMoreThenOne : Form
    {
        private readonly ILogger _logger;
        private readonly ISushiLogic _logic;
        public FormSushiMoreThenOne(ILogger<FormSushiMoreThenOne> logger, ISushiLogic logic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = logic;
        }

        private void LoadData()
        {
            try
            {
                var list = _logic.ReadList(null);
                if (list != null)
                {
                    dataGridView.DataSource = list;
                    dataGridView.Columns["Id"].Visible = false;
                    dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dataGridView.Columns["SushiComponents"].Visible = false;
                }
                _logger.LogInformation("Load sushi");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error on load sushi");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            var service = Program.ServiceProvider?.GetService(typeof(FormSushi));
            if (service is FormSushi form)
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void ButtonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                var service = Program.ServiceProvider?.GetService(typeof(FormSushi));
                if (service is FormSushi form)
                {
                    form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
            }
        }

        private void ButtonRemove_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("Delete record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
                    _logger.LogInformation("Delete sushi");
                    try
                    {
                        if (!_logic.Delete(new SushiBindingModel { Id = id }))
                        {
                            throw new Exception("Error on deleting. Additional info below.");
                        }
                        LoadData();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error delete sushi");
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void ButtonReload_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void FormSushiMoreThenOne_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
