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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.PhotoAdmin
{
	/// <summary>
	/// Summary description for GeneralOptionsPage.
	/// </summary>
	public class GeneralOptionsPage : BaseGalleryAdminPage
	{
    
		#region Child Controls
		
		protected CheckBox createDirectories;
		protected FormLabel Formlabel1;
		protected ResourceControl Resourcecontrol2;
		protected ResourceControl ResourceControl1;
		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected MPContainer Mpcontainer1;
		protected HelpIcon Helpicon4;
		protected FormLabel Formlabel2;
		protected TextBox ImageX;
		protected RegularExpressionValidator Regularexpressionvalidator1;
		protected HelpIcon Helpicon3;
		protected FormLabel Formlabel3;
		protected TextBox ImageY;
		protected RegularExpressionValidator Regularexpressionvalidator2;
		protected HelpIcon Helpicon1;
		protected FormLabel Formlabel7;
		protected TextBox Quality;
		protected RegularExpressionValidator Regularexpressionvalidator3;
		protected HelpIcon Helpicon2;
		protected HelpIcon Helpicon5;
		protected FormLabel Formlabel4;
		protected CheckBox OriginalSizeOverride;
		protected ResourceLinkButton SaveButton;
		protected TextBox RSSDefaultThreadsPerFeed;
		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon26;
		protected CommunityServer.ControlPanel.Controls.ResourceControl ResourceControl53;
		protected CommunityServer.ControlPanel.Controls.FileOnlyStatusMessage FOStatus;
		protected CommunityServer.ControlPanel.Controls.ConfigOKStatusMessage Status;
		protected System.Web.UI.WebControls.PlaceHolder OptionHolder;
		protected RequiredFieldValidator RSSDefaultThreadsPerFeedValidator;
		protected HelpIcon Helpicon6;
		protected FormLabel Formlabel5;
		protected CheckBox autoCreateGallery;
        protected DropDownList GroupList;

		#endregion
	
		private void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				GalleryConfiguration config = GalleryConfiguration.Instance(false);
				if(config.FileOnly)
				{
					FOStatus.Visible = true;
					OptionHolder.Visible = false;
				}
				else
				{
					createDirectories.Checked = config.CreateGalleryDirectories;
					autoCreateGallery.Checked = config.AutoCreateGallery;
					ImageX.Text = config.MaxX.ToString() ;
					ImageY.Text = config.MaxY.ToString();
					Quality.Text = config.Quality.ToString();
					RSSDefaultThreadsPerFeed.Text = config.RssDefaultThreadsPerFeed.ToString();

                    GroupList.DataSource = GalleryGroups.GetGroups(false, true, false);
                    GroupList.DataTextField = "Name";
                    GroupList.DataValueField = "GroupId";
                    GroupList.DataBind();

                    ListItem li = GroupList.Items.FindByValue(config.DefaultGroupID.ToString());
                    if (li != null)
                        li.Selected = true;
                }
			}
		}

		#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);
			this.SaveButton.Click +=new EventHandler(SaveButton_Click);
		}
		
		/// <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 System.EventHandler(this.Page_Load);

		}
		#endregion

        private void SaveButton_Click(object sender, EventArgs e)
        {
			if (!Page.IsValid)
				return;

			GalleryConfiguration config = GalleryConfiguration.Instance(false);

			if(config.FileOnly)
				return;

			config.CreateGalleryDirectories = this.createDirectories.Checked;
			config.AutoCreateGallery = this.autoCreateGallery.Checked;

            if (this.GroupList.Items.Count > 0)
                config.DefaultGroupID = Int32.Parse(this.GroupList.SelectedValue);

            if(!Globals.IsNullorEmpty(this.ImageX.Text))
				config.MaxX = int.Parse(this.ImageX.Text) ;
			else
				config.MaxX = -1;

			if(!Globals.IsNullorEmpty(this.ImageY.Text))
				config.MaxY = int.Parse(this.ImageY.Text) ;
			else
				config.MaxY = -1;

			if(!Globals.IsNullorEmpty(this.Quality.Text))
				config.Quality = int.Parse(this.Quality.Text) ;
			else
				config.Quality = 80;

			try 
			{ 
				config.RssDefaultThreadsPerFeed = Int32.Parse( RSSDefaultThreadsPerFeed.Text ); 
				if(config.RssDefaultThreadsPerFeed < 1)
					RSSDefaultThreadsPerFeedValidator.IsValid = false;
			}
			catch { RSSDefaultThreadsPerFeedValidator.IsValid = false; }

			if (Page.IsValid)
			{
                config.Save();
				Status.Visible = true;
			}
        }
	}
}
