using System;
using NotepadEnhanced.GUI.Forms;

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

        public GotoPanel(MainForm frmMain)
        {
            InitializeComponent();
            _frmMain = frmMain;
            _frmMain.AcceptButton = btnGoto;
            _frmMain.PtbActive.TextChanged += _activePTB_TextChanged;
            nudLineNumber.Maximum = _frmMain.PtbActive.Lines.Length;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            nudLineNumber.Focus();
        }

        private void _activePTB_TextChanged(object sender, EventArgs e)
        {
            nudLineNumber.Maximum = _frmMain.PtbActive.Lines.Length;
        }

        private void btnGoto_Click(object sender, EventArgs e)
        {
            int lineIndex = (int)nudLineNumber.Value - 1;
            _frmMain.PtbActive.GoTo(lineIndex);
            _frmMain.PtbActive.Focus();
        }

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

        private void btnGotoTop_Click(object sender, EventArgs e)
        {
            _frmMain.PtbActive.SelectionStart = 0;
            _frmMain.PtbActive.Focus();
        }

        private void btnGotoBottom_Click(object sender, EventArgs e)
        {
            _frmMain.PtbActive.SelectionStart = _frmMain.PtbActive.TextLength
                - _frmMain.PtbActive.SelectionLength;

            _frmMain.PtbActive.Focus();
        }
    }
}
