using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Mail;
using System.Net;

namespace VoServices.Footprint.Web
{
	/// <summary>
	/// Summary description for check.
	/// </summary>
	public partial class check : System.Web.UI.Page
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{
			Response.Expires = -1;
			Response.Buffer = false;
			//Response.ContentType = "text/plain";
			Response.Write("<pre>");

			int error = 0, success = 0;

			// client
			Response.Write("HTTP client:" + Request.UserAgent + "\r\n\r\n");

			// sql connectionstring
			if (CheckSqlString("SqlConnectionString")) success++; else error++;
			if (CheckSqlString("SqlConnectionString_Safe")) success++; else error++;

			if (CheckSmtp("ErrorReportSmtp", "ErrorReportAddress")) success++; else error++;

			//if (CheckWebService(ConfigurationSettings.AppSettings["UserWebServiceUrl"])) success++; else error++;

			//
			string root = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/") + 1);
			if (CheckWebService(root + ConfigurationSettings.AppSettings["CurrentWebServiceUrl"] + "/search.asmx")) success++; else error++;
			if (CheckWebService(root + ConfigurationSettings.AppSettings["CurrentWebServiceUrl"] + "/search_votable.asmx")) success++; else error++;
			if (CheckWebService(root + ConfigurationSettings.AppSettings["CurrentWebServiceUrl"] + "/admin.asmx")) success++; else error++;
			if (CheckWebService(root + ConfigurationSettings.AppSettings["CurrentWebServiceUrl"] + "/util.asmx")) success++; else error++;

			Response.Write(String.Format("Check process ended with {0} error(s).\r\n", error));
			Response.Write("Check performed on " + DateTime.Now.ToString());
			Response.Write("</pre>");
			Response.End();
		}

		protected bool CheckSqlString(string sqlstring)
		{
			bool res = false;

			Response.Write("Checking SQL connection string...\r\n");
			Response.Write("     web.config key: " + sqlstring + "\r\n");
			//Response.Write("     " + ConfigurationSettings.AppSettings[sqlstring] + "\r\n");

			try
			{
				SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings[sqlstring]);
				SqlCommand cmd = new SqlCommand("SELECT TOP 1 * FROM Spectra", cn);
				SqlDataAdapter da = new SqlDataAdapter(cmd);

				DataSet ds = new DataSet();
				da.Fill(ds);

				ds.Dispose();
				da.Dispose();
				cn.Close(); cn.Dispose();
				Response.Write("     OK\r\n\r\n");
				res = true;
			}
			catch (System.Exception ex)
			{
				Response.Write("     ERROR: " + ex.Message + "\r\n\r\n");
				res = false;
			}

			return res;
		}

		protected bool CheckSmtp(string smtp, string address)
		{
			bool res = false;

			Response.Write("Checking SMTP server...\r\n");
			Response.Write("     web.config key: " + smtp + "\r\n");
			//Response.Write("     " + ConfigurationSettings.AppSettings[smtp] + "\r\n");

			try
			{
				SmtpMail.SmtpServer = ConfigurationSettings.AppSettings[smtp];

				SmtpMail.Send("webmaster@voservices.net", ConfigurationSettings.AppSettings[address],
					"Web site test routine",
					"Web site test routine");

				Response.Write("     OK\r\n\r\n");
				res = true;
			}
			catch (System.Exception ex)
			{
				Response.Write("     ERROR: " + ex.Message + "\r\n\r\n");
				res = false;
			}

			return res;
		}

		protected bool CheckWebService(string url)
		{
			bool res = false;

			try
			{
				Response.Write("Checking web service...\r\n");
				Response.Write("     url: " + url + "\r\n");

				WebClient wc = new WebClient();
				byte[] buffer = wc.DownloadData(url);
		
				wc.Dispose();

				Response.Write("     OK\r\n\r\n");
				res = true;
			}
			catch (System.Exception ex)
			{
				Response.Write("     ERROR: " + ex.Message + "\r\n\r\n");
				res = false;
			}

			return res;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion
	}
}
