using System;
using System.Windows.Forms;
using System.Drawing;
using NotepadEnhanced.Properties;
using NotepadEnhanced.Localization;

namespace NotepadEnhanced.GUI.Controls
{
    class PromptButton : Button
    {
        private readonly Timer _tmrAutoHide = new Timer();
        private readonly TabControl _tabControl;
        private int _timeShown;
        private const int _CORNER_PADDING = 10;
        private const int _TIME_TO_SHOW = 4;

        public PromptButton(TabControl tabControl)
        {
            _tabControl = tabControl;
            tabControl.Resize +=tabControl_Resize;
            this.Padding = new Padding(3);
            this.Image = Resources.Restore;
            this.TextImageRelation = TextImageRelation.ImageBeforeText;
            base.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            base.Text = Strings.RestoreSessionPrompt;
            base.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            _tmrAutoHide.Interval = 1000;
            _tmrAutoHide.Tick += _tmrAutoHide_Tick;
            _tmrAutoHide.Start();
            PositionButton();
        }

        private void tabControl_Resize(object sender, EventArgs e)
        {
            PositionButton();
            this.BringToFront();
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.BringToFront();
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            this.Dispose();
        }

        private void PositionButton()
        {
            int xPos = _tabControl.Location.X + _tabControl.ClientSize.Width - this.Width - _CORNER_PADDING;
            int yPos = _tabControl.Location.Y + _tabControl.ClientSize.Height - this.Height - _CORNER_PADDING;
            this.Location = new Point(xPos, yPos);
        }

        private void _tmrAutoHide_Tick(object sender, EventArgs e)
        {
            _timeShown++;

            if (_timeShown.Equals(_TIME_TO_SHOW))
            {
                this.Dispose();
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _tmrAutoHide.Dispose();
        }
    }
}
