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.IO;
using System.Web.Mail;

namespace VoServices.Footprint.Web
{
	/// <summary>
	/// Summary description for _default.
	/// </summary>
	public partial class user : BasePageTemplated
	{
		protected System.Web.UI.WebControls.Panel Panel1;

		protected bool _loggedIn = false;
		protected User _user;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			_user = new User(DatabaseConnection);

			if (((string) Session["UserWebServiceId"]) == "")
			{
				TitleLabel.Text = "Create user account";
			}
			else
			{
				_loggedIn = true;

				TitleLabel.Text = "Change user profile";
				PasswordInfoLabel.Visible = true;

				// Loading user info
				_user.Guid = (string) Session["UserWebServiceId"];
				_user.Load();

				if (!IsPostBack)
				{
					UserId.Text = _user.Username;
					UserId.Enabled = false;
					Name.Text = _user.Name;
					Institute.Text = _user.Institute;
					Email.Text = _user.Email;
				}
			}
		}

		#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

		protected void Ok_Click(object sender, System.EventArgs e)
		{
			string url = "";

//			try
//			{
				if (!_loggedIn)
					_user.Username = UserId.Text;

				_user.Name = Name.Text;
				_user.Institute = Institute.Text;
				_user.Email = Email.Text;

				if (!_loggedIn)
					_user.Password = Password.Text;
				else if (Password.Text != "")
					_user.Password = Password.Text;

				_user.Save();

			// sending e-mail
			if (!_loggedIn)
			{
				string body = "";
				StreamReader tmpfile = new StreamReader(Server.MapPath("../templates/register.txt"));
				body = tmpfile.ReadToEnd();
				tmpfile.Close();

				body = body.Replace("[$Username]", _user.Username);
				body = body.Replace("[$Password]", _user.Password);
				body = body.Replace("[$Name]", _user.Name);
				body = body.Replace("[$Institute]", _user.Institute);
				body = body.Replace("[$Email]", _user.Email);
				body = body.Replace("[$GUID]", _user.Guid);
				body = body.Replace("[$Date]", DateTime.Now.ToString());

				SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["ErrorReportSmtp"];

				SmtpMail.Send("webmaster@voservices.net",
					_user.Email,
					"Your Spectrum Service Password",
					body);
			}

				Session["UserWebServiceId"] = _user.Guid;
				Session["UserName"] = _user.Username;

				url = "userresult.aspx?res=success";
//			}
//			catch (System.Exception ex)
//			{
//				url = "userresult.aspx?res=failed&msg=" + Server.UrlEncode(ex.Message);
//			}

			Response.Redirect(url);
		}

		protected void Cancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(this.OriginalReferer);
		}
	}
}
