using System;
using System.Windows.Forms;

namespace NotepadEnhanced
{
    class Fade : IDisposable
    {
        private uint _fadeOutSpeed = 10;
        private uint _fadeInSpeed = 15;
        private float _fadeOutTo = 0f;
        private float _fadeInTo = 1f;
        private float _fadRes = 0.05f;
        private Form _form;
        private Timer _tmrFadeIn = new Timer();
        private Timer _tmrFadeOut = new Timer();

        public void SetupTimer()
        {
            _tmrFadeIn.Interval = (int)_fadeInSpeed;
            _tmrFadeOut.Interval = (int)_fadeOutSpeed;
            _tmrFadeIn.Tick += new EventHandler(FadeIn_Tick);
            _tmrFadeOut.Tick += new EventHandler(FadeOut_Tick);
        }

        /// <summary>
        /// Creates an instance of the fade class
        /// </summary>
        /// <param name="form">the form to be faded</param>
        public Fade(Form form)
        {
            SetupTimer();
            _form = form;
        }

        /// <summary>
        /// Initiate fade-in effect
        /// </summary>
        public void StartFadeIn()
        {
            _tmrFadeOut.Stop();
            _tmrFadeIn.Start();
        }

        /// <summary>
        /// Initiate fade-in effect
        /// </summary>
        //public void StartFadeOut()
        //{
        //    _tmrFadeIn.Stop();
        //    _tmrFadeOut.Start();
        //}

        private void FadeIn_Tick(object sender, EventArgs e)
        {
            _form.Opacity += _fadRes;

            if (_form.Opacity >= _fadeInTo)
            {
                _tmrFadeIn.Stop();
                _form.Opacity = _fadeInTo;
                this.Dispose();
            }
        }

        private void FadeOut_Tick(object sender, EventArgs e)
        {
            _form.Opacity -= _fadRes;

            if (_form.Opacity <= _fadeOutTo)
            {
                _tmrFadeOut.Stop();
                _form.Opacity = _fadeOutTo;
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _tmrFadeIn.Dispose();
            _tmrFadeOut.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
