// Personal Webpage Engine
// Copyright(c) 2008 Mihail Kochanov
// You can contact me at StilgarAD@yahoo.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
//
// Personal Web Page Engine
//
//------------------------------------------------------------------------------

using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Text;
using System.Web;

/// <summary>
/// Utilities
/// </summary>
public static class Utilities
{
    private static int timeDifference = Convert.ToInt32(ConfigurationManager.AppSettings["TimeDifference"]);

    /// <summary>
    /// Adjust the time according to the time difference and formats it
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static string ToTimeAdjustedString(this DateTime date)
    {
        return date.AddHours(timeDifference).ToString(Constants.Settings.DateTimeFormat);
    }

    /// <summary>
    /// Replaces bracket tags with HTML tags
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public static string AddHtmlTags(this string arg)
    {
        //Singleline mode needed to make the dot match new line. This way tags that are include new line will be matched.
        Regex regex = new Regex(Constants.Regex.CaptureBracketTags, RegexOptions.IgnoreCase | RegexOptions.Singleline);

        //the first backreference contains the whole opening tag with attributes
        //the second contains only the name of the tag
        //the third contains the value of the tag
        return regex.Replace(arg, "<$1>$3</$2>");
    }

    /// <summary>
    /// Replaces HTML tags with bracket tags
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public static string RemoveHtmlTags(this string arg)
    {
        //Singleline mode needed to make the dot match new line. This way tags that are include new line will be matched.
        Regex regex = new Regex(Constants.Regex.CaptureHtmlTags, RegexOptions.IgnoreCase | RegexOptions.Singleline);

        //the first backreference contains the whole opening tag with attributes
        //the second contains only the name of the tag
        //the third contains the value of the tag
        return regex.Replace(arg, "[$1]$3[/$2]");
    }

    /// <summary>
    /// Replaces the URLs in the input with HTML links
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string ReplaceUrlsWithLinks(this string input)
    {
        return Regex.Replace(input,
            @"\b(((http(s)?|ftp|file)://)|www\.)[^\s]+",
            match =>
            {
                string url = match.Value.StartsWith("www.", StringComparison.InvariantCultureIgnoreCase) ? "http://" + match.Value : match.Value;
                return String.Format("<a target='_blank' href='{0}'>{1}</a>", url, match.Value);                    
            },
            RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
    }

    public static string ReplaceLinksWithUrls(this string input)
    {
        return Regex.Replace(input, @"<a\b[^>]*>(?<URL>.*?)</a>", "${URL}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
    }

    public static string HtmlEncode(this string input)
    {
        return HttpUtility.HtmlEncode(input);
    }

    public static string HtmlDecode(this string input)
    {
        return HttpUtility.HtmlDecode(input);
    }

    public static string GenerateCommentUrl(Comment comment, int pageNumber)
    {
        return "~/ViewNewsItem.aspx?" +
            //add the NewsItemID to the URL
            Constants.QueryString.NewsItemID + "=" + comment.NewsItem.NewsItemID +
            //add the page to the URL if it is not the first page
            (pageNumber == 1 ? "" : ("&" + Constants.QueryString.Page + "=" + pageNumber)) +
            //add the anchor to the target comment to the URL
            "#" + Constants.Anchors.Comment + comment.CommentID;
    }
}

