namespace XmlSerialization.Client {
	using System;
	using System.ComponentModel;
	using System.Drawing;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using Config.Web.Serialization;

	/// <summary>
	///	简要描述 EditConfiguration.
	/// </summary>
	public class EditConfiguration : System.Web.UI.Page {
		protected Label lblGlobalizationResponse;
		protected Button btnEditGlobalization;
		protected TextBox txtGlobalizationResponse;
		protected TextBox txtGlobalizationRequest;
		protected LinkButton lnkShowAllGlobalization;
		protected Button btnEditSessionState;
		protected TextBox txtSessionStateStateConnectionString;
		protected DropDownList ddSessionStateCookieless;
		protected TextBox txtSessionStateSqlConnectionString;
		protected DropDownList ddSessionStateMode;
		protected Button btnEditTrace;
		protected DropDownList ddTraceMode;
		protected DropDownList ddTracePageOutput;
		protected TextBox txtTraceRequestLimit;
		protected DropDownList ddTraceEnabled;
		protected LinkButton lnkShowAllTrace;
		protected Button btnEditAuthentication;
		protected DropDownList ddAuthentication;
		protected LinkButton lnkShowAllAuth;
		protected Button btnEditCustomErrors;
		protected DropDownList ddCustomErrors;
		protected LinkButton lnkShowAllCE;
		protected Button btnEditCompilation;
		protected LinkButton lnkShowAllCompilation;
		protected Label lblGlobalizationRequest;
		protected LinkButton lnkEditGlobalization;
		protected Label lblSessionStateSqlConnectionString;
		protected Label lblSesionStateTimeout;
		protected Label lblSessionStateCookieless;
		protected Label lblSessionStateStateConnectionString;
		protected Label lblSessionStateMode;
		protected Label lblTraceMode;
		protected Label lblTracePageOutput;
		protected Label lblTraceRequestLimit;
		protected Label lblTraceEnabled;
		protected LinkButton lnkEditTrace;
		protected Label lblAuthenticationMode;
		protected LinkButton lnkEditAuthentication;
		protected Label lblCustomErrorsMode;
		protected LinkButton lnkEditCustomErrors;
		protected Label lblCompilationDebug;
		protected LinkButton lnkEditCompilation;
		protected Panel pnlGlobalization;
		protected Panel pnlSessionState;
		protected Panel pnlTrace;
		protected Panel pnlAuthentication;
		protected Panel pnlCustomErrors;
		protected Panel pnlCompilation;
		protected Panel pnlAll;
		protected LinkButton lnk;
		protected LinkButton lnkEdit;
		protected TextBox txtSessionStateTimeout;
		protected LinkButton lblEditSessionState;
		protected LinkButton lnkShowAllSessionState;
		protected DropDownList ddCompilationDebug;
		protected Label lblCompilationDefaultLanguage;
		protected DropDownList ddCompilationDefaultLanguage;
		private string filePath = "";
	
		public EditConfiguration() {
			Page.Init += new System.EventHandler(Page_Init);
		}

