function num(s) {
	return parseInt(s.replace(".", ""));
}

function str(n) {
	if (n == 0) {
		return "0";
	}
	s = "";
	while (n > 0) {
		if (s != "") {
			s = "." + s;
		}
		r = n % 1000;
		s = r.toString().padStart(3, "0") + s;
		n = ~~(n/1000);		
	}
	return s.replace(/^0+/, "");
}

function createRow(key, value) {
	keyCell = document.createElement("td");
  keyCell.textContent = key;
  valueCell = document.createElement("td");
  valueCell.textContent = value;
  row = document.createElement("tr");
  row.appendChild(keyCell);
  row.appendChild(valueCell);
	return row;
}

function shortcutElementReplace(elt, img, text) {
	imgElements = elt.getElementsByTagName("img")
	for (i=0; i<imgElements.length; i++) {
		if (i == 0) {
			elt.getElementsByTagName("a")[0].classList.add("has_notif");
		}
		imgElements[i].remove();
		i --;
	}
	
	spanImageElement = document.createElement("span");
	spanImageElement.classList.add("shortcut_element_image");

	imgElement = document.createElement("img");
	imgElement.setAttribute("src", chrome.runtime.getURL("images/"+img+".svg"));
	spanImageElement.appendChild(imgElement);

	hyperlinkElement = elt.getElementsByTagName("a")[0];
	hyperlinkElement.textContent = "";
	hyperlinkElement.appendChild(spanImageElement);

	if (text) {
		spanDescElement = document.createElement("span");
		spanDescElement.classList.add("shortcut_element_desc");
		spanDescElement.textContent = text;
		hyperlinkElement.appendChild(spanDescElement);
	}
}

/* Improve main menu display */
layCastleTopElements = document.getElementsByClassName("lay_castle_top");
if (layCastleTopElements.length == 1) {
	document.getElementById("banner_skyscraper").remove();

	shortcutElements = layCastleTopElements[0].getElementsByClassName("shortcut_element");
	shortcutElementReplace(shortcutElements[0], "ranking", shortcutElements[0].textContent.replace(/[^0-9]/g, ""));
	shortcutElementReplace(shortcutElements[1], "ally", "");
	shortcutElementReplace(shortcutElements[2], "profile", "");
	shortcutElementReplace(shortcutElements[3], "premium", "");
	shortcutElementReplace(shortcutElements[4], "messages", "");
	shortcutElementReplace(shortcutElements[5], "tools", "");
	shortcutElementReplace(shortcutElements[6], "favorites", "");
	
	shortcutContainers = layCastleTopElements[0].getElementsByClassName("shortcut_container");
	shortcutContainers[0].classList.add("shortcut_container_left");
	shortcutContainers[2].classList.add("shortcut_container_right"); 
}

/* Display unit-points on user profile */
if (document.body.id === "info_player") {
	mainContentPane = document.getElementsByClassName("contentpane")[1];
	borderLists = mainContentPane.getElementsByClassName("borderlist");
	
	playerPropertiesTable = borderLists[0];
	playerPropertiesRows = playerPropertiesTable.getElementsByTagName("tr");
	totalPoints = num(playerPropertiesRows[2].getElementsByTagName("td")[1].textContent);
	villagesCount = num(playerPropertiesRows[4].getElementsByTagName("td")[1].textContent);

	villagesTable = borderLists[1];
	villagesRows = villagesTable.getElementsByTagName("tr");
	villagesPoints = 0;
	for (i=1; i<villagesRows.length; i+=1) {
		villagesPoints += num(villagesRows[i].getElementsByTagName("td")[2].textContent);
	}
	villagesBonus = 2250 * Math.max(0, villagesCount - 1);
	
	armyPoints = totalPoints - villagesPoints - villagesBonus;
	if (villagesCount > 0) {
		armyPercent = (Math.round(armyPoints / villagesCount) / 100).toString() + " %";
	} else {
		armyPercent = "-";
	}

	playerPropertiesTable.appendChild(createRow("Points troupes:", str(armyPoints)));
	playerPropertiesTable.appendChild(createRow("% points troupes:", armyPercent));
}

/* Display unit-points on village overview */
if (document.body.id === "overview") {
	settlements = document.getElementById("settlement").textContent.split("|");
	noborderRows = document.getElementsByClassName("noborder");
	for (i=0; i<noborderRows.length; i++) {
		if (noborderRows[i].textContent.startsWith("Points:")) {
			villagePointsRow = noborderRows[i];
			break;
		}
	}
	unitPointsRow = villagePointsRow.cloneNode(true);
	villagePointsRow.getElementsByTagName("td")[1].textContent = "Points village: " + settlements[3];
	unitPointsRow.getElementsByTagName("td")[1].textContent = "Points troupes: " + settlements[4];
	villagePointsRow.after(unitPointsRow);
}


