using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
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 TABS.MailCarrier.WebSDK.Contact;

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

    public Contact[] GetFavorites(string filter, string sortExpression, string searchKeyword)
    {
        if (sortExpression == "") sortExpression = "DisplayName";
        string rangeFilter = "";
        switch (filter)
        {
            case "ㄱ": rangeFilter = "ct_display >= 'ㄱ' AND ct_display < 'ㄴ'"; break;
            case "ㄴ": rangeFilter = "ct_display >= 'ㄴ' AND ct_display < 'ㄷ'"; break;
            case "ㄷ": rangeFilter = "ct_display >= 'ㄷ' AND ct_display < 'ㄹ'"; break;
            case "ㄹ": rangeFilter = "ct_display >= 'ㄹ' AND ct_display < 'ㅁ'"; break;
            case "ㅁ": rangeFilter = "ct_display >= 'ㅁ' AND ct_display < 'ㅂ'"; break;
            case "ㅂ": rangeFilter = "ct_display >= 'ㅂ' AND ct_display < 'ㅅ'"; break;
            case "ㅅ": rangeFilter = "ct_display >= 'ㅅ' AND ct_display < 'ㅇ'"; break;
            case "ㅇ": rangeFilter = "ct_display >= 'ㅇ' AND ct_display < 'ㅈ'"; break;
            case "ㅈ": rangeFilter = "ct_display >= 'ㅈ' AND ct_display < 'ㅊ'"; break;
            case "ㅊ": rangeFilter = "ct_display >= 'ㅊ' AND ct_display < 'ㅋ'"; break;
            case "ㅋ": rangeFilter = "ct_display >= 'ㅋ' AND ct_display < 'ㅌ'"; break;
            case "ㅌ": rangeFilter = "ct_display >= 'ㅌ' AND ct_display < 'ㅍ'"; break;
            case "ㅍ": rangeFilter = "ct_display >= 'ㅍ' AND ct_display < 'ㅎ'"; break;
            case "ㅎ": rangeFilter = "ct_display >= 'ㅎ' AND ct_display < '힣'"; break;
            case "etc": rangeFilter = "ct_display < 'ㄱ' OR ct_display > '힣'"; break;
        }

        Favorites favorites = new Favorites(WebUser.CurrentUser.DomainUserID);
        if (sortExpression.IndexOf("DisplayName") >= 0)
            sortExpression = sortExpression.Replace("DisplayName", "ct_display");
        else if (sortExpression.IndexOf("JobCompany") >= 0)
            sortExpression = sortExpression.Replace("JobCompany", "ct_job_company");
        else if (sortExpression.IndexOf("Email") >= 0)
            sortExpression = sortExpression.Replace("Email", "ct_email");

        if (searchKeyword != "")
        {
            string subQuery = string.Format("(ct_display LIKE '%{0}%' OR ct_email LIKE '%{0}%' OR ct_job_company LIKE '%{0}%' OR ct_job_phone LIKE '%{0}%' OR ct_job_fax LIKE '%{0}%' OR ct_home_mobile LIKE '%{0}%')", searchKeyword);
            if (rangeFilter != "") rangeFilter += " AND " + subQuery;
            else rangeFilter = subQuery;
        }

        return favorites.GetContacts(rangeFilter, sortExpression);
    }
}
