/*
 * ZomboidDoc - Lua library compiler for Project Zomboid
 * Copyright (C) 2020-2021 Matthew Cain
 *
 * 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/>.
 */
package io.cocolabs.pz.zdoc.doc.detail;

import org.jsoup.nodes.Element;

abstract class DetailSignature {

	final String signature;

	DetailSignature(String signature) {
		this.signature = normalizeSignature(signature);
	}

	/**
	 * Normalize given signature text:
	 * <ul>
	 * <li>Convert non-breaking space ({@code &nbsp}) to whitespace.</li>
	 * <li>Convert newlines to whitespace.</li>
	 * <li>Remove consecutive whitespaces.</li>
	 * </ul>
	 *
	 * @param text {@code String} to normalize.
	 * @return normalized text.
	 */
	private static String normalizeSignature(String text) {

		StringBuilder sb = new StringBuilder();
		char lastChar = 0;
		for (char c : text.toCharArray())
		{
			// convert &nbsp and newlines to whitespace
			if (c == 160 || c == '\r' || c == '\n') {
				c = ' ';
			}
			// remove consecutive whitespaces
			if (lastChar == ' ' && c == ' ') {
				continue;
			}
			sb.append(c);
			lastChar = c;
		}
		return sb.toString();
	}

	/**
	 * Gets the combined text of the given element and all its children.
	 * Whitespace is normalized and trimmed. The normalization process also
	 * includes converting non-breaking space ({@code &nbsp}) and newlines to whitespace.
	 *
	 * @param element {@code Element} text to normalize.
	 * @return normalized and trimmed text or empty text if none.
	 *
	 * @see Element#text()
	 */
	static String normalizeElement(Element element) {
		return normalizeSignature(element.text());
	}

	@Override
	public String toString() {
		return signature;
	}
}
