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

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Files.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;

namespace CommunityServer.ControlPanel.FileAdmin
{
	public class FolderOptionsControl : BaseFilesAdminControl
	{
		#region Public Properties

		private int sectionID = -1;
		public int SectionID
		{
			get { return sectionID; }
			set { sectionID = value;}
		}

		#endregion

		#region Child Controls

		protected YesNoRadioButtonList EnableExternalLinks;
		protected YesNoRadioButtonList EnableDownloadDisclaimer;
		protected TextBox AllowedFileExtensions;
		protected TextBox RestrictedFileExtensions;

		#endregion

		private bool IsNew
		{
			get{return sectionID <= 0;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			CSContext context = CSContext.Current;
			sectionID = context.GetIntFromQueryString("SectionID", -1);

			if(!Page.IsPostBack)
			{
				BindSettingsData();
			}

		}

		#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()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		#region Folder Settings

		void BindSettingsData()
		{
			Folder f;

			if(IsNew)
			{
				f = new Folder();
			}
			else
			{
				f = CommunityServer.Files.Components.Folders.GetFolder(this.sectionID, false);
			}

			EnableExternalLinks.SelectedValue = f.EnableExternalLinks;
			EnableDownloadDisclaimer.SelectedValue = f.EnableDisclaimer;
			AllowedFileExtensions.Text = f.AllowedFileExtensions;
			RestrictedFileExtensions.Text = f.RestrictedFileExtensions;
		}

		void Update() 
		{
			Folder f = CommunityServer.Files.Components.Folders.GetFolder(this.sectionID, false);

			f.EnableExternalLinks = EnableExternalLinks.SelectedValue;
			f.EnableDisclaimer = EnableDownloadDisclaimer.SelectedValue;
			f.AllowedFileExtensions = AllowedFileExtensions.Text;
			f.RestrictedFileExtensions = RestrictedFileExtensions.Text;

			CommunityServer.Files.Components.Folders.Update(f);
		}

		#endregion

		public void Save()
		{
			//Validate we are an Admin
			if(!CSContext.Current.User.IsFileAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			Update();
		}
	}
}
