import * as storage from "../common/storage.js";

const STORAGE_NAME = "/food-sim/recipe-";
const types = ["starters", "soups", "fish", "meat", "deserts"];

let globalStats = { str: 0, mag: 0, def: 0, hp: 0, mp: 0 };
let typeItem = [];
let hasSelection = [false, false, false, false, false];
let ingredientsList = [];

document.addEventListener("DOMContentLoaded", (event) => {
	updateStats();

	types.forEach(function (type, typeIdx) {
		let savedRecipe = storage.get(STORAGE_NAME + typeIdx);

		const recipes = document.querySelectorAll(
			"div.recipes." + type + " .recipe",
		);

		recipes.forEach(function (item, index) {
			let recipeName = item.querySelector(".title").innerText;
			if (savedRecipe != undefined && recipeName == savedRecipe) {
				hasSelection[typeIdx] = true;
				selectRecipe(item, typeIdx);
			}

			item.addEventListener("click", function () {
				if (!hasSelection[typeIdx]) {
					hasSelection[typeIdx] = true;
					selectRecipe(item, typeIdx);
				} else {
					if (typeItem[typeIdx] != item) {
						unselectRecipe(typeIdx);
						selectRecipe(item, typeIdx);
					} else {
						hasSelection[typeIdx] = false;
						unselectRecipe(typeIdx);
						updateStats();
					}
				}
			});
		});
	});

	document
		.getElementById("hearty-meal")
		.addEventListener("click", function () {
			if (this.checked) {
				let bonusStr = Math.ceil(globalStats.str * 0.25);
				let bonusMag = Math.ceil(globalStats.mag * 0.25);
				let bonusDef = Math.ceil(globalStats.def * 0.25);
				let bonusHP = Math.ceil(globalStats.hp * 0.25);
				let bonusMP = Math.ceil(globalStats.mp * 0.25);

				let stats = {
					str: bonusStr,
					mag: bonusMag,
					def: bonusDef,
					hp: bonusHP,
					mp: bonusMP,
				};

				addStats(stats);
			} else {
				let bonusStr = Math.ceil(globalStats.str * 0.2);
				let bonusMag = Math.ceil(globalStats.mag * 0.2);
				let bonusDef = Math.ceil(globalStats.def * 0.2);
				let bonusHP = Math.ceil(globalStats.hp * 0.2);
				let bonusMP = Math.ceil(globalStats.mp * 0.2);

				let stats = {
					str: bonusStr,
					mag: bonusMag,
					def: bonusDef,
					hp: bonusHP,
					mp: bonusMP,
				};

				removeStats(stats);
				updateStats();
			}
		});
});

function unselectRecipe(index) {
	// Remove all ingredients of this recipe
	let ingredients = typeItem[index].querySelectorAll(".ingredients ul li");
	for (let ingredient of ingredients) {
		ingredientsList = ingredientsList.filter(
			(ing) => ing != ingredient["innerText"],
		);
	}

	storage.remove(STORAGE_NAME + index);

	typeItem[index].style["box-shadow"] = "0px 0px 10px rgba(0, 0, 0, 0.4)";
	let stats = JSON.parse(typeItem[index].dataset["stats"]);
	removeStats(stats);
}

function selectRecipe(item, index) {
	typeItem[index] = item;
	item.style["box-shadow"] = "0px 0px 10px rgba(0, 255, 0, 0.8)";

	// Add all ingredients of this recipe to a global list
	let ingredients = item.querySelectorAll(".ingredients ul li");
	for (let ingredient of ingredients) {
		ingredientsList.push(ingredient["innerText"]);
	}

	let recipeName = item.querySelector(".title").innerText;
	storage.set(STORAGE_NAME + index, recipeName);

	let stats = JSON.parse(item.dataset["stats"]);
	addStats(stats);
}

function addStats(stats) {
	globalStats.str += stats.str;
	globalStats.mag += stats.mag;
	globalStats.def += stats.def;
	globalStats.hp += stats.hp;
	globalStats.mp += stats.mp;
	updateStats();
}

function removeStats(stats) {
	globalStats.str -= stats.str;
	globalStats.mag -= stats.mag;
	globalStats.def -= stats.def;
	globalStats.hp -= stats.hp;
	globalStats.mp -= stats.mp;
}

const STR_MAX = 5;
const MAG_MAX = 5;
const DEF_MAX = 5;
const HP_MAX = 63;
const MP_MAX = 50;

const CENTER_POINT = [150, 120];

const STR_MAX_POINT = [150, 20];
const MAG_MAX_POINT = [50, 100];
const DEF_MAX_POINT = [250, 100];
const HP_MAX_POINT = [210, 210];
const MP_MAX_POINT = [90, 210];

function updateStats() {
	let strProgress = globalStats.str / STR_MAX;
	let magProgress = globalStats.mag / MAG_MAX;
	let defProgress = globalStats.def / DEF_MAX;
	let hpProgress = globalStats.hp / HP_MAX;
	let mpProgress = globalStats.mp / MP_MAX;

	let poly = document.getElementById("stats-mod");
	updatePoint(
		poly,
		0,
		STR_MAX_POINT,
		strProgress,
		globalStats.str,
		"stat-str",
	);
	updatePoint(
		poly,
		4,
		MAG_MAX_POINT,
		magProgress,
		globalStats.mag,
		"stat-mag",
	);
	updatePoint(
		poly,
		1,
		DEF_MAX_POINT,
		defProgress,
		globalStats.def,
		"stat-def",
	);
	updatePoint(poly, 2, HP_MAX_POINT, hpProgress, globalStats.hp, "stat-hp");
	updatePoint(poly, 3, MP_MAX_POINT, mpProgress, globalStats.mp, "stat-mp");

	// let list = document.getElementById("ingredients-list");
	// list.innerHTML = "";
	// for (let ingredient of ingredientsList) {
	// 	let ingredientElem = document.createElement("li");
	// 	ingredientElem.innerText = ingredient;
	// 	list.appendChild(ingredientElem);
	// }
}

function updatePoint(poly, statId, max, progress, value, statElem) {
	poly.points[statId].x =
		CENTER_POINT[0] + (max[0] - CENTER_POINT[0]) * progress;
	poly.points[statId].y =
		CENTER_POINT[1] + (max[1] - CENTER_POINT[1]) * progress;

	document.getElementById(statElem).innerHTML = "+" + value;
}
