using Darkmatter.Core.Contracts.Features.History;
using Darkmatter.Features.ShapeBuilder.UI;
using UnityEngine;

namespace Darkmatter.Features.ShapeBuilder.Commands
{
    /// <summary>
    /// Undoable snap. Captures the piece's pre-snap pose + parent at ctor time;
    /// Execute drives the normal snap tween; Undo restores the pre-snap state
    /// and re-enables drag.
    /// </summary>
    internal sealed class SnapPieceCommand : ICommand
    {
        private readonly ShapePiece _piece;
        private readonly Vector2 _prevPos;
        private readonly Vector2 _prevSize;
        private readonly Quaternion _prevRot;
        private readonly Transform _prevParent;

        public SnapPieceCommand(ShapePiece piece)
        {
            _piece = piece;

            var rt = piece.RectTransform;
            _prevPos    = rt.anchoredPosition;
            _prevSize   = rt.sizeDelta;
            _prevRot    = rt.localRotation;
            _prevParent = rt.parent;
        }

        public void Execute() => _piece.SnapInternal();

        public void Undo() => _piece.UnsnapInternal(_prevParent, _prevPos, _prevSize, _prevRot);
    }
}
