using Data;
using Data.Models;
using Data.Repositories;
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 Laba3
{
    public partial class ProductForm : Form
    {
        private readonly IProductRepository _productRepository;
        private readonly IManufacturerRepository _manufacturerRepository;
        private Product _product;
        private bool _isNewProduct;
        private const int MINL = 3;
        private const int MAXL= 10;

        public ProductForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository, Product product = null)
        {
            InitializeComponent();

            _productRepository = productRepository;
            _manufacturerRepository = manufacturerRepository;


            var manufacturers = _manufacturerRepository.GetAllManufacturers().Select(m => m.Name).ToList();
            // cmbManufacturer.Data = manufacturers;
            foreach (var manufacturer in manufacturers)
            {
                cmbManufacturer.AddItem(manufacturer);
            }

            _product = product;
            _isNewProduct = product == null;
            txtName.MinLength = MINL;
            txtName.MaxLength = MAXL;
            dtpDeliveryDate.DateFormat = "dd MMMM yyyy";

            if (!_isNewProduct)
            {
                txtName.InputValue = _product.Name;
                cmbManufacturer.SelectedValue = _product.ManufacturerName;
                dtpDeliveryDate.Date = _product.DeliveryDate;
                if (_product.Image != null)
                {
                    using (var ms = new MemoryStream(_product.Image))
                    {
                        pbImage.Image = Image.FromStream(ms);
                    }
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (var openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    pbImage.Image = Image.FromFile(openFileDialog.FileName);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.InputValue))
            {
                MessageBox.Show("Name is required.");
                return;
            }

            if (_isNewProduct)
            {
                _product = new Product();
            }

            _product.Name = txtName.InputValue;
            _product.ManufacturerName = cmbManufacturer.SelectedValue.ToString();
            _product.DeliveryDate = dtpDeliveryDate.Date;

            if (pbImage.Image != null)
            {
                using (var ms = new MemoryStream())
                {
                    pbImage.Image.Save(ms, pbImage.Image.RawFormat);
                    _product.Image = ms.ToArray();
                }
            }

            if (_isNewProduct)
            {
                _productRepository.AddProduct(_product);
            }
            else
            {
                _productRepository.UpdateProduct(_product);
            }

            
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}