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.Collections.Generic;
using C1.Silverlight.FlexGrid;
using C1.Silverlight.FlexGridBook;

namespace ExcelBook
{
    public class CellStyleChangeAction : FlexGridBookAction
    {
        Dictionary<CellRange, CellStyle> _oldStyles, _newStyles;

        public CellStyleChangeAction(C1FlexGridBook flex) : base(flex)
        {
            _oldStyles = new Dictionary<CellRange, CellStyle>();
            SaveStyles(_oldStyles);
        }
        override public bool SaveNewState()
        {
            _newStyles = new Dictionary<CellRange, CellStyle>();
            SaveStyles(_newStyles);
            return true;
        }
        override public void Undo()
        {
            ApplyStyles(_oldStyles);
        }
        override public void Redo()
        {
            ApplyStyles(_newStyles);
        }
        void SaveStyles(Dictionary<CellRange, CellStyle> styles)
        {
            foreach (var rng in _flex.Selection.Cells)
            {
                var row = _flex.Rows[rng.Row] as ExcelRow;
                var col = _flex.Columns[rng.Column];
                styles[rng] = row.GetCellStyle(col);
            }
        }
        void ApplyStyles(Dictionary<CellRange, CellStyle> styles)
        {
            foreach (var rng in styles.Keys)
            {
                var row = _flex.Rows[rng.Row] as ExcelRow;
                var col = _flex.Columns[rng.Column];
                row.SetCellStyle(col, styles[rng]);
            }
        }
    }
}
