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

using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;

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

		#region Child Controls

		protected ResourceControl SectionDescription;
		protected MPContent DescriptionRegion;
		protected ResourceControl Section_Sub_Description;
		protected MPContent TaskRegion;
		protected MPContainer Mpcontainer1;
        protected Repeater theBlogs;
        protected AlphaPicker picker;
        protected IText message;
	
		#endregion

        

        private void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["Delete"] != null)
            {
                int blogid = Globals.SafeInt(Request.QueryString["Delete"],-1);
                if(blogid > 0)
                {
                    Weblog blog = Weblogs.GetWeblog(blogid,true);
                    Weblogs.Delete(blog);

                    if(Request.QueryString["q"] != null)
                        Response.Redirect(Request.Path + "?q=" + Request.QueryString["q"]);
                    else if(Request.QueryString["a"] != null)
                        Response.Redirect(Request.Path + "?a=" + Request.QueryString["a"]);
                    else
                        Response.Redirect(Request.Path);
                }
            }
            else
            {

                ArrayList blogs = Weblogs.GetWeblogs(false,true,false);
                ArrayList list = null;
                message.Visible = false;

                if(picker.IsSelectAll)
                    list = new ArrayList(blogs); 
                else if(picker.SelectedLetter != null)
                {
                    Regex r = new Regex(picker.FilterText, RegexOptions.IgnoreCase);
                    list = new ArrayList();
                    foreach(Weblog w in blogs)
                    {
                        if(r.IsMatch(w.Name) || r.IsMatch(w.ApplicationKey) || (w.Owners != null && r.IsMatch(w.Owners)))
                            list.Add(w);
                    }

                    message.Text = string.Format(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_BlogAdmin_Blogs_FilterResults") , list.Count == 0 ? "no" : list.Count.ToString("#,#"));
                    message.Visible = true;
                
                }
                else 
                {
                    if(blogs.Count <= 50)
                        list = new ArrayList(blogs);
                    else
                    {
                        message.Text = string.Format(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_BlogAdmin_Blogs_FilterNoResults"),blogs.Count == 0 ? "no" :  blogs.Count.ToString("#,#"));
                        message.Visible = true;              
                    }

                }

                if(list != null)
                {
                    list.Sort(new BlogSorter());
                    theBlogs.DataSource = list;
                    theBlogs.DataBind();
                }
                else
                {
                    theBlogs.Visible = false;
                }
            }
        }

        private class BlogSorter : IComparer
        {
            #region IComparer Members

            public int Compare(object x, object y)
            {
                Weblog w1 = x as Weblog;
                Weblog w2 = y as Weblog;

                return w1.ApplicationKey.CompareTo(w2.ApplicationKey);
            }

            #endregion
        }

		#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

        protected string FormatOwners(object obj)
        {
            if(obj == null)
                return string.Empty;

            else
            {
                return obj.ToString().Replace(";", "<br />");
            }
        }

        protected string DeleteLink(object obj)
        {
            if(Context.Request.QueryString.Count > 0)
                return Context.Request.RawUrl.ToString() + "&Delete=" + obj.ToString();

            else
            {
                return Context.Request.RawUrl.ToString() + "?Delete=" + obj.ToString();
            }
        }
	}
}
