using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TABS.MailCarrier.WebSDK;
using TABS.MailCarrier.WebSDK.Board;

public partial class Board_List2 : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string defLang = WebUser.CurrentUser.UiLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(defLang);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(defLang);
        base.InitializeCulture();
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        WebUtil.SetMcCommonCss(Page.Header);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = AppSetting.GetTitleInfo(Request).Title;

        if (!IsPostBack)
        {
            ArticlesGridView.Columns[0].HeaderText = Resources.Board.SearchAllType;
            int pageIndex = WebParam.GetIntParamValue("page");
            ArticlesGridView.PageIndex = pageIndex;
            CategoryList.SelectedValue = WebParam.GetStrParamValue("searchCategoty");
            KeywordTextBox.Text = WebParam.GetStrParamValue("searchKeyword");
            SetDataSoutceParameter();
        }
    }

    private void SetDataSoutceParameter()
    {
        SearchAllArticlesDataSource.SelectParameters[0].DefaultValue = CategoryList.SelectedValue;
        SearchAllArticlesDataSource.SelectParameters[1].DefaultValue = KeywordTextBox.Text.Trim();
    }

    #region Page Event Methods

    protected void ArticlesGridView_PreRender(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;
        if ((gv.ShowHeader == true && gv.Rows.Count > 0) || (gv.ShowHeaderWhenEmpty == true))
        {
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        if (gv.ShowFooter == true && gv.Rows.Count > 0)
        {
            gv.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

    protected void SearchButton_Click(object sender, EventArgs e)
    {
        if (!AppSetting.PassDisplayBoardFilter(WebUser.CurrentUser.MailAddress)) return;

        string keyword = KeywordTextBox.Text.Trim();
        SetDataSoutceParameter();
        ArticlesGridView.PageIndex = 0;
        ArticlesGridView.DataBind();
    }

    protected void SearchCancelButton_Click(object sender, EventArgs e)
    {
        KeywordTextBox.Text = "";       // <- DataBind() 전 호출 필수
        SetDataSoutceParameter();
        ArticlesGridView.PageIndex = 0;
        ArticlesGridView.DataBind();
    }

    #endregion

    #region ArticlesGridView Methods

    protected string GetSubject(Article article)
    {
        StringBuilder sb = new StringBuilder();
        if ((article.Flag & Article.AttrFlag.FixedTop) > 0)
        {
            sb.Append(string.Format("<span class='text-primary'><strong>{0}</strong></span>", Server.HtmlEncode(article.Subject)));
            if (article.CommentCount > 0)
                sb.Append(string.Format(" <span class='text-primary'><strong>[{0}]</strong></span>", article.CommentCount));
        }
        else
        {
            bool newArticle = false;
            if (!article.Read)
            {
                TimeSpan timeSpan = DateTime.UtcNow - article.Date;
                if (timeSpan.TotalDays < 14) newArticle = true;
            }
            if (article.Depth > 0)
            {
                sb.Append(string.Format("<span class=\"fa-solid fa-arrow-turn-up fa-rotate-90\" style=\"margin-left:{0}px;margin-right:6px;\"></span>", article.Depth * 8));
            }

            if (newArticle)
            {
                sb.Append(string.Format("<span><strong>{0}</strong></span>", Server.HtmlEncode(article.Subject)));
                if (article.CommentCount > 0)
                    sb.Append(string.Format(" <span class='text-success'><strong>[{0}]</strong></span>", article.CommentCount));
            }
            else
            {
                sb.Append(string.Format("<span>{0}</span>", Server.HtmlEncode(article.Subject)));
                if (article.CommentCount > 0)
                    sb.Append(string.Format(" <span class='text-success'>[{0}]</span>", article.CommentCount));
            }
        }

        if (article.AttachCount > 0)
        {
            sb.Append(" <small>" + MailUtil.GetAttachFontIcon(article.AttachCount) + "</small>");
        }
        return sb.ToString();
    }

    protected string GetDate(DateTime writtenDate)
    {
        return WebUser.CurrentUser.ToLocalTime(writtenDate).ToString("d");
    }

    protected string GetClickEventFunction(Guid boardId, int articleId)
    {
        return string.Format("ViewArticle('{0}', {1}, '{2}', '{3}', '{4}')", boardId.ToString(), ArticlesGridView.PageIndex, articleId, CategoryList.SelectedValue, WebUtil.EncodeJsString2(KeywordTextBox.Text.Trim()));
    }

    #endregion
}