using System;

using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.MediaLibrary;
using CMS.Ecommerce;

using Lucene.Net.Index;
using Lucene.Net.Documents;

using org.pdfbox.pdmodel;
using org.pdfbox.util;

/// <summary>
/// Sample MyMediaFileInfoHandler class.
/// </summary>
[MyMediaFileInfoHandler]
public partial class CMSModuleLoader
{
    #region "Macro methods loader attribute"

    /// <summary>
    /// Module registration
    /// </summary>
    private class MyMediaFileInfoHandler : CMSLoaderAttribute
    {
        /// <summary>
        /// Initializes the module
        /// </summary>
        public override void Init()
        {
            // Insert After handler
            MediaFileInfo.TYPEINFO.Events.Insert.After += new EventHandler<ObjectEventArgs>(Update_Index);
            // Update After handler
            MediaFileInfo.TYPEINFO.Events.Update.After += new EventHandler<ObjectEventArgs>(Update_Index);
            // Delete After handler
            MediaFileInfo.TYPEINFO.Events.Delete.After += new EventHandler<ObjectEventArgs>(Delete_After);
        }
        

        /// <summary>
        /// After Update MediaFileInfo event handler
        /// </summary>
        void Delete_After(object sender, ObjectEventArgs e)
        {
            // Update search index for media library info if smart search is enabled and custom search index exists 
            if (SearchIndexInfoProvider.SearchEnabled && SearchIndexInfoProvider.SearchTypeEnabled(SearchHelper.CUSTOM_SEARCH_INDEX))
            {
                // Get media file info
                MediaFileInfo mfi = e.Object as MediaFileInfo;
                // Create delete task
                SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Delete, SearchHelper.CUSTOM_SEARCH_INDEX, MyMediaSearchHelper.CUSTOM_ID_FIELD, mfi.FileID.ToString());
            }
        }


        /// <summary>
        /// Update index MediaFileInfo event handler
        /// </summary>
        void Update_Index(object sender, ObjectEventArgs e)
        {
            // Update search index for media library info if smart search is enabled and custom search index exists 
            if (SearchIndexInfoProvider.SearchEnabled && SearchIndexInfoProvider.SearchTypeEnabled(SearchHelper.CUSTOM_SEARCH_INDEX))
            {
                // Get media file info
                MediaFileInfo mfi = e.Object as MediaFileInfo;

                // Create new UPDATE search task
                SearchTaskInfo sti = new SearchTaskInfo();
                sti.SetValue("SearchTaskType", MyMediaSearchHelper.MYMEDIAINDEX_TASK_TYPE_UPDATE);
                sti.SetValue("SearchTaskStatus", MyMediaSearchHelper.MYMEDIAINDEX_STATUS_READY);
                sti.SearchTaskObjectType = SearchHelper.CUSTOM_SEARCH_INDEX;
                sti.SearchTaskField = MyMediaSearchHelper.CUSTOM_ID_FIELD;
                sti.SearchTaskValue = mfi.FileID.ToString();
                // If rebuild then 1 (higher priority)
                sti.SearchTaskPriority = 0;
                // Set creation time
                sti.SetValue("SearchTaskCreated", DateTime.Now);
                
                // Insert task into database
                sti.Generalized.InsertData();
            }
        }
    }

    #endregion
}
