using System;
using System.Collections.Generic;
using System.Web;
using TABS.MailCarrier.StgBase;
using TABS.MailCarrier.WebSDK.Approval;

/// <summary>
/// Summary description for SearchedDocPersonDataSource
/// </summary>
public class SearchedDocPersonDataSource
{
    protected enum CategoryType
    {
        Code = 0x00000001,
        Subject = 0x00000002,
        Body = 0x00000004,
        Writer = 0x00000008
    }

    private int _totalCount;

    public SearchedDocPersonDataSource()
	{
	}

    public ApprovalDocPerson[] GetApvDocs(Guid domainId, string user, int documentType, Guid parentId, int category, string searchKeywords, int maximumRows, int startRowIndex)
    {
        string codeKeyword = "", subjectKeyword = "", bodyKeyword = "", writerKeyword = "";
        CategoryType type = (CategoryType)category;
        if ((type & CategoryType.Code) > 0)
        {
            codeKeyword = searchKeywords;
        }
        if ((type & CategoryType.Subject) > 0)
        {
            subjectKeyword = searchKeywords;
        }
        if ((type & CategoryType.Body) > 0)
        {
            bodyKeyword = searchKeywords;
        }
        if ((type & CategoryType.Writer) > 0)
        {
            writerKeyword = searchKeywords;
        }

        const int MaxKwCount = 3;       // 최대 검색 키워드(제목, 본문) 수
        int kwCount;
        string queries, query;
        string subQuery = (parentId != Guid.Empty) ? string.Format("app_parent='{0}'", parentId.ToString()) : "app_parent IS NULL";
        if (codeKeyword != "")
        {
            if (subQuery != "") subQuery += " AND ";
            subQuery += string.Format("(apd_code LIKE '%{0}%')", McspUtil.GetKeyword4Like(codeKeyword));
        }
        if (subjectKeyword != "")
        {
            if (subQuery != "") subQuery += " AND ";
            queries = "";
            kwCount = 0;
            string[] keywords = subjectKeyword.Split(new char[] { ' ' });
            foreach (string keyword in keywords)
            {
                if (keyword.Length == 0) continue;
                if (++kwCount > MaxKwCount) break;
                query = string.Format("(apd_subject LIKE '%{0}%')", McspUtil.GetKeyword4Like(keyword));
                if (queries.Length == 0) queries = query;
                else queries += " AND " + query;
            }
            subQuery += "(" + queries + ")";
        }
        if (bodyKeyword != "")
        {
            if (subQuery != "") subQuery += (subjectKeyword != "") ? " OR " : " AND ";
            queries = "";
            kwCount = 0;
            string[] keywords = bodyKeyword.Split(new char[] { ' ' });
            foreach (string keyword in keywords)
            {
                if (keyword.Length == 0) continue;
                if (++kwCount > MaxKwCount) break;
                if (AppSetting.UseFulltextSearch)
                    query = string.Format("(apd_id in (SELECT apd_id FROM ApvDocument WHERE CONTAINS(apd_searchText,'{0}')))", McspUtil.GetKeyword4FullText(keyword));
                else
                    query = string.Format("(apd_searchText LIKE '%{0}%')", McspUtil.GetKeyword4Like(keyword));
                if (queries.Length == 0) queries = query;
                else queries += " AND " + query;
            }
            subQuery += "(" + queries + ")";
        }
        if (writerKeyword != "")
        {
            if (subQuery != "") subQuery += " AND ";
            subQuery += string.Format("(apd_username LIKE '%{0}%' OR apd_userId LIKE '%{0}%')", McspUtil.GetKeyword4Like(writerKeyword));
        }

        if (subQuery == "")
            subQuery = "1=2 AND ";
        else
            subQuery = "(" + subQuery + ") AND ";

        ApprovalDocuments documents = new ApprovalDocuments(domainId, user);
        return documents.Search((ApprovalDocuments.EnumDocumentType)documentType, startRowIndex / maximumRows, maximumRows, subQuery, out _totalCount, parentId);
    }

    public int GetTotalCount(Guid domainId, string user, int documentType, Guid parentId, int category, string searchKeywords)
    {
        return _totalCount;
    }
}