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 Bitrix.UI;
using Bitrix.Components;
using Bitrix.IBlock;
using System.Collections.Generic;
using Bitrix.DataTypes;
using Bitrix.Services.Text;


public partial class PhotogalleryAlbumEdit : BXComponent
{
    //PROPERTIES
    public int AlbumId
    {
        get
        {
            if (!IsComponentDesignMode)
            {
                return Results.Get("AlbumId", 0);
            }

            return 0;
        }
    }

    public string BackUrl
    {
        get
        {
            return Results.Get("BackUrl", "");
        }
        set
        {
            Results["BackUrl"] = value;
        }
    }

    public string ParamAlbum
    {
        get
        {
            return Parameters.Get<string>("ParamAlbum", "Album");
        }
    }

    public bool CanModify
    {
        get
        {
            return Results.Get("ModifyStructure", false);
        }

        private set
        {
            Results["ModifyStructure"] = value;
        }
    }

    public void SaveAlbumInfo(int parentAlbumId, string title, string description)
    {
        BXInfoBlockSectionOld sec;
        if (AlbumId > 0)
        {
            sec = BXInfoBlockSectionManagerOld.GetById(AlbumId);
            sec.ParentSectionId = parentAlbumId;
            sec.Name = title;
            sec.DescriptionType = BXTextType.Text;
            sec.Description = description;
        }
        else
            sec = BXInfoBlockSectionManagerOld.Create(Parameters.Get("IBlockId", 0),
                parentAlbumId,
                true,
                500,
                title,
                -1,
                -1,
                description,
                BXTextType.Text,
                string.Empty,
                string.Empty);



        sec.Update();

        string url;
        if (EnableSef)
        {
            Results["AlbumId"] = (AlbumId > 0) ? AlbumId.ToString() : string.Empty;
            if (AlbumId > 0)
            {
                url = MakeLink(Parameters.Get<string>("SefFolder"), Parameters.Get<string>("Sef_Album"), Results);
            }
            else
            {
                url = MakeLink(Parameters.Get<string>("SefFolder"), string.Empty, Results);
            }
        }
        else
        {
            if (AlbumId > 0)
            {
                url = string.Format("{0}?{1}={2}",
                    Request.Path,
                    Parameters.Get<string>("ParamAlbum", "Album"),
                    AlbumId);
            }
            else
            {
                url = Request.Path;
            }
        }

        if (Parameters.Get<bool>("EnableAjax", false) && !IsComponentDesignMode)
        {
            string script = string.Format("setTimeout(\"PageTransfer('{0}');\",0);",
                url);
            ScriptManager.RegisterStartupScript(Page, typeof(Page), "AJAX_BUTTON_CLICK", script, true);
        }
        else
            Response.Redirect(url);
    }

    public override bool ProcessCommand(string commandName, BXParamsBag<object> commandParameters, System.Collections.Generic.List<string> commandErrors)
    {
        int albumId = (int)commandParameters["Id"];
        int parentAlbumId = (int)commandParameters["ParentId"];

        BXInfoBlockSectionOld sec;
        if (albumId > 0)
        {
            sec = BXInfoBlockSectionManagerOld.GetById(albumId);
            sec.ParentSectionId = parentAlbumId;
            sec.Name = commandParameters["Title"].ToString();
            sec.Description = commandParameters["Description"].ToString();
        }
        else
            sec = BXInfoBlockSectionManagerOld.Create(Parameters.Get("IBlockId", 0),
                parentAlbumId,
                true,
                500,
                commandParameters["Title"].ToString(),
                -1,
                -1,
                commandParameters["Description"].ToString(),
                BXTextType.Text,
                string.Empty,
                string.Empty);
                
        

        sec.Update();

        string url;
        if (EnableSef)
        {
            Results["AlbumId"] = (albumId > 0) ? albumId.ToString() : string.Empty;
            if (albumId > 0)
            {
                url = MakeLink(Parameters.Get<string>("SefFolder"), Parameters.Get<string>("Sef_Album"), Results);
            }
            else
            {
                url = MakeLink(Parameters.Get<string>("SefFolder"), string.Empty, Results);
            }
        }
        else
        {
            if (albumId > 0)
            {
                url = string.Format("{0}?{1}={2}",
                    Request.Path,
                    Parameters.Get<string>("ParamAlbum", "Album"),
                    albumId);
            }
            else
            {
                url = Request.Path;
            }
        }

        if (Parameters.Get<bool>("EnableAjax", false) && !IsComponentDesignMode)
        {
            string script = string.Format("setTimeout(\"PageTransfer('{0}');\",0);",
                url);
            ScriptManager.RegisterStartupScript(Page, typeof(Page), "AJAX_BUTTON_CLICK", script, true);
        }
        else
            Response.Redirect(url);

        return true;
        //return base.ProcessCommand(commandName, commandParameters, commandErrors);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (EnableSef)
        {
            MapVariable(Parameters.Get<string>("SEFFolder", "/photogallery"), Parameters.Get<string>("SEF_AlbumEdit"), Results);

            if(AlbumId > 0)
                BackUrl = MakeLink(Parameters.Get<string>("SefFolder"), Parameters.Get<string>("Sef_Album"), Results);
            else
                BackUrl = MakeLink(Parameters.Get<string>("SefFolder"), string.Empty, Results);
        }
        else
        {

            BXParamsBag<string> variableAlias = new BXParamsBag<string>();

            variableAlias["AlbumID"] = Parameters.Get<string>("ParamAlbumEdit", "AlbumEdit");

            MapVariable(variableAlias, Results);

            BackUrl = string.Format("{0}?{1}={2}",
                Request.Path,
                ParamAlbum,
                AlbumId);
            
        }

        if (BXInfoBlockManagerOld.GetById(Parameters.Get("IBlockId", 0)).IsUserCanOperate("IBlockModifyStructure"))
            CanModify = true;

        if (AlbumId > 0)
        {
            BXInfoBlockSectionOld sec = BXInfoBlockSectionManagerOld.GetById(AlbumId);

            if (BXInfoBlockManagerOld.GetById(sec.IBlockId).IsUserCanOperate("IBlockModifyStructure"))
                CanModify = true;

            Results["AlbumTitle"] = sec.Name;
            Results["AlbumDescription"] = sec.DescriptionRaw;
            Results["ParentAlbum"] = sec.ParentSectionId;
            ParentAlbumId = sec.ParentSectionId;
        }
        else
        {
            Results["ParentAlbum"] = 0;
        }

        List<BXInfoBlockSectionOld> sections = new List<BXInfoBlockSectionOld>();


        foreach (BXInfoBlockSectionOld s in BXInfoBlockSectionManagerOld.GetTree(Parameters.Get("IBlockId", 0), 0))
        {
            sections.Add(s);
        }

        Results["SECTIONS"] = sections;

        IncludeComponentTemplate();
    }

    private int? _parentAlbumId = null;
    public int? ParentAlbumId 
    {
        get { return _parentAlbumId; }
        protected set { _parentAlbumId = value; }
    }
}
