using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mesnac.Basic.Service
{
    /// <summary>
    /// 事件处理器
    /// </summary>
    public class EventHandlerProcessor
    {
        #region 字段定义

        private object _oldValue;           //保存事件源原始值

        #endregion

        #region 构造方法

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="oldValue">传递事件源原始值</param>
        public EventHandlerProcessor(object oldValue)
        {
            this._oldValue = oldValue;
        }

        #endregion

        #region 事件处理

        #region 文本框事件处理

        /// <summary>
        /// 文本框事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        public void TextBox_LostFocus(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            string oldStr = String.IsNullOrEmpty(this._oldValue as string) ? String.Empty : this._oldValue.ToString();
            if (textBox.Text.Equals(oldStr))
            {
                return;
            }
            OperationCommand cmd = new OperationCommand(textBox, textBox.Text, oldStr, this.TextBox_LostFocus);
            UndoRedoService.UndoStack.Push(cmd);
            this._oldValue = textBox.Text;
        }

        #endregion

        #region 复选框事件处理

        /// <summary>
        /// 复选框事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        public void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            bool oldValue = false;
            if (this._oldValue != null)
            {
                bool.TryParse(this._oldValue.ToString(), out oldValue);
            }
            if (checkBox.Checked == oldValue)
            {
                return;
            }
            OperationCommand cmd = new OperationCommand(checkBox, checkBox.Checked, oldValue, this.CheckBox_CheckedChanged);
            UndoRedoService.UndoStack.Push(cmd);
            this._oldValue = checkBox.Checked;
        }

        #endregion

        #region 组合框事件处理

        /// <summary>
        /// 组合框事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        public void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            if (comboBox.SelectedItem == this._oldValue)
            {
                return;
            }
            OperationCommand cmd = new OperationCommand(comboBox, comboBox.SelectedItem, this._oldValue, this.ComboBox_SelectedIndexChanged);
            UndoRedoService.UndoStack.Push(cmd);
            this._oldValue = comboBox.SelectedItem;
        }

        #endregion

        #region 日历事件处理

        /// <summary>
        /// 日历控件事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        public void DateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dateTimePicker = sender as DateTimePicker;
            DateTime oldValue = DateTime.Now;
            if (this._oldValue != null)
            {
                DateTime.TryParse(this._oldValue.ToString(), out oldValue);
            }
            if (String.Format("{0:yyyyMMddHHmmss}", dateTimePicker.Value).Equals(String.Format("{0:yyyyMMddHHmmss}", oldValue)))
            {
                return;
            }
            OperationCommand cmd = new OperationCommand(dateTimePicker, dateTimePicker.Value, this._oldValue, this.DateTimePicker_ValueChanged);
            UndoRedoService.UndoStack.Push(cmd);
            this._oldValue = dateTimePicker.Value;
        }

        #endregion

        #region DataGridView事件处理

        /// <summary>
        /// DataGridView事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        public void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;
            DataTable dtNew = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(dataGridView);
            OperationCommand cmd = new OperationCommand(dataGridView, dtNew, this._oldValue, this.DataGridView_CellValueChanged, e.RowIndex, e.ColumnIndex);
            UndoRedoService.UndoStack.Push(cmd);
            this._oldValue = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(dataGridView);
        }

        #endregion

        #endregion
    }
}