-- Note the user of window.localStorage.
-- All items are stored under the prefix 'gallery.'.

def gallery.getFavorites()
	set str to window.localStorage.getItem('gallery.favorites')
	if not str then
		set str to '[]'
	end
	set favorites to JSON.parse(str)
	return favorites
catch e
	log `invalid favorites data stored: ${e}`
	return []
end

def gallery.saveFavorites(favorites)
	window.localStorage.setItem('gallery.favorites', JSON.stringify(favorites))
	send favoritesUpdated(favorites: favorites) to document
end

def gallery.favoriteImage(imgEl, src, partnerId, price)
	set favorites to gallery.getFavorites()
	if not gallery.getFavorites().some( \ o -> o.src is src) then
		log `in if`
		js(favorites, src, price) return favorites.concat({ src, price }) end
		set favorites to it

		-- create a copy of the img and have it move to the new favorites icon in the corner!
		set favoritesMenuButton to #favorites-menu-button-container
		if favoritesMenuButton then
			set contentContainer to the first #content

			set cpy to imgEl.cloneNode()

			js(cpy, contentContainer, imgEl, favoritesMenuButton) requestAnimationFrame(() => {
				let imgRect = imgEl.getBoundingClientRect();
				let menuBtnRect = favoritesMenuButton.getBoundingClientRect();

				let width = imgRect.width;
				let height = imgRect.height;
				let startingLeft = imgRect.left;
				let startingTop = imgRect.top;

				let endingLeft = menuBtnRect.left + (menuBtnRect.width / 2);
				let endingTop = menuBtnRect.top + (menuBtnRect.height / 2);

				let xDistance = endingLeft - startingLeft;
				let yDistance = endingTop - startingTop;
				let distance = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance))
				// console.log('distance:', distance);
				let timeToPerformAnimation = 2 * distance;

				let widthPx = `${width}px`;
				let heightPx = `${height}px`;
				let startingLeftPx = `${startingLeft}px`;
				let startingTopPx = `${startingTop}px`;

				let endingLeftPx = `${endingLeft}px`;
				let endingTopPx = `${endingTop}px`;

				let style = cpy.style;
				style['opacity'] = 0.75;
				style['position'] = 'absolute';
				style['width'] = widthPx;
				style['height'] = heightPx;
				style['left'] = startingLeftPx;
				style['top'] = startingTopPx;
				// console.log('style:', style);


				contentContainer.appendChild(cpy)

				requestAnimationFrame(() => {
					cpy.animate(
						{
							// opacity: [1, 0.1, 0],
							width: [widthPx, '1px', '0px'],
							height: [heightPx, '1px', '0px'],
							left: [startingLeftPx, endingLeftPx, '0px'],
							top: [startingTopPx, endingTopPx, '0px'],
							easing: ['ease-out', 'ease-in'],
						},
						2 * timeToPerformAnimation
					)

					setTimeout(() => {
						cpy.style.display = 'none';
						requestAnimationFrame(() => {
							contentContainer.removeChild(cpy)
						});
					}, 2 * timeToPerformAnimation);
				});
			}) end
		else
			log 'button not found:'
		end

	end
	if #{partnerId} then
		send favorited to #{partnerId}
	end
	gallery.saveFavorites(favorites)
end

def gallery.unfavoriteImage(src, partnerId)
	-- log `gallery.unfavoriteImage(${src}, ${partnerId})`
	set favorites to gallery.getFavorites()
	if favorites.some( \ o -> o.src is src) then
		remove src from favorites
		js(favorites, src) return favorites.filter(path => path.src !== src) end
		set favorites to it
	end
	if #{partnerId} then
		send unfavorited to #{partnerId}
	end
	gallery.saveFavorites(favorites)
end

def gallery.isFavorited(src)
	return gallery.getFavorites().some( \ o -> o.src is src)
end
