using System;
using System.Threading;
using System.Windows.Forms;

namespace LJLib.Jdt
{
    public partial class Form_jdt : Form, IJdt, IDisposable
    {
        public static Form_jdt NewWin()
        {
            Form_jdt rslt = new Form_jdt();
            ThreadPool.QueueUserWorkItem(rslt.ShowJdt);
            do 
            {
                Thread.Sleep(100);
            } while (!rslt.isShown);
            return rslt;
        }

        private Form_jdt()
        {
            InitializeComponent();
            this.closeJdt = this.CloseJdt;
            this.dispose = this.Dispose;
            this.acceptTotal = this.AcceptTotal;
            this.inc = this.Inc;
            this.setMsg = this.SetMsg;
            this.ole_test = this.label1.Text;
        }

        public bool isShown = false;

        private void ShowJdt(object state)
        {
            this.ShowDialog();
        }

        public void CloseJdt()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(this.closeJdt);
            }
            else
            {
                if (this.isShown)
                {
                    this.Close();
                    this.isShown = false;
                }
            }
        }

        private string ole_test;

        public void AcceptTotal(int total)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(this.acceptTotal, new object[] { total });
            }
            else
            {
                this.progressBar1.Maximum = total;
                this.Inc(0);
                this.SetMsg(this.ole_test);
            }
        }

        public void Inc(int value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(this.inc, new object[] { value });
            }
            else
            {
                if (value < this.progressBar1.Minimum)
                {
                    value = this.progressBar1.Minimum;
                }
                if (value > this.progressBar1.Maximum)
                {
                    value = this.progressBar1.Maximum;
                }
                this.progressBar1.Value = value;
                float progress = (float)this.progressBar1.Value / this.progressBar1.Maximum * 100;
                this.label2.Text = progress.ToString("####0.0") + "%";
            }
        }

        public void SetMsg(string value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(this.setMsg, new object[] { value });
            }
            else
            {
                this.label1.Text = value;
            }
        }

        private delegate void Delegate1();
        private Delegate1 closeJdt;
        private Delegate1 dispose;

        private delegate void Delegate2(int value);
        private Delegate2 acceptTotal;
        private Delegate2 inc;

        private delegate void Delegate3(string value);
        private Delegate3 setMsg;

        private void Form_jdt_Shown(object sender, EventArgs e)
        {
            this.Focus();
            isShown = true;
        }

        #region IDisposable 成员

        void IDisposable.Dispose()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(this.dispose);
            }
            else
            {
                CloseJdt();
                base.Dispose();
            }
        }

        #endregion
    }
}
