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

namespace Bitrix.IBlock.Components
{
    public partial class IBlockMyElementsComponent : Bitrix.UI.BXComponent
    {
        #region Enums
        public enum MannerOfUserAssociation
        {
            NoBody = 1,
            CreatedBy,
            IBlockProperty
        }

        public enum MannerOfIssueModificationPermission
        {
            Active = 1,
            NotActive,
            Always
        }

        public enum ElementStatus
        {
            Active = 1,
            NotActive
        }
        #endregion

        #region Events
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            string templateName = "list";
            string action = string.Empty;

            if (EnableSef)
            {
                BXParamsBag<string> sefTemplates = new BXParamsBag<string>();
                sefTemplates.Add("list$page", SefTemplatePageIndex);
                sefTemplates.Add("list$all", SefTemplateShowAll);
                sefTemplates.Add("form$new", SefTemplateCreateElement);
                sefTemplates.Add("form$edit", SefTemplateModifyElement);


                string code = MapVariable(SefFolder, sefTemplates, ComponentCache, "list");

                int position = code.IndexOf('$');
                if (position > 0)
                {
                    templateName = code.Substring(0, position);
                    action = code.Substring(position + 1);
                }
                else
                {
                    templateName = code;
                    action = string.Empty;
                }


                if (string.Equals(templateName, "form", StringComparison.InvariantCultureIgnoreCase))
                {
                    if ((string.Equals(action, "edit", StringComparison.InvariantCultureIgnoreCase) && !PermitOfModification) ||
                        (string.Equals(action, "new", StringComparison.InvariantCultureIgnoreCase) && !PermitOfCreation)
                        )
                    {
                        templateName = "list";
                        action = string.Empty;
                    }
                }


                if (string.Equals(templateName, "list", StringComparison.InvariantCultureIgnoreCase))
                {
                    BXParamsBag<object> replaceItems = new BXParamsBag<object>(ComponentCache);
                    replaceItems.Remove("PageID");  

                    replaceItems["IblockId"] = replaceItems["IBLOCK_ID"] = IBlockId.ToString();

                    ComponentCache["PagingIndexTemplate"] = CombineLink(SefFolder, string.Empty);
                    ComponentCache["PagingPageTemplate"] = CombineLink(SefFolder, MakeLink(SefTemplatePageIndex, replaceItems));
                    ComponentCache["PagingShowAllTemplate"] = CombineLink(SefFolder, MakeLink(SefTemplateShowAll, replaceItems));
                    ComponentCache["ShowAll"] = string.Equals(code, "list$all", StringComparison.Ordinal) ? "y" : "n";
                    ComponentCache["ElementCreationUrl"] = CombineLink(SefFolder, MakeLink(SefTemplateCreateElement, replaceItems));
                    ComponentCache["ElementModificationUrl"] = CombineLink(SefFolder, MakeLink(SefTemplateModifyElement, replaceItems));
                }
                else if (string.Equals(templateName, "form", StringComparison.InvariantCultureIgnoreCase))
                    Parameters["RedirectPageUrl"] = CombineLink(SefFolder, string.Empty);
            }
            else
            {
                bool aboutCreateEl = false,
                        aboutModifyEl = false;
                string createElParName = RequestParamNameCreateElement,
                        modifyElParName = RequestParamNameModifyElement;

                if (!string.IsNullOrEmpty(modifyElParName))
                {
                    string elementID = Request.QueryString.Get(modifyElParName);
                    if (!string.IsNullOrEmpty(elementID))
                    {
                        aboutModifyEl = true;
                        ComponentCache["ElementID"] = elementID;
                    }
                }

                if (!aboutModifyEl && !string.IsNullOrEmpty(createElParName))
                {
                    int keyCount = Request.QueryString.Count;
                    for (int i = 0; i < keyCount; i++)
                    {
                        if (!string.Equals(Request.QueryString[i], createElParName, StringComparison.InvariantCultureIgnoreCase))
                            continue;
                        aboutCreateEl = true;
                        ComponentCache["ElementID"] = "0";
                        break;
                    }
                }

                if ((aboutModifyEl && PermitOfModification) || (aboutCreateEl && PermitOfCreation))
                {
                    templateName = "form";
                    Parameters["RedirectPageUrl"] = Request.FilePath;
                }
                else
                {
                    BXParamsBag<string> requestParamAliases = new BXParamsBag<string>();
                    requestParamAliases["PageID"] = RequestParamNamePageIndex;
                    requestParamAliases["ShowAll"] = RequestParamNameShowAll;
                    BXComponentManager.MapVariable(requestParamAliases, ComponentCache, true);

                    if (ComponentCache.ContainsKey("ShowAll") && string.IsNullOrEmpty(ComponentCache.GetString("ShowAll", null)))
                        ComponentCache["ShowAll"] = "y";

                    string filePath = Request.FilePath;

                    ComponentCache["PagingIndexTemplate"] = filePath;
                    ComponentCache["PagingPageTemplate"] = String.Format("{0}?{1}=#PageId#", filePath, RequestParamNamePageIndex);
                    ComponentCache["PagingShowAllTemplate"] = String.Format("{0}?{1}", filePath, RequestParamNameShowAll);

                    ComponentCache["ElementCreationUrl"] = !string.IsNullOrEmpty(createElParName) ? string.Concat("?", createElParName) : string.Empty;
                    ComponentCache["ElementModificationUrl"] = !string.IsNullOrEmpty(modifyElParName) ? string.Concat("?", modifyElParName, "=#ElementID#") : string.Empty;
                }
            }
            IncludeComponentTemplate(templateName);
        }
        #endregion

