using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace FineUIPro.Web.Controls
{
    public partial class ProgressBox : System.Web.UI.UserControl
    {
        #region 各种属性
        /// <summary>最大值</summary>
        public int Max
        {
            get { return this.Session["_Max"] != null ? Convert.ToInt32(this.Session["_Max"]) : 100; }
            set
            {
                if (this.Session["_Max"] != null) { this.Session["_Max"] = value; }
                else { this.Session.Add("_Max", value); }
            }
        }

        private static int _value;
        /// <summary>进度值 </summary>
        public int Value
        {
            get { return _value; }
            set
            {
                if (value < 0) { value = 0; }
                if (value > this.Max) { value = this.Max; }
                _value = value;
            }
        }

        /// <summary> ajax回调时的网址 </summary>
        public String PageUrl
        {
            get { return this.ViewState["_PageUrl"] == null ? this.Page.Request.Url.ToString() : this.ViewState["_PageUrl"].ToString(); }
            set
            {
                if (this.ViewState["_PageUrl"] != null)
                { this.ViewState.Add("_PageUrl", value); }
                else
                { this.ViewState["_PageUrl"] = value; }
            }
        }

        public delegate void ThreadStartFunciton();
        public ThreadStartFunciton RunFunction
        {
            get { return this.Session[this.ID + "_runFunction"] as ThreadStartFunciton; }
            set
            {
                if (this.Session[this.ID + "_runFunction"] != null)
                { this.Session.Add(this.ID + "_runFunction", value); }
                else
                { this.Session[this.ID + "_runFunction"] = value; }
            }
        }

        #endregion

        #region 各种方法

        /// <summary>开始一个进程,运行this.RunFunction中指定的函数 </summary>
        public void StartThread()
        {
            this.StartThread(this.RunFunction);
        }

        /// <summary>开始一个进程,运行指定的函数 </summary>
        /// <param name="runFunction"><函数的名称/param>
        public void StartThread(ThreadStartFunciton runFunction)
        {

            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(runFunction));
            thread.Start();
        }

        /// <summary>获得客户端回调函数的字符串 </summary>
        /// <returns>STring</returns>
        public String GetPostBackFunctionNameReference()
        {
            return "returnResponse();";
        }

        /// <summary>向客户观返回取当前进度的值</summary>
        public void ResponseValue()
        {
            Response.Write(this.Value.ToString() + "," + this.Max.ToString());
            Response.Flush();
            Response.Close();
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        { }
    }
}