using Microsoft.Extensions.Logging;
using ShipyardContracts.BusinessLogicsContracts;
using ShipyardContracts.ViewModels;
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 ShipyardView
{
    public partial class FormSellShips : Form
    {
        private readonly ILogger _logger;
        private readonly IShopLogic _shopLogic;
        private readonly IShipLogic _shipLogic;
        private readonly List<ShipViewModel>? _listShip;
        public FormSellShips(ILogger<FormSellShips> logger, IShopLogic shopLogic, IShipLogic shipLogic)
        {
            InitializeComponent();
            _logger = logger;
            _shopLogic = shopLogic;
            _shipLogic = shipLogic;
            _listShip = shipLogic.ReadList(null);
            if (_listShip != null) 
            {
                comboBoxShips.DisplayMember = "ShipName";
                comboBoxShips.ValueMember = "Id";
                comboBoxShips.DataSource=_listShip;
                comboBoxShips.SelectedItem = null;
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            if (comboBoxShips.SelectedValue == null)
            {
                MessageBox.Show("Выберите корабль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(numericUpDownCount.Text))
            {
                MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _logger.LogInformation("Продажа поездок");
            try
            {
                var ship = _shipLogic.ReadElement(new()
                {
                    Id = (int)comboBoxShips.SelectedValue
                });
                if (ship == null)
                {
                    throw new Exception("Корабль не найден. Дополнительная информация в логах.");
                }
                var operationResult = _shopLogic.SellShips(
                    model: ship,
                    count: (int)numericUpDownCount.Value
                );
                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();
        }
    }
}