        #region Component
        /// <summary>
        /// ID инфоблока
        /// </summary>
        public int IBlockId
        {
            get { return Parameters.Get<int>("IBlockId"); }
        }

        ///// <summary>
        ///// Флаг включения режима ЧПУ
        ///// </summary>
        //public bool EnableSef
        //{
        //    get { return Parameters.Get<bool>("EnableSef", false); }
        //}

        /// <summary>
        ///Каталог ЧПУ (относительно корня сайта)
        /// </summary>
        public string SefFolder
        {
            get { return Parameters.Get<string>("SEFFolder", string.Empty); }
        }

        /// <summary>
        /// Псевдоним параметра запроса для флага создания нового эл-та.
        /// </summary>
        public string RequestParamNameCreateElement
        {
            get { return Parameters.Get<string>("RequestParamNameCreateElement", "new"); }
        }

        /// <summary>
        /// Псевдоним параметра запроса для ID редактируемого эл-та.
        /// </summary>
        public string RequestParamNameModifyElement
        {
            get { return Parameters.Get<string>("RequestParamNameModifyElement", "id"); }
        }

        /// <summary>
        /// Псевдоним параметра запроса для индекса страницы в списке эл-тов
        /// </summary>
        public string RequestParamNamePageIndex
        {
            get { return Parameters.Get<string>("RequestParamNamePageIndex", "page"); }
        }

        /// <summary>
        /// Псевдоним параметра запроса для флага отображения всех эл-тов
        /// </summary>
        public string RequestParamNameShowAll
        {
            get { return Parameters.Get<string>("RequestParamNameShowAll", "all"); }
        }

        /// <summary>
        /// ЧПУ-шаблон для сценария создания эл-та
        /// </summary>
        public string SefTemplateCreateElement
        {
            get { return Parameters.Get<string>("SefTemplateCreateElement", "/new/"); }
        }

        /// <summary>
        /// ЧПУ-шаблон для сценария редактирования эл-та
        /// </summary>
        public string SefTemplateModifyElement
        {
            get { return Parameters.Get<string>("SefTemplateModifyElement", "/edit-#ElementId#/"); }
        }

        /// <summary>
        /// ЧПУ-шаблон для сценария отображения страницы в списке эл-тов
        /// </summary>
        public string SefTemplatePageIndex
        {
            get { return Parameters.Get<string>("SefTemplatePageIndex", "/page-#PageId#"); }
        }

