/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *
 */

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Utils.Helpers {
	
	/// <summary>
	/// Helper methods for strings.
	/// </summary>
	public abstract class StringHelper {

		/// <summary>
		/// Splits a given text pagewise for a text generator.
		/// </summary>
		/// <returns>The text split into pages.</returns>
		/// <param name="text">The text to split.</param>
		/// <param name="settings">The text generator settings.</param>
		public static List<string> SplitPagewise(string text, Text uiText) {
			List<string> pages = new List<string>();
			TextGenerationSettings settings = uiText.GetGenerationSettings(uiText.rectTransform.rect.size);
			TextGenerator generator = uiText.cachedTextGenerator;

			// While there is text
			while (text.Length > 0) {
				// Populate generator to get size
				generator.Populate (text, settings);

				// Get displayable string
				string display = text.Substring(0, generator.characterCountVisible);
				if (string.IsNullOrEmpty(display)) break; // Prevent infinite loop

				// Make word wrap correct because unity cannot word wrap!
				if (display.Length > 0 && text.Length > display.Length) {
					if (display[display.Length - 1] != ' ' && text[display.Length] != ' ') {
						string[] tempDisplayArray = display.Split(' ');
						display = display.TrimEnd(tempDisplayArray[tempDisplayArray.Length - 1].ToCharArray());
					}
				}

				pages.Add(display.Trim());				// Add display string to list of pages
				text = text.Remove(0, display.Length);	// Remove handled text segment / page
				generator.Invalidate();					// Invalidate generator for next text segment
			}

			return pages;
		}
	}
}
