using SoftwareInstallationContracts.BusinessLogicsContracts;

namespace SoftwareInstallationView
{
    public partial class FormSellPackage : Form
    {
        private readonly IShopLogic _shopLogic; 
        private readonly IPackageLogic _packageLogic; 

        public FormSellPackage(IPackageLogic logic, IShopLogic shopLogic)
        {
            InitializeComponent();
            _packageLogic = logic;
            _shopLogic = shopLogic;
            var list = logic.ReadList(null);
            if (list != null)
            {
                comboBoxPackage.DisplayMember = "PackageName";
                comboBoxPackage.ValueMember = "Id";
                comboBoxPackage.DataSource = list;
                comboBoxPackage.SelectedItem = null;
            }
        }

        private void ButtonSell_Click(object sender, EventArgs e)
        {
            if (comboBoxPackage.SelectedValue == null)
            {
                MessageBox.Show("Выберите изделие", "Ошибка",MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (numericUpDownCount.Value <= 0)
            {
                MessageBox.Show("Количество должно быть больше нуля", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var count = (int)numericUpDownCount.Value;
            var package = _packageLogic.ReadElement(new() { Id = (int)comboBoxPackage.SelectedValue });
            if (package == null || !_shopLogic.SellPackages(package, count))
            {
                MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DialogResult = DialogResult.OK;
            Close();
        }

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