using System;
using System.Drawing;
using System.Windows.Forms;
using FastReport.Utils;

namespace FastReport.Forms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ProgressForm : BaseForm
    {
        private Report report;
        private bool aborted;
        private long ticks = 0;

        /// <summary>
        /// Gets Aborted state
        /// </summary>
        public bool Aborted
        {
            get { return aborted; }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (report != null)
                report.Abort();
            aborted = true;
        }

        private void ProgressForm_Paint(object sender, PaintEventArgs e)
        {
            using (Pen p = new Pen(Color.Gray, 2))
            {
                p.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                e.Graphics.DrawRectangle(p, DisplayRectangle);
            }
        }

        private void ProgressForm_Shown(object sender, EventArgs e)
        {
            lblProgress.Width = Width - lblProgress.Left * 2;
        }

        /// <summary>
        /// 
        /// </summary>
        public void ShowProgressMessage(string message)
        {
            lblProgress.Text = message;
            lblProgress.Refresh();
            if (ticks++ % 500 == 0)
                Config.DoEvent();
        }

        /// <summary>
        /// Initialazes a new instance of the <see cref="ProgressForm"/> class.
        /// </summary>
        /// <param name="report">A reference to the report.</param>
        public ProgressForm(Report report)
        {
            aborted = false;
            this.report = report;
            InitializeComponent();
            btnCancel.Text = Res.Get("Buttons,Cancel");
            UIUtils.CheckRTL(this);
            UpdateDpiDependencies();
        }

        /// <summary>
        /// Initialazes a new instance of the <see cref="ProgressForm"/> class.
        /// </summary>
        /// <param name="report">A reference to the report.</param>
        /// <param name="withCancelButton">Specifies whether the form should be with Cancel button.</param>
        public ProgressForm(Report report, bool withCancelButton)
        {
            this.report = report;
            InitializeComponent();
            btnCancel.Text = Res.Get("Buttons,Cancel");
            btnCancel.Enabled = withCancelButton;
            btnCancel.Visible = withCancelButton;
            lblProgress.Location = new Point(12, 50);
            UIUtils.CheckRTL(this);
            UpdateDpiDependencies();
        }
    }
}