using System;
using System.Windows.Forms;
using System.Drawing;
using NotepadEnhanced.GUI.Forms;

namespace NotepadEnhanced
{
    class SaveAnimation : IDisposable
    {
        private bool _appearing = true;
        private const int _PADDING = 20;
        private int _idleTicks;
        private readonly PictureBox _picBox = new PictureBox();
        private readonly Timer _tmrAnimate = new Timer();
        private readonly Timer _tmrIdle = new Timer();
        private Point _targetPos, _startingPos;
        private readonly MainForm _frmMain;

        public SaveAnimation(MainForm frmMain)
        {
            _tmrIdle.Interval = 1000;
            _tmrIdle.Tick +=_tmrIdle_Tick;
            _frmMain = frmMain;
            _picBox.Visible = false;
            _frmMain.Controls.Add(_picBox);
            _picBox.Image = Properties.Resources.Saved;
            _picBox.Size = _picBox.Image.Size;
            _picBox.BackColor = Color.Transparent;
            _tmrAnimate.Interval = 1;
            _tmrAnimate.Tick += _tmrAnimate_Tick;
            SetTargetPosition();
            SetStartingPosition();
        }

        private void SetTargetPosition()
        {
            int xPos = _frmMain.DisplayRectangle.Width - _picBox.Width - _PADDING;
            int yPos = _frmMain.DisplayRectangle.Height - _picBox.Height - _PADDING;
            _targetPos = new Point(xPos, yPos);
        }

        private void SetStartingPosition()
        {
            int xPos = _frmMain.DisplayRectangle.Width;
            int yPos = _frmMain.DisplayRectangle.Height - _picBox.Height - _PADDING;
            _startingPos = new Point(xPos, yPos);
        }

        public void Animate()
        {
            _picBox.Location = _startingPos;
            _picBox.Visible = true;
            _picBox.BringToFront();
            _tmrAnimate.Start();
        }

        private void _tmrIdle_Tick(object sender, EventArgs e)
        {
            if (++_idleTicks == 1)
            {
                _tmrAnimate.Start();
            }
        }

        private void _tmrAnimate_Tick(object sender, EventArgs e)
        {
            if (_appearing)
            {
                if (_picBox.Location.X != _targetPos.X)
                {
                    _picBox.Location = new Point(_picBox.Location.X - 2, _picBox.Location.Y);
                }

                if (_picBox.Location.Y != _targetPos.Y)
                {
                    _picBox.Location = new Point(_picBox.Location.X, _picBox.Location.Y - 2);
                }

                if (_picBox.Location.X <= _targetPos.X)
                {
                    _appearing = false;
                    _tmrAnimate.Stop();
                    _tmrIdle.Start();
                }
            }
            else
            {
                if (_picBox.Location.X != _startingPos.X)
                {
                    _picBox.Location = new Point(_picBox.Location.X + 2, _picBox.Location.Y);
                }

                if (_picBox.Location.Y != _startingPos.Y)
                {
                    _picBox.Location = new Point(_picBox.Location.X, _picBox.Location.Y + 2);
                }

                if (_picBox.Location.X >= _startingPos.X)
                {
                    this.Dispose();
                }
            }
        }

        public void Dispose()
        {
            _tmrAnimate.Stop();
            _tmrIdle.Stop();
            _picBox.Dispose();
            _tmrAnimate.Dispose();
            _tmrIdle.Dispose();
        }
    }
}
