using System.Text;

namespace ZelWiki.Engine.Implementation.Utility
{
    public static class Differentiator
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="thisRev"></param>
        /// <param name="prevRev"></param>
        /// <returns></returns>
        public static string GetComparisonSummary(string thisRev, string prevRev)
        {
            var summary = new StringBuilder();

            var thisRevLines = thisRev.Split('\n');
            var prevRevLines = prevRev.Split('\n');
            var thisRevLineCount = thisRevLines.Length;
            var prevRevLinesCount = prevRevLines.Length;

            var linesAdded = prevRevLines.Except(thisRevLines).Count();
            var linesDeleted = thisRevLines.Except(prevRevLines).Count();

            if (thisRevLineCount != prevRevLinesCount)
            {
                summary.Append($"{Math.Abs(thisRevLineCount - prevRevLinesCount):N0} 行修改.");
            }

            if (linesAdded > 0)
            {
                if (summary.Length > 0) summary.Append(' ');
                summary.Append($"{linesAdded:N0} 行新增.");
            }

            if (linesDeleted > 0)
            {
                if (summary.Length > 0) summary.Append(' ');
                summary.Append($"{linesDeleted:N0} 行删除.");
            }

            if (summary.Length == 0)
            {
                summary.Append($"未修改.");
            }

            return summary.ToString();
        }
    }
}