		protected void Page_Init(object sender, EventArgs e) {
		//组件初始化
			InitializeComponent();
		}

		
		private void InitializeComponent() {
			this.lnkEditCompilation.Click += new System.EventHandler(this.lnkEditCompilation_Click);
			this.lnkEditCustomErrors.Click += new System.EventHandler(this.lnkEditCustomErrors_Click);
			this.lnkEditAuthentication.Click += new System.EventHandler(this.lnkEditAuthentication_Click);
			this.lnkEditTrace.Click += new System.EventHandler(this.lnkEditTrace_Click);
			this.lblEditSessionState.Click += new System.EventHandler(this.lblEditSessionState_Click);
			this.lnkEditGlobalization.Click += new System.EventHandler(this.lnkEditGlobalization_Click);
			this.lnkShowAllCompilation.Click += new System.EventHandler(this.lnkShowAllCompilation_Click);
			this.btnEditCompilation.Click += new System.EventHandler(this.btnEditCompilation_Click);
			this.lnkShowAllCE.Click += new System.EventHandler(this.lnkShowAllCE_Click);
			this.btnEditCustomErrors.Click += new System.EventHandler(this.btnEditCustomErrors_Click);
			this.lnkShowAllAuth.Click += new System.EventHandler(this.lnkShowAllAuth_Click);
			this.btnEditAuthentication.Click += new System.EventHandler(this.btnEditAuthentication_Click);
			this.lnkShowAllTrace.Click += new System.EventHandler(this.lnkShowAllTrace_Click);
			this.btnEditTrace.Click += new System.EventHandler(this.btnEditTrace_Click);
			this.lnkShowAllSessionState.Click += new System.EventHandler(this.lnkShowAllSessionState_Click);
			this.btnEditSessionState.Click += new System.EventHandler(this.btnEditSessionState_Click);
			this.lnkShowAllGlobalization.Click += new System.EventHandler(this.lnkShowAllGlobalization_Click);
			this.btnEditGlobalization.Click += new System.EventHandler(this.btnEditGlobalization_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}

		public void lnkShowAllGlobalization_Click (object sender, System.EventArgs e) {
			this.pnlGlobalization.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void lnkShowAllSessionState_Click (object sender, System.EventArgs e) {

			this.pnlSessionState.Visible = false;
			this.pnlAll.Visible = true;
		}


		public void lnkShowAllTrace_Click (object sender, System.EventArgs e) {
			this.pnlTrace.Visible = false;
			this.pnlAll.Visible = true;
		}


		public void lnkShowAllCompilation_Click(object sender, System.EventArgs e) {
			this.pnlCompilation.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void lnkShowAllCE_Click (object sender, System.EventArgs e) {
			this.pnlCustomErrors.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void lnkShowAllAuth_Click (object sender, System.EventArgs e) {
			this.pnlAuthentication.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void btnEditCompilation_Click(object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			Compilation comp = systemWeb.Compilation;
			comp.DefaultLanguage = this.ddCompilationDefaultLanguage.SelectedItem.Text;
			comp.Debug = System.Convert.ToBoolean(ddCompilationDebug.SelectedItem.Value);
			systemWeb.Compilation = comp;
			ConfigSerialization.PersistConfig(cs,filePath);
			this.FillControls();
			this.pnlCompilation.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void btnEditAuthentication_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			Authentication auth = systemWeb.Authentication;
			auth.Mode = this.ddAuthentication.SelectedItem.Value;
			systemWeb.Authentication = auth;
			ConfigSerialization.PersistConfig(cs,filePath);
			this.FillControls();
			this.pnlAuthentication.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void btnEditCustomErrors_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			CustomErrors custErrors = systemWeb.CustomErrors;
			custErrors.Mode = this.ddCustomErrors.SelectedItem.Value;
			systemWeb.CustomErrors = custErrors;
			ConfigSerialization.PersistConfig(cs,filePath);
			this.FillControls();
			this.pnlCustomErrors.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void btnEditTrace_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			Trace trace = systemWeb.Trace;
			trace.Enabled = Convert.ToBoolean(this.ddTraceEnabled.SelectedItem.Value);
			trace.RequestLimit = Convert.ToInt32(this.txtTraceRequestLimit.Text);
			trace.PageOutput = Convert.ToBoolean(this.ddTracePageOutput.SelectedItem.Value);
			trace.TraceMode = this.ddTraceMode.SelectedItem.Text;
			systemWeb.Trace = trace;
			ConfigSerialization.PersistConfig(cs,filePath);
			this.FillControls();
			this.pnlTrace.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void btnEditSessionState_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			SessionState state = systemWeb.SessionState;
			state.Mode = this.ddSessionStateMode.SelectedItem.Value;
			state.StateConnectionString = this.txtSessionStateStateConnectionString.Text;
			state.Cookieless = Convert.ToBoolean(this.ddSessionStateCookieless.SelectedItem.Value);
			state.Timeout = Convert.ToInt32(this.txtSessionStateTimeout.Text);
			state.SqlConnectionString = this.txtSessionStateSqlConnectionString.Text;
			systemWeb.SessionState = state;
			ConfigSerialization.PersistConfig(cs,filePath);
			this.FillControls();
			this.pnlSessionState.Visible = false;
			this.pnlAll.Visible = true;
		}

		public void btnEditGlobalization_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			Globalization glob = systemWeb.Globalization;
			glob.RequestEncoding = this.txtGlobalizationRequest.Text;
			glob.ResponseEncoding = this.txtGlobalizationResponse.Text;
			systemWeb.Globalization = glob;
			ConfigSerialization.PersistConfig(cs,filePath);
			this.FillControls();
			this.pnlGlobalization.Visible = false;
			this.pnlAll.Visible = true;
		}
		private void Page_Load(object sender, System.EventArgs e) {
			this.filePath = this.Server.MapPath("configTest.xml");
			this.FillControls();
		}
		private void FillControls() {
			Configuration cs = ConfigSerialization.LoadConfig(filePath);
			SystemWeb systemWeb = cs.SystemWeb;
			this.lblCompilationDebug.Text = systemWeb.Compilation.Debug.ToString();
			this.lblCompilationDefaultLanguage.Text = systemWeb.Compilation.DefaultLanguage;
			this.lblCustomErrorsMode.Text = systemWeb.CustomErrors.Mode;
			this.lblAuthenticationMode.Text = systemWeb.Authentication.Mode;
			//Trace
			Trace trace = systemWeb.Trace;
			this.lblTraceEnabled.Text = trace.Enabled.ToString();
			this.lblTraceRequestLimit.Text = trace.RequestLimit.ToString();
			this.lblTracePageOutput.Text = trace.PageOutput.ToString();
			this.lblTraceMode.Text = trace.TraceMode;
			//Session State
			SessionState state = systemWeb.SessionState;
			//Inproc
			this.lblSessionStateMode.Text = state.Mode.ToString();
			//使用 SQL Server			
			this.lblSessionStateStateConnectionString.Text = state.StateConnectionString.ToString();
			//Cookieless
			this.lblSessionStateCookieless.Text = state.Cookieless.ToString();
			//Timeout
			this.lblSesionStateTimeout.Text = state.Timeout.ToString();
			//Server
			this.lblSessionStateSqlConnectionString.Text = state.SqlConnectionString;

			//Globalization
			Globalization glob = systemWeb.Globalization;
			this.lblGlobalizationRequest.Text = glob.RequestEncoding;
			this.lblGlobalizationResponse.Text = glob.ResponseEncoding;
		}

		protected void lnkEditCompilation_Click(object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(this.filePath);
			string lang = cs.SystemWeb.Compilation.DefaultLanguage;
			DropDownList dd = this.ddCompilationDefaultLanguage;
			dd.SelectedIndex = GetIndex(lang,dd);
			bool debug = cs.SystemWeb.Compilation.Debug;
			if (debug) {
				this.ddCompilationDebug.SelectedIndex = 0;
			} else {
				this.ddCompilationDebug.SelectedIndex = 1;
			}
			this.pnlAll.Visible = false;
			this.pnlCompilation.Visible = true;
		}

		private int GetIndex(string lang,DropDownList dropDown) {
			int itemCount = dropDown.Items.Count;
			for (int i=0;i<itemCount;i++) {
				if (dropDown.Items[i].Value.ToUpper() == lang.ToUpper()) {					
					return i;
				}
			}
			return 0;
		}

		public void lnkEditCustomErrors_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(this.filePath);
			string mode = cs.SystemWeb.CustomErrors.Mode;
			if (mode.ToUpper() == "OFF") {
				this.ddCustomErrors.SelectedIndex = 0;
			} else {
				this.ddCustomErrors.SelectedIndex = 1;
			}
			this.pnlAll.Visible = false;
			this.pnlCustomErrors.Visible = true;
		}
		public void lnkEditAuthentication_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(this.filePath);
			string mode = cs.SystemWeb.Authentication.Mode;
			if (mode.ToUpper() == "NONE") {
				this.ddAuthentication.SelectedIndex = 0;
			} else if (mode.ToUpper() == "WINDOWS") {
				this.ddAuthentication.SelectedIndex = 1;
			} else if (mode.ToUpper() == "COOKIE"){
				this.ddAuthentication.SelectedIndex = 2;
			} else if (mode.ToUpper() == "PASSPORT"){
				this.ddAuthentication.SelectedIndex = 3;
			}
			this.pnlAll.Visible = false;
			this.pnlAuthentication.Visible = true;
		}
		public void lnkEditTrace_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(this.filePath);
			Trace trace = cs.SystemWeb.Trace;
			//trace 可用
			if (trace.Enabled) {
				this.ddTraceEnabled.SelectedIndex = 0;
			} else {
				this.ddTraceEnabled.SelectedIndex = 1;
			}
			//Request Limit
			this.txtTraceRequestLimit.Text = trace.RequestLimit.ToString();
			//Page Output
			if (trace.PageOutput) {
				this.ddTracePageOutput.SelectedIndex = 0;
			} else {
				this.ddTracePageOutput.SelectedIndex = 1;
			}
			//Trace Mode
			if (trace.TraceMode.ToUpper() == "DEFAULT")	{
				this.ddTraceMode.SelectedIndex = 0;
			} else if (trace.TraceMode.ToUpper() == "SORTBYCATEGORY") {				
				this.ddTraceMode.SelectedIndex = 1;
			} else if (trace.TraceMode.ToUpper() == "SORTBYTIME") {
				this.ddTraceMode.SelectedIndex = 2;
			}
			this.pnlAll.Visible = false;
			this.pnlTrace.Visible = true;
		}
		public void lblEditSessionState_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(this.filePath);
			SessionState state = cs.SystemWeb.SessionState;
			//Mode
			string mode = state.Mode;
			DropDownList dd = this.ddSessionStateMode;
			dd.SelectedIndex = GetIndex(mode,dd);
			//Cookieless
			if (state.Cookieless)	{
				this.ddSessionStateCookieless.SelectedIndex = 0;
			} else 	{
				this.ddSessionStateCookieless.SelectedIndex = 1;
			}
			//Timeout
			this.txtSessionStateTimeout.Text = state.Timeout.ToString();
			this.txtSessionStateSqlConnectionString.Text = state.SqlConnectionString;
     		this.txtSessionStateStateConnectionString.Text = state.StateConnectionString.ToString();
			this.pnlAll.Visible = false;
			this.pnlSessionState.Visible = true;

		}
		public void lnkEditGlobalization_Click (object sender, System.EventArgs e) {
			Configuration cs = ConfigSerialization.LoadConfig(this.filePath);
			Globalization glob = cs.SystemWeb.Globalization;
			this.txtGlobalizationRequest.Text = glob.RequestEncoding;
			this.txtGlobalizationResponse.Text = glob.ResponseEncoding;
			this.pnlAll.Visible = false;
			this.pnlGlobalization.Visible = true;
		}
	}
}
