using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Bitrix.UI;
using Bitrix.DataLayer;
using Bitrix.Services.Text;
using Bitrix;
using System.Text;
using Bitrix.Security;
using Bitrix.Blog;

public partial class bitrix_admin_BlogList : BXAdminPage
{
    protected override void OnPreInit(EventArgs e)
    {
        if (!BXPrincipal.Current.IsCanOperate(BXBlog.Operations.AdminManagement))
            BXAuthentication.AuthenticationRequired();

        base.OnPreInit(e);
    }


    protected override void OnLoad(EventArgs e)
    {
        MasterTitle = GetMessage("PageTitle");
        Page.Title = GetMessage("PageTitle");

        base.OnLoad(e);
    }

    protected void ItemGrid_Select(object sender, BXSelectEventArgs e)
	{
        if (!BXPrincipal.Current.IsCanOperate(BXBlog.Operations.AdminManagement))
            return;

        List<BlogWrapper>  list = new List<BlogWrapper>();

        BXFilter f = new BXFilter(ItemFilter.CurrentFilter, BXBlog.Fields);

        BXSelect s = new BXSelect(BXSelectFieldPreparationMode.Normal,
            BXBlog.Fields.Id,
            BXBlog.Fields.Name,
            BXBlog.Fields.DateCreated,
            BXBlog.Fields.DateLastPosted,
            BXBlog.Fields.Active,
            BXBlog.Fields.Owner,
            BXBlog.Fields.Slug,
            BXBlog.Fields.Categories.Category
            );

        BXBlogCollection blogList = BXBlog.GetList(
            f,
            new BXOrderBy(BXBlog.Fields, string.IsNullOrEmpty(e.SortExpression) ? "Id" : e.SortExpression),
            s,
            null,
            BXTextEncoder.EmptyTextEncoder
            );

        foreach (BXBlog blog in blogList)
            list.Add(new BlogWrapper(blog, this));

        e.Data = list;
	}


    protected void ItemGrid_SelectCount(object sender, BXSelectCountEventArgs e)
	{
        e.Count = BXBlog.Count(new BXFilter(ItemFilter.CurrentFilter, BXBlog.Fields));
	}

    protected void ItemGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        BXGridViewRow row = (BXGridViewRow)e.Row;
        BlogWrapper wrapper = (BlogWrapper)row.DataItem;

        row.UserData.Add("ID", wrapper.ID);
    }


    protected void ItemGrid_Delete(object sender, BXDeleteEventArgs e)
	{
        if (!BXPrincipal.Current.IsCanOperate(BXBlog.Operations.AdminManagement))
            return;
		try
		{
	        BXBlog.Delete(e.Keys["ID"]);
			e.DeletedCount++;
		}
		catch (Exception ex)
		{
			ErrorMessage.AddErrorMessage(ex.Message);
		}
	}
}
/// <summary>
/// Обёртка для блога
/// </summary>
public class BlogWrapper
{
    BXBlog _charge;
    BXAdminPage _parentPage;
    public BlogWrapper(BXBlog charge, BXAdminPage parentPage)
    {
        if (charge == null)
            throw new ArgumentNullException("charge");

        if (parentPage == null)
            throw new ArgumentNullException("parentPage");

        _charge = charge;
        _parentPage = parentPage;
    }

    public string ID
    {
        get { return _charge.Id.ToString(); }
    }

    public string Name
    {
        get { return _charge.Name; }
    }

    public string DateCreated
    {
        get { return _charge.DateCreated.ToString("g"); }
    }

    public string DateLastPosted
    {
        get { return _charge.DateLastPosted.ToString("g"); }
    }

    public string Active
    {
        get { return _charge.Active ? _parentPage.GetMessageRaw("Kernel.Yes") : _parentPage.GetMessageRaw("Kernel.No"); }
    }

    public string Owner
    {
        get 
        { 
            BXBlogUser owner = _charge.Owner;
            return owner != null ? owner.User.GetDisplayName() : "";
        }
    }

    public string Slug
    {
        get { return _charge.Slug; }
    }

    public string Categories
    {
        get 
        {
            BXBlogCategoryCollection categories = _charge.Categories;
            if (categories == null || categories.Count == 0)
                return "";

            StringBuilder sb = new StringBuilder();
            foreach (BXBlogCategory category in categories)
            {
                if (sb.Length > 0)
                    sb.Append(", ");
                sb.Append(category.Name);
            }
            return sb.ToString();
        }
    }

}
