using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Model;

namespace Spread_WF_Delete_Content
{
    public class ClearSelectedCellsUndoAction : FarPoint.Win.Spread.UndoRedo.UndoAction
    {
        SpreadView spreadView = null;                         // SpreadView where action happens
        FarPoint.Win.Spread.SheetView activeSheet = null;     // active SheetView in root workbook
        FarPoint.Win.Spread.SheetView sheet = null;           // SheetView where action happens 
        FarPoint.Win.Spread.Model.CellRange cellRange = null; // CellRange being cleared
        DataObject clipData = null;                           // DataObject containing data from CellRange

        public override bool PerformUndoAction(object sender)
        {
            SpreadView rootWorkbook = sender as SpreadView;     // sender is always root SpreadView when called from UI

            if (rootWorkbook != null) // but sender might be null if other code calls this, so check!
            {
                activeSheet = rootWorkbook.Sheets[rootWorkbook.ActiveSheetIndex]; // save active SheetView (to restore on undo)
                spreadView = rootWorkbook.GetActiveWorkbook();                    // save active SpreadView (to restore on undo)
                sheet = spreadView.GetSheetView();                                // get SheetView to operate on
                cellRange = sheet.GetSelection(sheet.SelectionCount - 1);         // get CellRange to operate on
                if (cellRange == null)  // GetSelection return null if there is no selection, just active cell
                    cellRange = new CellRange(sheet.ActiveRowIndex, sheet.ActiveColumnIndex, 1, 1); // use active cell in this case
            }
            if (SaveUndoState()) // save data from range
            { // then clear it
                sheet.ClearRange(cellRange.Row, cellRange.Column, cellRange.RowCount, cellRange.ColumnCount, false);
                return true;
            }
            return false; // something failed, return false to discard action from undo stack
        }

        protected override bool SaveUndoState()
        {
            if (cellRange != null) // need CellRange set in PerformUndoAction (implied sheet is valid)
                clipData = sheet.GetClipDataObject(false, cellRange, ClipboardCopyOptions.All); // save data object for cell range
            return clipData != null;
        }

        public override bool Undo(object sender)
        {
            SpreadView rootWorkbook = sender as SpreadView;
            if (rootWorkbook != null)
            {
                rootWorkbook.ActiveSheetIndex = rootWorkbook.Sheets.IndexOf(activeSheet); // restore active sheet (in case user changed it)
                rootWorkbook.SetActiveWorkbook(spreadView); // restore active workbook in sheet (in case user changed it)
                sheet.ClearSelection(); // reset selection (clear, then add)
                sheet.AddSelection(cellRange.Row, cellRange.Column, cellRange.RowCount, cellRange.ColumnCount);
                sheet.ClipboardPaste(ClipboardPasteOptions.All, clipData); // paste data pack from data object
                return true;
            }
            return false;
        }
    }
}
