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

namespace DDUtilityApp.LOGPARSER
{

    public partial class FrmFindDialog : Form
    {

        public event EventHandler FInd;

        public Control Control { get; set; } = null;

        public string SelectedText
        {
            get { return this.cboxFind.Text; }
            set { this.cboxFind.Text = value; }
        }

        public FrmFindDialog()
        {
            InitializeComponent();
            this.SetLayout();
            this.SetEventHandler();
        }

        public FrmFindDialog(TextBox textBox) : this()
        {
            this.Control = textBox;
        }

        private void SetLayout()
        {
            this.Text = "Find Dialog";
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }

        private void SetEventHandler()
        {
            this.btnNext.Click += BtnNext_Click;
            this.btnPrevious.Click += BtnPrevious_Click;
            this.btnClose.Click += BtnClose_Click;
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            try
            {
                TextBox control = this.Control as TextBox;

                int start = control.SelectionStart + control.SelectionLength;
                int index = control.Text.IndexOf(this.cboxFind.Text, start);
                if (index < 0) return;

                control.SelectionStart = index;
                control.SelectionLength = this.cboxFind.Text.Length;
                control.ScrollToCaret();

                if (this.FInd != null) this.FInd(this, new EventArgs());
                this.ComboBox_ItemAppend();
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        private void BtnPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                TextBox control = this.Control as TextBox;

                int end = control.SelectionStart;
                int index = control.Text.LastIndexOf(this.cboxFind.Text, end);
                if (index < 0) return;

                control.SelectionStart = index;
                control.SelectionLength = this.cboxFind.Text.Length;
                control.ScrollToCaret();

                if (this.FInd != null) this.FInd(this, new EventArgs());
                this.ComboBox_ItemAppend();
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

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

        private void ComboBox_ItemAppend()
        {
            try
            {
                string value = this.cboxFind.Text;
                if (string.IsNullOrWhiteSpace(value)) return;

                this.cboxFind.Items.Remove(value);
                this.cboxFind.Items.Insert(0, value);
                this.cboxFind.Text = value;
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        public void Next(string selectedText = "")
        {
            if (!string.IsNullOrWhiteSpace(selectedText)) this.cboxFind.Text = selectedText;
            this.BtnNext_Click(this.btnNext, new EventArgs());
        }

        public void Pervious(string selectedText = "")
        {
            if (!string.IsNullOrWhiteSpace(selectedText)) this.cboxFind.Text = selectedText;
            this.BtnPrevious_Click(this.btnNext, new EventArgs());
        }

    }

}
