using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using C1.Silverlight.FlexGrid;

namespace C1.Silverlight.FlexGridExcel
{
    /// <summary>
    /// Undoable Paste action
    /// </summary>
    public class PasteAction : IUndoableAction
    {
        //-------------------------------------------------------------------------
        #region ** fields

        C1FlexGrid _flex;
        CellRange _oldSel, _newSel;
        List<PasteCellInfo> _pasteCells = new List<PasteCellInfo>();

        #endregion

        //-------------------------------------------------------------------------
        #region ** ctor

        /// <summary>
        /// Initializes a new instance of an <see cref="SortAction"/>.
        /// </summary>
        /// <param name="sender"><see cref="C1FlexGrid"/> that owns the action.</param>
        public PasteAction(object sender)
        {
            _flex = sender as C1FlexGrid;
            _oldSel = _newSel = _flex.Selection;
        }

        #endregion

        //-------------------------------------------------------------------------
        #region ** object model

        /// <summary>
        /// Adds information about an individual cell changing within a paste operation.
        /// </summary>
        public void PasteCell(int row, int col, object oldValue, object newValue)
        {
            _pasteCells.Add(new PasteCellInfo(row, col, oldValue, newValue));
        }
        /// <summary>
        /// Finishes the paste operation.
        /// </summary>
        public void FinishPasting()
        {
            _newSel = _flex.Selection;
        }

        #endregion

        //-------------------------------------------------------------------------
        #region ** IUndoableAction

        /// <summary>
        /// Reverts the effect of the last action.
        /// </summary>
        public void Undo()
        {
            foreach (var pc in _pasteCells)
            {
                _flex[pc.Row, pc.Col] = pc.OldValue;
            }
            _flex.Selection = _oldSel;
        }
        /// <summary>
        /// Restores the effect of the last undone action.
        /// </summary>
        public void Redo()
        {
            foreach (var pc in _pasteCells)
            {
                _flex[pc.Row, pc.Col] = pc.NewValue;
            }
            _flex.Selection = _newSel;
        }

        #endregion

        //-------------------------------------------------------------------------
        #region ** implementation

        struct PasteCellInfo
        {
            public int Row, Col;
            public object OldValue, NewValue;
            public PasteCellInfo(int row, int col, object oldValue, object newValue)
            {
                Row = row;
                Col = col;
                OldValue = oldValue;
                NewValue = newValue;
            }
        }

        #endregion
    }
}
