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 PDFReader_Demo_CSharp.UI
{
    public partial class SearchDlg : Form
    {
        private MainForm main_form_ = null;
        public SearchDlg(MainForm main_form)
        {
            InitializeComponent();
            main_form_ = main_form;
            this.FindNextButton.Enabled = false;
        }

        private void FindNextButton_Click(object sender, EventArgs e)
        {
            Find();
        }

        private void Find()
        {
            if (main_form_ == null) return;
            int direction = -1;
            if (DownRadioButton.Checked == true)
                direction = 0;
            main_form_.SearchText(searchBox.Text, caseSensitiveCheckBox.Checked, WholeWordsOnlyCheckBox.Checked, direction);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
           this.Close();
        }

        private void SearchDlg_FormClosing(object sender, FormClosingEventArgs e)
        {
            main_form_.EnableButon(true);

            main_form_.StopSearch();
        }

        private void searchBox_TextChanged(object sender, EventArgs e)
        {
            if (!searchBox.Text.Equals(""))
            {
                this.FindNextButton.Enabled = true;
            }
            else
            {
                this.FindNextButton.Enabled = false;
            }

            if (main_form_.IsInSearching())
                main_form_.StopSearch();
        }

        private void caseSensitiveCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            main_form_.StopSearch();
        }

        private void WholeWordsOnlyCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            main_form_.StopSearch();
        }

        private void searchBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            var nKeyCode = e.KeyCode;
            System.Diagnostics.Debug.WriteLine(string.Format("KeyCode: {0}, KeyData: {1}", nKeyCode, e.KeyData));
            if (nKeyCode == Keys.F3)
            {
                Find();
            }
        }
    }
}
