using System;
using System.Windows.Forms;

namespace Sgry.Ann
{
	using Azuki;

	static partial class Actions
	{
		/// <summary>
		/// UNDO last operation.
		/// </summary>
		public static AnnAction Undo
			= delegate( AppLogic app )
		{
			app.Azuki.Undo();
		};

		/// <summary>
		/// Execute again most recently UNDOed operation.
		/// </summary>
		public static AnnAction Redo
			= delegate( AppLogic app )
		{
			app.Azuki.Redo();
		};

		/// <summary>
		/// Cuts currently selected text or current line if nothing selected.
		/// </summary>
		public static AnnAction Cut
			= delegate( AppLogic app )
		{
			app.Azuki.Cut();
		};

		/// <summary>
		/// Copies currently selected text or current line if nothing selected.
		/// </summary>
		public static AnnAction Copy
			= delegate( AppLogic app )
		{
			app.Azuki.Copy();
		};

		/// <summary>
		/// Pastes clipboard text and replace to currently selected text.
		/// </summary>
		public static AnnAction Paste
			= delegate( AppLogic app )
		{
			app.Azuki.Paste();
		};

		/// <summary>
		/// Shows find dialog.
		/// </summary>
		public static AnnAction ShowFindDialog
			= delegate( AppLogic app )
		{
			app.MainForm.ActivateSearchPanel();
		};

		/// <summary>
		/// Finds next matching pattern.
		/// </summary>
		public static AnnAction FindNext
			= delegate( AppLogic app )
		{
			// set text pattern to emphasize
			app.UpdateWatchPatternForTextSearch();

			// seek to next occurrence
			app.FindNext();
		};

		/// <summary>
		/// Finds previous matching pattern.
		/// </summary>
		public static AnnAction FindPrev
			= delegate( AppLogic app )
		{
			// set text pattern to emphasize
			app.UpdateWatchPatternForTextSearch();

			// seek to previous occurrence
			app.FindPrev();
		};

		/// <summary>
		/// Shows "GotoLine" dialog.
		/// </summary>
		public static AnnAction ShowGotoDialog
			= delegate( AppLogic app )
		{
			using( GotoForm form = new GotoForm() )
			{
				IUserInterface azuki = app.Azuki;
				form.LineNumber = azuki.GetLineIndexFromCharIndex( azuki.CaretIndex ) + 1;
				DialogResult result = form.ShowDialog();
				if( result == DialogResult.OK
					&& form.LineNumber < azuki.LineCount )
				{
					int index = azuki.GetLineHeadIndex( form.LineNumber - 1 );
					azuki.Select( index, index );
					azuki.ScrollToCaret();
				}
			}
		};

		/// <summary>
		/// Removes whitespaces at end of each line.
		/// </summary>
		public static AnnAction TrimTrailingSpace
			= delegate( AppLogic app )
		{
			Sgry.Azuki.Actions.TrimTrailingSpace( app.Azuki );
		};

		/// <summary>
		/// Removes whitespaces at beginning of each line.
		/// </summary>
		public static AnnAction TrimLeadingSpace
			= delegate( AppLogic app )
		{
			Sgry.Azuki.Actions.TrimLeadingSpace( app.Azuki );
		};

		/// <summary>
		/// Convertes tab characters in selection to equivalent amount of
		/// spaces.
		/// </summary>
		public static AnnAction ConvertTabsToSpaces
			= delegate( AppLogic app )
		{
			Sgry.Azuki.Actions.ConvertTabsToSpaces( app.Azuki );
		};

		/// <summary>
		/// Convertes space characters in selection to tab characters as much 
		/// as possible.
		/// </summary>
		public static AnnAction ConvertSpacesToTabs
			= delegate( AppLogic app )
		{
			Sgry.Azuki.Actions.ConvertSpacesToTabs( app.Azuki );
		};

		/// <summary>
		/// Selects all text.
		/// </summary>
		public static AnnAction SelectAll
			= delegate( AppLogic app )
		{
			app.Azuki.SelectAll();
		};

		/// <summary>
		/// Sets EOL code for input to CR+LF
		/// and unify existing EOL code to CR+LF if user choses so.
		/// </summary>
		public static AnnAction SetEolCodeToCRLF
			= delegate( AppLogic app )
		{
			app.SetEolCode( "\r\n" );
		};

		/// <summary>
		/// Sets EOL code for input to LF
		/// and unify existing EOL code to LF if user choses so.
		/// </summary>
		public static AnnAction SetEolCodeToLF
			= delegate( AppLogic app )
		{
			app.SetEolCode( "\n" );
		};

		/// <summary>
		/// Sets EOL code for input to CR
		/// and unify existing EOL code to CR if user choses so.
		/// </summary>
		public static AnnAction SetEolCodeToCR
			= delegate( AppLogic app )
		{
			app.SetEolCode( "\r" );
		};
	}
}
