using System;
using System.Drawing;
using System.Text.RegularExpressions;
using NotepadEnhanced.GUI.Forms;
using NotepadEnhanced.Localization;

namespace NotepadEnhanced.GUI.Controls.Panels
{
    public partial class ReplacePanel : LargePanelBase
    {
        private readonly MainForm _frmMain;
        private bool _findingNext;

        public ReplacePanel(MainForm frmMain)
        {
            InitializeComponent();
            _frmMain = frmMain;
            _frmMain.PtbActive.SelectionLengthChanged += PtbActive_SelectionLengthChanged;
            _frmMain.PtbActive.SelectionChanged += PtbActive_SelectionChanged;
        }

        private void PtbActive_SelectionChanged(object sender, EventArgs e)
        {
            btnReplaceSelected.Enabled = !string.IsNullOrEmpty(_frmMain.PtbActive.SelectedText);
        }

        private void PtbActive_SelectionLengthChanged(object sender, EventArgs e)
        {
            if (_findingNext)
            {
                _findingNext = false;
                return;
            }

            txtFind.Text = _frmMain.PtbActive.SelectedText;
        }

        private void txtFind_TextChanged(object sender, EventArgs e)
        {
            bool isEnabled = txtFind.TextLength > 0;
            btnPnlFindNext.Enabled = isEnabled;
            btnReplaceAll.Enabled = isEnabled;
            txtFind.BackColor = Color.FromKnownColor(KnownColor.Window);
        }

        public void SetTextBoxFocus()
        {
            if (txtFind.TextLength > 0)
            {
                txtReplace.Select();
            }
            else
            {
                txtFind.Select();
            }
        }

        private void btnPnlFindNext_Click(object sender, EventArgs e)
        {
            _findingNext = true;
            bool isFinished = _frmMain.PtbActive.FindNext(txtFind.Text, chkMatchCase.Checked);

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

        private void btnReplace_Click(object sender, EventArgs e)
        {
            _frmMain.PtbActive.SelectedText = txtReplace.Text;
        }

        private void btnReplaceAll_Click(object sender, EventArgs e)
        {
            RegexOptions options = (chkMatchCase.Checked)
                ? RegexOptions.IgnoreCase : RegexOptions.None;

            DisplayReplaceCount(options);

            _frmMain.PtbActive.Text = Regex.Replace(_frmMain.PtbActive.Text,
                txtFind.Text, txtReplace.Text, options);
        }

        private void DisplayReplaceCount(RegexOptions options)
        {
            MatchCollection MC = Regex.Matches(_frmMain.PtbActive.Text, txtFind.Text, options);
            lblReplaceCount.Text = MC.Count + " " + Strings.ItemsReplaced;
        }

        public void SetFindTextBoxBackColor(Color color)
        {
            txtFind.BackColor = color;
        }

        private void txtFind_Enter(object sender, EventArgs e)
        {
            _frmMain.AcceptButton = btnPnlFindNext;
        }

        private void txtReplace_Enter(object sender, EventArgs e)
        {
            _frmMain.AcceptButton = btnReplaceAll;
        }

        public string FindText
        {
            get { return txtFind.Text; }
            set { txtFind.Text = value; }
        }
    }
}
