using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Minint.Core.Models;

/// <summary>
/// A single raster layer. Pixels are indices into the parent document's palette.
/// Array layout is row-major: Pixels[y * width + x].
/// </summary>
public sealed class MinintLayer : INotifyPropertyChanged
{
    private string _name;
    private bool _isVisible;
    private byte _opacity;

    public string Name
    {
        get => _name;
        set { if (_name != value) { _name = value; Notify(); } }
    }

    public bool IsVisible
    {
        get => _isVisible;
        set { if (_isVisible != value) { _isVisible = value; Notify(); } }
    }

    /// <summary>
    /// Per-layer opacity (0 = fully transparent, 255 = fully opaque).
    /// Used during compositing: effective alpha = paletteColor.A * Opacity / 255.
    /// </summary>
    public byte Opacity
    {
        get => _opacity;
        set { if (_opacity != value) { _opacity = value; Notify(); } }
    }

    /// <summary>
    /// Palette indices, length must equal container Width * Height.
    /// Index 0 = transparent by convention.
    /// </summary>
    public int[] Pixels { get; }

    public MinintLayer(string name, int pixelCount)
    {
        _name = name;
        _isVisible = true;
        _opacity = 255;
        Pixels = new int[pixelCount];
    }

    /// <summary>
    /// Constructor for deserialization — accepts a pre-filled pixel buffer.
    /// </summary>
    public MinintLayer(string name, bool isVisible, byte opacity, int[] pixels)
    {
        _name = name;
        _isVisible = isVisible;
        _opacity = opacity;
        Pixels = pixels;
    }

    public event PropertyChangedEventHandler? PropertyChanged;
    private void Notify([CallerMemberName] string? prop = null)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
