using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SushiBar
{
    public partial class FormImplementer : Form
    {
        private readonly ILogger _logger;
        private readonly IImplementerLogic _logic;
        private int? _id;
        public int Id { set { _id = value; } }

        public FormImplementer(ILogger<FormImplementer> logger, IImplementerLogic logic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = logic;
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxPassword.Text))
            {
                MessageBox.Show("Fill password", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(textBoxFio.Text))
            {
                MessageBox.Show("Fill fio", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Saving");
            try
            {
                var model = new ImplementerBindingModel
                {
                    Id = _id ?? 0,
                    ImplementerFio = textBoxFio.Text,
                    Password = textBoxPassword.Text,
                    Qualification = (int)numericUpDownQul.Value,
                    WorkExperience = (int)numericUpDownExp.Value,
                };
                var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
                if (!operationResult)
                {
                    throw new Exception("Error on saving. Additional info below");
                }
                MessageBox.Show("Good", "Info",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error on save");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            }
        }

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

        private void FormImplementer_Load(object sender, EventArgs e)
        {
            if (_id.HasValue)
            {
                try
                {
                    _logger.LogInformation("Getting implementer");
                    var view = _logic.ReadElement(new ImplementerSearchModel
                    {
                        Id = _id.Value
                    });
                    if (view != null)
                    {
                        textBoxFio.Text = view.ImplementerFio;
                        textBoxPassword.Text = view.Password;
                        numericUpDownQul.Value = view.Qualification;
                        numericUpDownExp.Value = view.WorkExperience;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error on getting implementer");
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                }
            }
        }
    }
}
