//------------------------------------------------------------------------------
// <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.Components;
using CommunityServer.Blogs.Components;
using CommunityServer.ControlPanel.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.BlogAdmin
{
	/// <summary>
	/// Summary description for GeneralOptionsPage.
	/// </summary>
	public class GeneralOptionsPage : BaseBlogAdminPage
	{

		#region Child Controls
		
		protected CheckBox createDirectories;
        protected FileOnlyStatusMessage FOStatus;
        protected ConfigOKStatusMessage Status;
        protected PlaceHolder OptionHolder;
        protected ResourceLinkButton SaveButton;
		protected CheckBox checkAutoCreate;
		protected DropDownList GroupList;
		protected CheckBox EnableCrossPosting;
		protected TextBox PingServices;
		protected TextBox AggregateTags;
        protected TextBox ServicePostCountLimit;
		protected CheckBox EnableRawHeadEditing;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			SaveButton.Text = ResourceManager.GetString("Save");
			if(!IsPostBack)
			{
				WeblogConfiguration config = WeblogConfiguration.Instance(false);
                if(config.FileOnly)
                {
                    FOStatus.Visible = true;
                    OptionHolder.Visible = false;
                }
                else
                {
                    createDirectories.Checked = config.CreateBlogDirectories;
					checkAutoCreate.Checked = config.EnableAutoCreate;
					EnableCrossPosting.Checked = config.EnableCrossPosting;
					EnableRawHeadEditing.Checked = config.EnableRawHeadEditing;
                    ServicePostCountLimit.Text = config.ServicePostCountLimit.ToString();
                    
					GroupList.DataSource = WeblogGroups.GetWeblogGroups(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;


					if (config.DefaultPingServices != null)
					{
						foreach (string url in config.DefaultPingServices)
						{
							if (PingServices.Text.Length > 0)
								PingServices.Text += "; ";

							PingServices.Text += url;
						}
					}

					foreach (string tag in config.DefaultAggregateTags)
					{
						if (AggregateTags.Text.Length > 0)
							AggregateTags.Text += "; ";

						AggregateTags.Text += tag;
					}

				}
			}
		}

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

				WeblogConfiguration config = WeblogConfiguration.Instance(false);
                if(!config.FileOnly)
                {
				
                    config.CreateBlogDirectories = this.createDirectories.Checked;
					config.EnableAutoCreate = this.checkAutoCreate.Checked;
					config.EnableCrossPosting = EnableCrossPosting.Checked;
					config.EnableRawHeadEditing = EnableRawHeadEditing.Checked;

                    config.ServicePostCountLimit = Int32.Parse(ServicePostCountLimit.Text); 


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

					ArrayList PingUrlsArray = new ArrayList();
					if (!Globals.IsNullorEmpty(PingServices.Text))
					{
						foreach (string url in PingServices.Text.Trim().Split(';'))
						{
							if (!Globals.IsNullorEmpty(url))
								PingUrlsArray.Add(url.Trim());
						}
					}
					config.DefaultPingServices = (string[])PingUrlsArray.ToArray(typeof(string));

					ArrayList AggregateTagsArray = new ArrayList();
					if (!Globals.IsNullorEmpty(AggregateTags.Text))
					{
						foreach (string tag in AggregateTags.Text.Trim().Split(';'))
						{
							if (!Globals.IsNullorEmpty(tag))
								AggregateTagsArray.Add(tag.Trim());
						}
					}
					config.DefaultAggregateTags = (string[])AggregateTagsArray.ToArray(typeof(string));
					
					Status.Visible = true;

                    config.Save();
                }
            }
        }
	}
}