        /// <summary>
        /// ЧПУ-шаблон для сценария отображения всех эл-тов в списке
        /// </summary>
        public string SefTemplateShowAll
        {
            get { return Parameters.Get<string>("SefTemplateShowAll", "/all"); }
        }

        public bool PermitOfCreation
        {
            get
            {
                object rObj = null;
                if (ComponentCache.TryGetValue("PermitOfCreation", out rObj))
                    return (bool)rObj;

                bool r = Parameters.Get<bool>("PermitOfCreation", false);
                ComponentCache["PermitOfCreation"] = r;
                return r;
            }
        }

        public bool PermitOfModification
        {
            get
            {
                object rObj = null;
                if (ComponentCache.TryGetValue("PermitOfModification", out rObj))
                    return (bool)rObj;

                bool r = Parameters.Get<bool>("PermitOfModification", false);
                ComponentCache["PermitOfModification"] = r;
                return r;
            }
        }

        public bool PermitOfDeletion
        {
            get
            {
                object rObj = null;
                if (ComponentCache.TryGetValue("PermitOfDeletion", out rObj))
                    return (bool)rObj;

                bool r = Parameters.Get("PermitOfDeletion", false);
                ComponentCache["PermitOfDeletion"] = r;
                return r;
            }
        }

        protected override void PreLoadComponentDefinition()
        {
            Title = GetMessageRaw("Title");
            Description = GetMessageRaw("Description");
            Icon = "images/eadd.gif";
            Group = new BXComponentGroup("iblock.elements", GetMessageRaw("Group"), 100, BXComponentGroup.Content);

            //BXCategory dataSourceCategory = BXCategory.DataSource;
            BXCategory mainCategory = BXCategory.Main;
            BXCategory sefCategory = BXCategory.Sef;
            BXCategory acessCategory = new BXCategory(GetMessageRaw("Category.Access"), "ACCESS", 110);
            BXCategory listSettings = BXCategory.ListSettings;
            listSettings.Sort = 120;
            BXCategory webFormSettings = new BXCategory(GetMessageRaw("Category.WebFormGeneral"), "WEB_FORM_GENERAL_SETTINGS", 130);
            BXCategory fieldSettingsCategory = new BXCategory(GetMessageRaw("Category.WebFormFields"), "WEB_FORM_FIELD_SETTINGS", 140);
            BXCategory customNameCategory = new BXCategory(GetMessageRaw("Category.WebFormCustomNames"), "WEB_FORM_CUSTOM_NAME_SETTINGS", 150);

            ParamsDefinition.Add(BXParametersDefinition.Paging);

            BXParamsBag<BXParam> sefParBag = BXParametersDefinition.Sef;
            sefParBag["EnableSEF"].ClientSideAction = new ParamClientSideActionGroupViewSwitch(ClientID, "EnableSEF", "Sef", "NonSef");
            sefParBag["SEFFolder"].ClientSideAction = new ParamClientSideActionGroupViewMember(ClientID, "SEFFolder", new string[] { "Sef" });
            ParamsDefinition.Add(sefParBag);

            #region Main
            ParamsDefinition.Add(
                "IBlockTypeId",
                new BXParamSingleSelection(
                    GetMessageRaw("InfoBlockType"),
                    String.Empty,
                    mainCategory
                ));

            ParamsDefinition.Add(
                "IBlockId",
                new BXParamSingleSelection(
                    GetMessageRaw("InfoBlockCode"),
                    String.Empty,
                    mainCategory
                ));

            #endregion

            #region Access
            BXRole admin = BXRoleManager.GetById(1);
            ParamsDefinition.Add(
                "RolesAuthorizedToManage",
                new BXParamMultiSelection(
                    GetMessageRaw("RolesAuthorizedToManage"),
                    admin != null ? admin.RoleName : string.Empty,
                    acessCategory
                ));

            ParamsDefinition.Add(
                "MannerOfUserAssociation",
                new BXParamSingleSelection(
                    GetMessageRaw("MannerOfUserAssociation"),
                    Enum.GetName(typeof(MannerOfUserAssociation), MannerOfUserAssociation.CreatedBy),
                    acessCategory,
                    null,
                    new ParamClientSideActionGroupViewSelector(ClientID, "MannerOfUserAssociation")
                ));

            ParamsDefinition.Add(
                "UserAssociatedByCustomIBlockProperty",
                new BXParamSingleSelection(
                    GetMessageRaw("UserAssociatedByCustomIBlockProperty"),
                    string.Empty,
                    acessCategory,
                    null,
                    new ParamClientSideActionGroupViewMember(ClientID, "UserAssociatedByCustomIBlockProperty", new string[] { Enum.GetName(typeof(MannerOfUserAssociation), MannerOfUserAssociation.IBlockProperty) })
                ));

            ParamsDefinition.Add(
                "PermitOfCreation",
                new BXParamYesNo(
                    GetMessageRaw("PermitOfCreation"),
                    false,
                    acessCategory,
                    new ParamClientSideActionGroupViewSwitch(ClientID, "PermitOfCreation", "PermitOfElementCreation", string.Empty)
                ));

            ParamsDefinition.Add(
                "MaxUserElements",
                new BXParamText(
                    GetMessageRaw("MaxUserElements"),
                    "15",
                    acessCategory,
                    new ParamClientSideActionGroupViewMember(ClientID, "MaxUserElements", new string[] { "PermitOfElementCreation" })
                ));

            ParamsDefinition.Add(
                "PermitOfModification",
                new BXParamYesNo(
                    GetMessageRaw("PermitOfModification"),
                    false,
                    acessCategory,
                    new ParamClientSideActionGroupViewSwitch(ClientID, "PermitOfModification", "PermitOfElementModification", string.Empty)
                ));

            ParamsDefinition.Add(
                "MannerOfIssueModificationPermission",
                new BXParamSingleSelection(
                    GetMessageRaw("MannerOfIssueModificationPermission"),
                    Enum.GetName(typeof(MannerOfIssueModificationPermission), MannerOfIssueModificationPermission.Active),
                    acessCategory,
                    null,
                    new ParamClientSideActionGroupViewMember(ClientID, "MannerOfIssueModificationPermission", new string[] { "PermitOfElementModification" })
                ));

            ParamsDefinition.Add(
                "ElementActiveAfterSave",
                new BXParamSingleSelection(
                    GetMessageRaw("ElementActiveAfterSave"),
                    Enum.GetName(typeof(MannerOfIssueModificationPermission), MannerOfIssueModificationPermission.Active),
                    acessCategory
                ));

            ParamsDefinition.Add(
                "PermitOfDeletion",
                new BXParamYesNo(
                    GetMessageRaw("PermitOfDeletion"),
                    false,
                    acessCategory
                ));
            #endregion

            #region List
            ParamsDefinition.Add(
                "SortBy",
                new BXParamSingleSelection(
                    GetMessageRaw("SortBy"),
                    "Id",
                    listSettings
                ));

            ParamsDefinition.Add(
                "SortOrder",
                new BXParamSort(
                    GetMessageRaw("SortOrder"),
                    true,
                    listSettings
                ));
            #endregion

            #region WebForm
            ParamsDefinition.Add(
                "EditFields",
                new BXParamDoubleList(
                    GetMessageRaw("EditFields"),
                    "Name",
                    webFormSettings
                ));

            ParamsDefinition.Add(
                "RequiredFields",
                new BXParamMultiSelection(
                    GetMessageRaw("RequiredFields"),
                    string.Empty,
                    webFormSettings
                ));

            ParamsDefinition.Add(
                "TextBoxSize",
                new BXParamText(
                    GetMessageRaw("TextBoxSize"),
                    "30",
                    webFormSettings
                ));

            ParamsDefinition.Add(
                "CreateButtonTitle",
                    new BXParamText(
                    GetMessageRaw("CreateButtonTitle"),
                    GetMessageRaw("TitleAddElement"),
                    webFormSettings
                ));

            ParamsDefinition.Add(
                "UpdateButtonTitle",
                    new BXParamText(
                    GetMessageRaw("UpdateButtonTitle"),
                    GetMessageRaw("TitleSaveElement"),
                    webFormSettings
                ));

			//ParamsDefinition.Add(
			//    "SuccessMessageAfterCreateElement",
			//    new BXParamText(
			//        GetMessageRaw("SuccessMessageAfterCreateElement"),
			//        GetMessageRaw("MessageElementHasBeenCreatedSuccessfully"),
			//        webFormSettings
			//    ));

			//ParamsDefinition.Add(
			//    "SuccessMessageAfterUpdateElement",
			//    new BXParamText(
			//        GetMessageRaw("SuccessMessageAfterUpdateElement"),
			//        GetMessageRaw("MessageElementHasBeenUpdatedSuccessfully"),
			//        webFormSettings
			//    ));

            //ParamsDefinition.Add(
            //    "RedirectPageUrl",
            //    new BXParamText(
            //        GetMessageRaw("RedirectPageUrl"),
            //        string.Empty,
            //        webFormSettings
            //    ));
            #endregion

            #region Field
            ParamsDefinition.Add(
                "MaxSectionSelect",
                new BXParamText(
                    GetMessageRaw("MaxSectionSelect"),
                    "3",
                    fieldSettingsCategory
                ));

            ParamsDefinition.Add(
                "OnlyLeafSelect",
                new BXParamYesNo(
                    GetMessageRaw("OnlyLeafSelect"),
                    false,
                    fieldSettingsCategory
                ));

            ParamsDefinition.Add(
                "MaxFileSizeUpload",
                new BXParamText(
                    GetMessageRaw("MaxFileSizeUpload"),
                    "1024",
                    fieldSettingsCategory
                ));

            ParamsDefinition.Add(
                "ActiveFromDateShowTime",
                new BXParamYesNo(
                    GetMessageRaw("ActiveFromDateShowTime"),
                    false,
                    fieldSettingsCategory
                ));

            ParamsDefinition.Add(
                "ActiveToDateShowTime",
                new BXParamYesNo(
                    GetMessageRaw("ActiveToDateShowTime"),
                    false,
                    fieldSettingsCategory
                ));

            ParamsDefinition.Add(
                "NameFieldMacros",
                    new BXParamText(
                    GetMessageRaw("NameFieldMacros"),
                    "#DetailText#-#DateCreate#-#CreatedBy#",
                    fieldSettingsCategory
                ));
            #endregion

            #region CustomName
            ParamsDefinition.Add(
                "NameCustomTitle",
                new BXParamText(
                    GetMessageRaw("NameCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));

            ParamsDefinition.Add(
                "ActiveFromDateCustomTitle",
                new BXParamText(
                    GetMessageRaw("ActiveFromDateCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));

            ParamsDefinition.Add(
                "ActiveToDateCustomTitle",
                new BXParamText(
                    GetMessageRaw("ActiveToDateCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));

            ParamsDefinition.Add(
                "SectionsCustomTitle",
                new BXParamText(
                    GetMessageRaw("SectionsCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));

            ParamsDefinition.Add(
                "PreviewTextCustomTitle",
                new BXParamText(
                    GetMessageRaw("PreviewTextCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));

            ParamsDefinition.Add(
                "PreviewImageCustomTitle",
                new BXParamText(
                    GetMessageRaw("PreviewPictureCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));

            ParamsDefinition.Add(
                "DetailTextCustomTitle",
                new BXParamText(
                    GetMessageRaw("DetailTextCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));


            ParamsDefinition.Add(
                "DetailImageCustomTitle",
                new BXParamText(
                    GetMessageRaw("DetailPictureCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));


            ParamsDefinition.Add(
                "CaptchaCustomTitle",
                new BXParamText(
                    GetMessageRaw("CaptchaCustomTitle"),
                    String.Empty,
                    customNameCategory
                ));
            #endregion

            #region Sef
            ParamsDefinition.Add(
                "RequestParamNameCreateElement",
                new BXParamText(
                    GetMessageRaw("RequestParamNameCreateElement"),
                    "new",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "RequestParamNameCreateElement", new string[] { "NonSef" })
                ));

            ParamsDefinition.Add(
                "RequestParamNameModifyElement",
                new BXParamText(
                    GetMessageRaw("RequestParamNameModifyElement"),
                    "id",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "RequestParamNameModifyElement", new string[] { "NonSef" })
                ));

            ParamsDefinition.Add(
                "RequestParamNamePageIndex",
                new BXParamText(
                    GetMessageRaw("RequestParamNamePageIndex"),
                    "page",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "RequestParamNamePageIndex", new string[] { "NonSef" })
                ));

            ParamsDefinition.Add(
                "RequestParamNameShowAll",
                new BXParamText(
                    GetMessageRaw("RequestParamNameShowAll"),
                    "all",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "RequestParamNameShowAll", new string[] { "NonSef" })
                ));


            ParamsDefinition.Add(
                "SefTemplateCreateElement",
                new BXParamText(
                    GetMessageRaw("SefPathCreateElement"),
                    "/new/",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "SefTemplateCreateElement", new string[] { "Sef" })
                ));

            ParamsDefinition.Add(
                "SefTemplateModifyElement",
                new BXParamText(
                    GetMessageRaw("SefPathModifyElement"),
                    "/edit-#ElementId#/",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "SefTemplateModifyElement", new string[] { "Sef" })
                ));

            ParamsDefinition.Add(
                "SefTemplatePageIndex",
                new BXParamText(
                    GetMessageRaw("SefPathPageIndex"),
                    "/page-#PageId#",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "SefTemplatePageIndex", new string[] { "Sef" })
                ));

            ParamsDefinition.Add(
                "SefTemplateShowAll",
                new BXParamText(
                    GetMessageRaw("SefPathShowAll"),
                    "/all",
                    BXCategory.Sef,
                    new ParamClientSideActionGroupViewMember(ClientID, "SefTemplateShowAll", new string[] { "Sef" })
                ));
            #endregion
        }

        protected override void LoadComponentDefinition()
        {
            #region Properties
            List<BXParamValue> properties = new List<BXParamValue>(),
                                propertiesForUserReference = new List<BXParamValue>();
            int selectedIblockId = 0;
            if (Parameters.ContainsKey("IBlockId"))
                int.TryParse(Parameters["IBlockId"], out selectedIblockId);

            if (selectedIblockId > 0)
            {
                BXCustomFieldCollection customFields = BXIBlock.GetCustomFields(selectedIblockId);
                foreach (BXCustomField customField in customFields)
                {
                    string title = BXTextEncoder.HtmlTextEncoder.Decode(customField.EditFormLabel);
                    string code = customField.Name.ToUpper();
                    properties.Add(new BXParamValue(title, code));
                    if (string.Equals(customField.CustomTypeId, "Bitrix.System.Int", StringComparison.Ordinal))
                        propertiesForUserReference.Add(new BXParamValue(title, code));
                }
            }
            #endregion

            #region Iblock type
            List<BXParamValue> typeParamValue = new List<BXParamValue>();
            typeParamValue.Add(new BXParamValue(GetMessageRaw("SelectIBlockType"), string.Empty));

            BXIBlockTypeCollection iblockTypes = BXIBlockType.GetList(null, new BXOrderBy(new BXOrderByPair(BXIBlockType.Fields.Name, BXOrderByDirection.Asc)), null, null, BXTextEncoder.EmptyTextEncoder);
            foreach (BXIBlockType iblockType in iblockTypes)
                typeParamValue.Add(new BXParamValue(iblockType.Translations[BXLoc.CurrentLocale].Name, iblockType.Id.ToString()));

            ParamsDefinition["IBlockTypeId"].Values = typeParamValue;
            ParamsDefinition["IBlockTypeId"].RefreshOnDirty = true;
            #endregion

            #region Iblock
            int selectedIBlockType = 0;
            if (Parameters.ContainsKey("IBlockTypeId"))
                int.TryParse(Parameters["IBlockTypeId"], out selectedIBlockType);

            BXFilter filter = new BXFilter();
            if (selectedIBlockType > 0)
                filter.Add(new BXFilterItem(BXIBlock.Fields.Type.ID, BXSqlFilterOperators.Equal, selectedIBlockType));
            if (!string.IsNullOrEmpty(DesignerSite))
                filter.Add(new BXFilterItem(BXIBlock.Fields.Sites.SiteId, BXSqlFilterOperators.Equal, DesignerSite));

            List<BXParamValue> iblockParamValue = new List<BXParamValue>();
            iblockParamValue.Add(new BXParamValue(GetMessageRaw("SelectIBlockID"), string.Empty));
            BXIBlockCollection iblocks = BXIBlock.GetList(filter, new BXOrderBy(new BXOrderByPair(BXIBlock.Fields.Name, BXOrderByDirection.Asc)), null, null, BXTextEncoder.EmptyTextEncoder);
            foreach (BXIBlock iblock in iblocks)
                iblockParamValue.Add(new BXParamValue(iblock.Name, iblock.Id.ToString()));

            ParamsDefinition["IBlockId"].Values = iblockParamValue;
            ParamsDefinition["IBlockId"].RefreshOnDirty = true;
            #endregion

            #region Sorting
            List<BXParamValue> sortingFields = new List<BXParamValue>();
            sortingFields.Add(new BXParamValue(GetMessageRaw("ElementID"), "ID"));
            sortingFields.Add(new BXParamValue(GetMessageRaw("ElementName"), "Name"));
            sortingFields.Add(new BXParamValue(GetMessageRaw("ActiveFromDate"), "ActiveFromDate"));
            sortingFields.Add(new BXParamValue(GetMessageRaw("ActiveUntilDate"), "ActiveToDate"));
            sortingFields.Add(new BXParamValue(GetMessageRaw("SortIndex"), "Sort"));
            sortingFields.Add(new BXParamValue(GetMessageRaw("DateOfModification"), "UpdateDate"));
            sortingFields.AddRange(properties);

            ParamsDefinition["SortBy"].Values = sortingFields;
            #endregion

            #region Access
            //RolesAuthorizedToManage
            IList<BXParamValue> authRolesValues = ParamsDefinition["RolesAuthorizedToManage"].Values;
            if (authRolesValues.Count > 0)
                authRolesValues.Clear();

            BXRoleCollection roles = BXRoleManager.GetList(new BXFormFilter(new BXFormFilterItem("Active", true, BXSqlFilterOperators.Equal)), new BXOrderBy_old("RoleName", "Asc"));
            for (int i = 0; i < roles.Count; i++)
            {
                BXRole role = roles[i];
                authRolesValues.Add(new BXParamValue(role.Title, role.RoleName));
            }
            //---
            //MannerOfIssueModificationPermission
            IList<BXParamValue> manOfIssueModificationPermissionValues = ParamsDefinition["MannerOfIssueModificationPermission"].Values;
            if (manOfIssueModificationPermissionValues.Count > 0)
                manOfIssueModificationPermissionValues.Clear();

            string[] manOfIssueModificationPermissionNames = Enum.GetNames(typeof(MannerOfIssueModificationPermission));
            int manOfIssueModificationPermissionNamesCount = manOfIssueModificationPermissionNames != null ? manOfIssueModificationPermissionNames.Length : 0;
            for (int j = 0; j < manOfIssueModificationPermissionNamesCount; j++)
            {
                string name = manOfIssueModificationPermissionNames[j];
                manOfIssueModificationPermissionValues.Add(new BXParamValue(GetMessageRaw(string.Concat("MannerOfIssueModificationPermission", name)), name));
            }
            //---
            //MannerOfUserAssociation
            IList<BXParamValue> mannerOfUserAssociationValues = ParamsDefinition["MannerOfUserAssociation"].Values;
            if (mannerOfUserAssociationValues.Count > 0)
                mannerOfUserAssociationValues.Clear();

            string[] mannerOfUserAssociationNames = Enum.GetNames(typeof(MannerOfUserAssociation));
            int mannerOfUserAssociationNamesCount = mannerOfUserAssociationNames != null ? mannerOfUserAssociationNames.Length : 0;
            for (int k = 0; k < mannerOfUserAssociationNamesCount; k++)
            {
                string name = mannerOfUserAssociationNames[k];
                mannerOfUserAssociationValues.Add(new BXParamValue(GetMessageRaw(string.Concat("MannerOfUserAssociation", name)), name));
            }
            //---
            //UserAssociatedByCustomIBlockProperty
            IList<BXParamValue> userAssociatedByCustomIBlockPropertyValues = ParamsDefinition["UserAssociatedByCustomIBlockProperty"].Values;
            if (userAssociatedByCustomIBlockPropertyValues.Count > 0)
                userAssociatedByCustomIBlockPropertyValues.Clear();

            userAssociatedByCustomIBlockPropertyValues.Add(new BXParamValue(GetMessageRaw("NotSelected"), string.Empty));
            for (int n = 0; n < propertiesForUserReference.Count; n++)
                userAssociatedByCustomIBlockPropertyValues.Add(propertiesForUserReference[n]);
            //---

            string[] elementStatusNames = Enum.GetNames(typeof(ElementStatus));
            IList<BXParamValue> elementStatusValues = ParamsDefinition["ElementActiveAfterSave"].Values;
            if (elementStatusValues.Count > 0)
                elementStatusValues.Clear();
            int elementStatusValuesCount = elementStatusNames != null ? elementStatusNames.Length : 0;
            for (int m = 0; m < elementStatusValuesCount; m++)
            {
                string name = elementStatusNames[m];
                elementStatusValues.Add(new BXParamValue(GetMessageRaw(string.Concat("ElementStatus", name)), name));
            }
            #endregion

            #region WebForm
            List<BXParamValue> fields = new List<BXParamValue>();
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionName"), "Name"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionActiveFromDate"), "ActiveFromDate"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionActiveToDate"), "ActiveToDate"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionSections"), "Sections"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionPreviewText"), "PreviewText"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionPreviewPicture"), "PreviewImage"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionDetailText"), "DetailText"));
            fields.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionDetailPicture"), "DetailImage"));

            IList<BXParamValue> requiredFieldsValues = ParamsDefinition["RequiredFields"].Values;
            if (requiredFieldsValues.Count > 0)
                requiredFieldsValues.Clear();

            IList<BXParamValue> editableFieldsValues = ParamsDefinition["EditFields"].Values;
            if (editableFieldsValues.Count > 0)
                editableFieldsValues.Clear();

            int fieldCount = fields.Count;
            for (int n = 0; n < fieldCount; n++)
            {
                requiredFieldsValues.Add(fields[n]);
                editableFieldsValues.Add(fields[n]);
            }


            editableFieldsValues.Add(new BXParamValue(GetMessageRaw("ElementFieldCaptionCaptcha"), "Captcha"));
            if (selectedIblockId > 0)
            {
                BXCustomFieldCollection customFields = BXIBlock.GetCustomFields(selectedIblockId);
                foreach (BXCustomField customField in customFields)
                {
                    string title = BXTextEncoder.HtmlTextEncoder.Decode(customField.EditFormLabel);
                    string code = customField.Name.ToUpper();
                    editableFieldsValues.Add(new BXParamValue(title, string.Concat("PROPERTY_", code)));
                }
            }
            #endregion

        }
        #endregion
    }

}
