using Microsoft.Xna.Framework.Graphics;
using System;


namespace ActionGame.UserInterface.Helper
{
    public class TextHelper
    {

        public static String GetCroppedText(String raw, SpriteFont font, Int32 maxWidth)
        {
            if ((raw == null) || (raw.Equals(""))) return "";

            String result = raw;
            Int32 rawWidth = (Int32)font.MeasureString(raw).X;

            if (rawWidth > maxWidth)
            {
                Int32 avgCharacterWidth = rawWidth / raw.Length;
                Int32 charactersPerLine = maxWidth / avgCharacterWidth;

                string[] words = raw.Split(' ');

                result = "";
                Int32 lineLength = 0;
                for (Int32 i = 0; i < words.Length; i++)
                {
                    if ((lineLength + words[i].Length) > charactersPerLine)
                    {
                        result += "\n";
                        lineLength = 0;
                    }

                    result += words[i] + " ";
                    lineLength += words[i].Length + 1;
                }
            }

            return result;
        }


    }
}
