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

namespace TheNovel
{
	partial class Find : Form
	{
		Main main;

		public Find (Main _main)
		{
			InitializeComponent ();
			Langsup.ApplyFind ( this );
			main = _main;
		}

		private void Cancel_Click ( object sender, EventArgs e )
		{
			DialogResult = DialogResult.Cancel;
		}

		private void FindWords_Click ( object sender, EventArgs e )
		{
			if ( Sentence.Text == "" )
			{
				MessageBox.Show ( Langsup.GetWord ( "inputmsg" ), Langsup.GetWord ( "msgtitle" ) );
				return;
			}

			int now = main.Novel.GetIndex ( main.Current );
			for ( int i = now; i < main.Novel.Count; i++ )
			{
				main.ChangeTitle ( main.Novel [ i ] );

				int ind = main.TextEditor.Text.IndexOf ( Sentence.Text, main.TextEditor.SelectionStart );

				if ( ind == 0 )
				{
					if ( AllOfChapter.Checked )
						continue;
					else
						break;
				}
				else
				{
					main.TextEditor.SelectionStart = ( ind <= -1 ) ? 1 : ind;
					main.TextEditor.SelectionLength = Sentence.Text.Length;
					DialogResult = DialogResult.OK;
				}
			}

			DialogResult = DialogResult.Ignore;
		}

		private void ReplaceWords_Click ( object sender, EventArgs e )
		{
			if ( Sentence.Text == "" )
			{
				MessageBox.Show ( Langsup.GetWord ( "inputmsg" ), Langsup.GetWord ( "msgtitle" ) );
				return;
			}

			int now = main.Novel.GetIndex ( main.Current );
			for ( int i = now; i < main.Novel.Count; i++ )
			{
				main.ChangeTitle ( main.Novel [ i ] );

				main.Novel [ i ].Text.Replace ( Sentence.Text, Replace.Text );
				main.TextEditor.Text = main.Novel [ i ].Text;

				if ( AllOfChapter.Checked )
					continue;
				else
					break;
			}

			DialogResult = DialogResult.Cancel;
		}
	}
}
