using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

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


/// <summary>
/// Class used by scheduler to execute the task.
/// </summary>  
public class MyMediaSearchTaskExecutor : ITask
{
    /// <summary>
    /// Processes search tasks (starts indexer).
    /// </summary>
    /// <param name="task">Task to start</param>
    public string Execute(TaskInfo task)
    {
            try
            {
                // Get all search tasks with ready status
                string where = "SearchTaskStatus = N'" + MyMediaSearchHelper.MYMEDIAINDEX_STATUS_READY + "'";

                // Load top X tasks from database ordered by priority
                DataSet ds = SearchTaskInfoProvider.GetSearchTasks(null, where, "SearchTaskPriority DESC, SearchTaskID", MyMediaSearchHelper.TASK_INDEXER_RETRIEVE_ROWS_COUNT);
                SearchIndexInfo sii = SearchIndexInfoProvider.GetSearchIndexInfo(MyMediaSearchHelper.MYMEDIASEARCH_NAME);

                if (sii != null)
                {
                    // If dataset empty nothing to do
                    while (!DataHelper.DataSourceIsEmpty(ds))
                    {
                        // Go trough all search tasks
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            // Create task info obejct
                            SearchTaskInfo sti = new SearchTaskInfo(dr);
                            bool delete = false;

                            switch (sti.GetStringValue("SearchTaskType", ""))
                            {
                                case MyMediaSearchHelper.MYMEDIAINDEX_TASK_TYPE_UPDATE:
                                    int fileId = ValidationHelper.GetInteger(sti.SearchTaskValue, 0);
                                    if (fileId > 0)
                                    {
                                        try
                                        {
                                            // Get media file info
                                            MediaFileInfo mfi = MediaFileInfoProvider.GetMediaFileInfo(fileId);
                                            // Update media file info in index file
                                            MyMediaSearchHelper.UpdateIndexItem(mfi, sii);
                                            
                                            // Field was updated successfully - detete it
                                            delete = true;
                                        }
                                        catch { }
                                     }
                                    break;

                                default:
                                    break;
                            }

                            if (delete)
                            {
                                // Delete processed task
                                SearchTaskInfoProvider.DeleteSearchTaskInfo(sti);
                            }
                            else
                            {
                                // Set status of task to process
                                sti.SetValue("SearchTaskStatus", MyMediaSearchHelper.MYMEDIAINDEX_STATUS_PROCESS);
                                SearchTaskInfoProvider.SetSearchTaskInfo(sti);
                            }
                        }

                        ds.Dispose();
                        // Retrieve new dataset with tasks
                        ds = SearchTaskInfoProvider.GetSearchTasks(null, where, "SearchTaskPriority DESC, SearchTaskID", MyMediaSearchHelper.TASK_INDEXER_RETRIEVE_ROWS_COUNT);
                    }
                    return string.Empty;
                }
                else
                {
                    return "MyMediaIndex not found!";
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }   
    }
}
