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

namespace SushiBar
{
    public partial class FormCreateOrder : Form
    {
        private readonly ILogger _logger;
        private readonly ISushiLogic _logicP;
        private readonly IOrderLogic _logicO;
        private readonly IClientLogic _logicC;

        public FormCreateOrder(
            ILogger<FormCreateOrder> logger, 
            ISushiLogic logicP, 
            IOrderLogic logicO, 
            IClientLogic logicC)
        {
            InitializeComponent();
            _logger = logger;
            _logicP = logicP;
            _logicO = logicO;
            _logicC = logicC;
        }

        private void CalcSum()
        {
            if (comboBoxSushi.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text))
            {
                try
                {
                    int id = Convert.ToInt32(comboBoxSushi.SelectedValue);
                    var sushi = _logicP.ReadElement(new SushiSearchModel { Id = id });
                    int count = Convert.ToInt32(textBoxCount.Text);
                    textBoxSum.Text = Math.Round(count * (sushi?.Price ?? 0), 2).ToString();
                    _logger.LogInformation("Calculating sum of order");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error on calculating sum of order");
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void LoadData()
        {
            try
            {
                var list = _logicP.ReadList(null);
                if (list != null)
                {
                    comboBoxSushi.DisplayMember = "SushiName";
                    comboBoxSushi.ValueMember = "Id";
                    comboBoxSushi.DataSource = list;
                    comboBoxSushi.SelectedItem = null;
                }
                var clientList = _logicC.ReadList(null);
                if (clientList != null)
                {
                    comboBoxClients.DisplayMember = "ClientFio";
                    comboBoxClients.ValueMember = "Id";
                    comboBoxClients.DataSource = clientList;
                    comboBoxClients.SelectedItem = null;
                }

            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error on loading sushi or client");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCount.Text))
            {
                MessageBox.Show("Fill in text box count", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBoxSushi.SelectedValue == null)
            {
                MessageBox.Show("Select sushi", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Create order");
            try
            {
                var operationResult = _logicO.CreateOrder(new OrderBindingModel
                {
                    SushiId = Convert.ToInt32(comboBoxSushi.SelectedValue),
                    ClientId = Convert.ToInt32(comboBoxClients.SelectedValue),
                    SushiName = comboBoxSushi.Text,
                    Count = Convert.ToInt32(textBoxCount.Text),
                    Sum = Convert.ToDouble(textBoxSum.Text),
                    ImplementerId = null
                }) ;
                if (!operationResult)
                {
                    throw new Exception("Error on creating order. Additional info below.");
                }
                MessageBox.Show("Saving is successful", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error on saving");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void FormCreateOrder_Load(object sender, EventArgs e)
        {
            _logger.LogInformation("Load sushi for order");
            LoadData();
        }

        private void TextBoxCount_TextChanged(object sender, EventArgs e)
        {
            CalcSum();
        }

        private void ComboBoxSushi_SelectedIndexChanged(object sender, EventArgs e)
        {
            CalcSum();
        }
    }
}
