using System.Drawing;
using System.Windows.Forms;

namespace ToyStoreApp;

public sealed partial class ProductDetailsForm : Form
{
    private readonly ProductView _product;

    public ProductDetailsForm(ProductView product)
    {
        _product = product;
        InitializeComponent();
        FillProduct();
    }

    private void ProductDetailsForm_Load(object? sender, EventArgs e)
    {
        LoadProductImage();
    }

    private void CloseButton_Click(object? sender, EventArgs e)
    {
        Close();
    }

    private void FillProduct()
    {
        Text = $"Карточка товара - {_product.Name}";
        titleLabel.Text = $"{_product.Category} | {_product.Name}";
        descriptionValueLabel.Text = string.IsNullOrWhiteSpace(_product.Description) ? "Нет описания" : _product.Description;
        manufacturerValueLabel.Text = _product.Manufacturer;
        supplierValueLabel.Text = _product.Supplier;
        priceValueLabel.Text = $"{_product.Price:N2} руб.";
                if (_product.DiscountPercent > 0)
                {
                    priceValueLabel.Font = new Font(priceValueLabel.Font, FontStyle.Strikeout);
                    priceValueLabel.ForeColor = Color.Red;
                }
                else
                {
                    priceValueLabel.Font = new Font(priceValueLabel.Font, FontStyle.Regular);
                    priceValueLabel.ForeColor = Color.Black;
                }
                OriginalPriceBindingLabel.Visible = false;
        unitValueLabel.Text = _product.Unit;
        stockValueLabel.Text = _product.StockQuantity.ToString();
        articleValueLabel.Text = _product.Article;
        DiscountValueBindingLabel.Text = $"{_product.DiscountPercent:N2}%";
        finalPriceValueLabel.Text = $"{_product.FinalPrice:N2} руб.";
    }

    private void LoadProductImage()
    {
        var path = Path.Combine(AppContext.BaseDirectory, "Images", _product.ImagePath);
        if (!File.Exists(path))
        {
            path = Path.Combine(AppContext.BaseDirectory, "Images", "picture.png");
        }

        productPictureBox.Image = File.Exists(path) ? Image.FromFile(path) : null;
        photoPlaceholderLabel.Visible = productPictureBox.Image is null;
    }
}