using System;
using System.Drawing;
using NotepadEnhanced.AppState;
using NotepadEnhanced.GUI.Forms;

namespace NotepadEnhanced.GUI.Controls.Panels
{
    public partial class FindPanel : SmallPanelBase
    {
        private readonly MainForm _frmMain;

        public FindPanel(MainForm frmMain)
        {
            InitializeComponent();
            chkMatchCase.Checked = UserSettings.Instance.IsFindMatchCase;
            _frmMain = frmMain;
            _frmMain.AcceptButton = btnFindNext;
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (!this.Visible)
                _frmMain.AcceptButton = null;
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            _frmMain.AcceptButton = btnFindNext;
        }

        private void btnFindNext_Click(object sender, EventArgs e)
        {
            bool isFinished = _frmMain.PtbActive.FindNext(txtPattern.Text, chkMatchCase.Checked);

            if (isFinished)
            {
                txtPattern.BackColor = Program.SearchDoneColor;
            }
            else
            {
                txtPattern.BackColor = Color.FromKnownColor(KnownColor.Window);
            }
        }

        private void txtPattern_TextChanged(object sender, EventArgs e)
        {
            btnFindNext.Enabled = txtPattern.Text.Length > 0;
            txtPattern.BackColor = Color.FromKnownColor(KnownColor.Window);
        }

        private void chkMatchCase_Click(object sender, EventArgs e)
        {
            UserSettings.Instance.IsFindMatchCase = chkMatchCase.Checked;
        }

        public HotTrackedTextBox PatternBox
        {
            get { return txtPattern; }
        }
    }
}