using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TABS.MailCarrier.WebSDK.Filebox;

/// <summary>
/// Filebox 파일 목록을 GridView에 출력하기 위한 DataSource 클래스
/// </summary>
public class FileboxDataSource
{
    public FileboxDataSource()
    {
    }

    public FileboxFile[] GetFileboxes(string folder, bool noAccess, string sortExpression)
    {
        if (!noAccess)
        {
            FileboxFolder fbFolder = new FileboxFolder(new Guid(folder));

            if (sortExpression.IndexOf("Name") >= 0)
            {
                sortExpression = sortExpression.Replace("Name", "fl_name");
            }
            else if (sortExpression.IndexOf("Description") >= 0)
            {
                sortExpression = sortExpression.Replace("Description", "fl_description");
            }
            else if (sortExpression.IndexOf("Size") >= 0)
            {
                sortExpression = sortExpression.Replace("Size", "fl_size");
            }
            else if (sortExpression.IndexOf("CreateTime") >= 0)
            {
                sortExpression = sortExpression.Replace("CreateTime", "fl_createtime");
            }
            else if (sortExpression.IndexOf("Creator") >= 0)
            {
                sortExpression = sortExpression.Replace("Creator", "fl_creatorName");
            }
            else
            {
                sortExpression = "fl_createtime desc";
            }

            return fbFolder.GetFiles("", sortExpression);
        }
        else
        {
            return null;
        }
    }

    public void DeleteFile(object id)
    {
        Guid fileId = (Guid)id;
        FileboxFile file = new FileboxFile(fileId);
        file.Remove();
    }
}
