//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Controls
{
	public abstract class UploadAttachment : TemplatedWebControl
	{
		#region Child Controls

		private HtmlInputFile File;
		private TextBox Url;
		private RadioButton UploadFile;
		private RadioButton LinkToUrl;
		private Control LocalAttachmentArea, RemoteAttachmentArea;
		private IText Instructions;
		private IButton Save;
		private IButton Cancel;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			File = (HtmlInputFile) FindControl("File");
			Url = (TextBox) FindControl ("Url");
			UploadFile = (RadioButton) FindControl("UploadFile");
			LinkToUrl = (RadioButton) FindControl("LinkToUrl");
			Save = FindButton("Save");
			Cancel = FindButton("Close");
			LocalAttachmentArea = FindControl("LocalAttachmentArea");
			RemoteAttachmentArea = FindControl("RemoteAttachmentArea");
			Instructions = FindText("Instructions");

			InitializeChildControls();
		}

		private void InitializeChildControls() 
		{
			Save.Click += new EventHandler(Save_Click);
			if (Cancel != null)
			{
				Cancel.Click += new EventHandler(Cancel_Click);
				Cancel.CausesValidation = false;
			}
		}

		#endregion

		#region Properties

		protected abstract Section Section
		{
			get;
		}

		public virtual bool AllowRemoteAttachment
		{
			get
			{
				return Permissions.ValidatePermissions(this.Section, Permission.RemoteAttachment, CSContext.Current.User);
			}
		}

		public virtual bool AllowLocalAttachment
		{
			get
			{
				return Permissions.ValidatePermissions(this.Section, Permission.LocalAttachment, CSContext.Current.User);
			}
		}

		#endregion

		#region Control Event Handlers

		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);

			this.SkinName = "Skin-UploadAttachment.ascx";
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		#endregion

		#region Data Binding

		public override void DataBind()
		{
			base.DataBind();

			// Set some resource labels
			Save.Text = ResourceManager.GetString("Save");

			if (Cancel != null)
				Cancel.Text = ResourceManager.GetString("Cancel");

			if (!AllowLocalAttachment && LocalAttachmentArea != null)
				LocalAttachmentArea.Visible = false;

			if (!AllowRemoteAttachment && RemoteAttachmentArea != null)
				RemoteAttachmentArea.Visible = false;

			if (AllowLocalAttachment && AllowRemoteAttachment)
			{
				if (Instructions != null)
					Instructions.Text = ResourceManager.GetString("UploadAttachment_Description");
			}
			else if (AllowLocalAttachment)
			{
				if (Instructions != null)
					Instructions.Text = ResourceManager.GetString("UploadAttachment_Description_FileOnly");

				UploadFile.Checked = true;
				UploadFile.Visible = false;
			}
			else if (AllowRemoteAttachment)
			{
				if (Instructions != null)
					Instructions.Text = ResourceManager.GetString("UploadAttachment_Description_UrlOnly");
	
				LinkToUrl.Checked = true;
				LinkToUrl.Visible = false;
			}
			else
			{
				if (Instructions != null)
					Instructions.Text = ResourceManager.GetString("UploadAttachment_Description_NoAccess");

				Save.Visible = false;
			}
		}

		#endregion

		#region Child Control Event Handlers

		protected virtual bool IsFileValid(string filename, bool isRemote, int contentLengthBytes, ref string message)
		{
			message = String.Empty;
			return true;
		}

		protected virtual void Save_Click(object sender, EventArgs e)
		{
			string filename;
			if (LinkToUrl.Checked)
				filename = Url.Text;
			else
				filename = File.PostedFile.FileName;

			string message = String.Empty;
			if (this.IsFileValid(filename, LinkToUrl.Checked, LinkToUrl.Checked ? 0 : File.PostedFile.ContentLength, ref message))
			{
				const string resultArray = "<script language=\"javascript\">window.parent.Telligent_Modal.Close(new Array(\"{0}\",\"{1}\",\"{2}\",{3}));</script>";
				PostAttachment pa = null;

				if (LinkToUrl.Checked)
				{
					Permissions.AccessCheck(this.Section, Permission.RemoteAttachment, CSContext.Current.User);
                    pa = new PostAttachment(Url.Text);
				}
				else
				{
					Permissions.AccessCheck(this.Section, Permission.LocalAttachment, CSContext.Current.User);

					pa = new PostAttachment(File.PostedFile);
					pa.FriendlyFileName = pa.FileName;
				}

				pa.DateCreated = DateTime.Now;
				pa.IsRemote = LinkToUrl.Checked;
				pa.SectionID = this.Section.SectionID;

				Guid g = Guid.NewGuid();
				CommonDataProvider.Instance().AddTemporaryPostAttachment(g, pa);

				string close = string.Format(resultArray, g, pa.FileName.Replace("\"", "\\\""), pa.FriendlyFileName.Replace("\"", "\\\""), pa.IsRemote.ToString().ToLower());
				Page.RegisterStartupScript(this.GetType().FullName, close);
			}
			else
			{
				IText error = FindText("Error");
				error.Text = Globals.IsNullorEmpty(message) ? ResourceManager.GetString("UploadAttachment_Error") : message;
				error.Visible = true;
			}
		}


		protected virtual void Cancel_Click(Object sender, EventArgs e)
		{
			Modal.ClosePage(this.Page);
		}

		#endregion
	}
}