var MessageHandler = (function() {
	// Handlers for each message type
	var animation = "slideDown";
	var handlers = {
		announcement: function(messageWrapper) {
			var announcementNotification = JSON.parse(messageWrapper.data);

			var liveScreen = document.getElementById("auctionProductLiveScreenBox");
			if (!liveScreen && !messageWrapper.hideToaster) {
				toastr.info(`<div class="item announcemantToast">${announcementNotification.message}</div>`,
					announcementNotification.title,
					{
						"closeButton": true,
						"positionClass": "toast-bottom-left",
						"newestOnTop": true,
						"progressBar": true,
						"preventDuplicates": false,
						"onclick": null,
						"showDuration": "30000",
						"hideDuration": "1000",
						"timeOut": "5000",
						"extendedTimeOut": "1000",
						"showEasing": "swing",
						"hideEasing": "linear",
						"showMethod": animation,
						"hideMethod": "fadeOut"
					});
				$('.toast-info').css("background-color", "#008080");
			}
		},

		bidNotification: function(messageWrapper) {
			var bidNotification = JSON.parse(messageWrapper.data);

			console.log(bidNotification);

			var auctionDto = bidNotification.auctionDto;

			var productToAuctionList = auctionDto.productToAuctionDtos;

			productToAuctionList.forEach(productToAuction => {

				/*var productToAuctionDto = auctionDto.productToAuctionDtos[0];*/

				//var productAuctionMappingId = productToAuctionDto.id;
				//console.log(productAuctionMappingId);

				var publicProductBidBox = document.getElementById("publicProductBidBox" + productToAuction.id);
				var liveScreen = document.getElementById("auctionProductLiveScreenBox");
				var publicInfo = document.getElementById("publicInfoOverlay" + productToAuction.productId);

				if (publicProductBidBox) {
					//var audio = new Audio('../Plugins/Misc.AuctionPlugin/Content/ding.mp3');
					//audio.play();

					var lastRequestId = window.getRequestId();
					var isMyRequest = messageWrapper.requestId == lastRequestId;

					console.log("isMyRequest: " + isMyRequest + "; lastRequestId: " + lastRequestId + "; messageWrapper.RequestId: " + messageWrapper.requestId);

					refreshPublicBidBox(bidNotification, isMyRequest, productToAuction.id, productToAuction);
				}
				if (publicInfo) {
					var functionName = "refreshPublicInfo" + productToAuction.productId;
					window[functionName](auctionDto);
				}
				if (liveScreen) {

					updateOnBid(bidNotification);
				} else if (!messageWrapper.hideToaster) {
					toastr.success(
						`<div class="item bidToast"><p>${bidNotification.currentPrice}</p><p>${bidNotification.productName
						}</p></div>`,
						"New bid arrived",
						{
							"closeButton": true,
							"positionClass": "toast-bottom-left",
							"newestOnTop": true,
							"progressBar": true,
							"preventDuplicates": false,
							"onclick": null,
							"showDuration": "30000",
							"hideDuration": "1000",
							"timeOut": "5000",
							"extendedTimeOut": "1000",
							"showEasing": "swing",
							"hideEasing": "linear",
							"showMethod": animation,
							"hideMethod": "fadeOut"
						});
					$('.toast-success').css("background-color", "#4caf50");
				}


			});
			
		},

		ProductToAuctionStatusNotification: function(messageWrapper) {
			var auctionStatusNotification = JSON.parse(messageWrapper.data);

			var auctionDto = auctionStatusNotification.auctionDto;
			var productToAuctionDto = auctionDto.productToAuctionDtos[0];
			//get the list of pta? should be 1
			console.log("PtaId: "+productToAuctionDto.id);

			var publicProductBidBox = document.getElementById("publicProductBidBox" + productToAuctionDto.id);
			var liveScreen = document.getElementById("auctionProductLiveScreenBox");
			var publicInfo = document.getElementById("publicInfoOverlay" + productToAuctionDto.productId);

			if (!liveScreen && !messageWrapper.hideToaster) {
				var messageTitle = "";
				var messageText = "";
				var messageColor = "";

				switch (productToAuctionDto.auctionStatus) {
				case AuctionStatus.None:
					messageTitle = `Item reset`;
					messageText = `The bids on item with index ${productToAuctionDto.sortIndex} has been resetted`;
					messageColor = "#6c757d";
					break;
				case AuctionStatus.Active:
					messageTitle = `Item activated`;
					messageText = `The bids on item with index ${productToAuctionDto.sortIndex} has been activated`;
					messageColor = "#4caf50";
					break;
				case AuctionStatus.FirstWarning:
					messageTitle = `First warning!`;
					messageText = `Hurry up! If no more bids, this item will be closed soon!`;
					messageColor = "#ffc107";
					break;
				case AuctionStatus.SecondWarning:
					messageTitle = `Second warning!`;
					messageText = `Hurry up! If no more bids, this item will be closed soon!`;
					messageColor = "#dc3545";
					break;
				case AuctionStatus.Pause:
					messageTitle = `Administrative message`;
					messageText = `The administrator has suspended the auction, it will go on soon probably`;
					messageColor = "#6c757d";
					break;
				case AuctionStatus.Sold:
					messageTitle = `Item sold!`;
					messageText = `The item has been sold, we are transitioning to the next item!`;
					messageColor = "#4caf50";
					break;
				case AuctionStatus.NotSold:
					messageTitle = `Item closed!`;
					messageText = `The item has been closed, we are transitioning to the next item!`;
					messageColor = "#6c757d";
					break;
				default:
					break;
				}

				toastr.success(`<div class="item bidToast"><p>${messageText}</p></div>`,
					messageTitle,
					{
						"closeButton": true,
						"positionClass": "toast-bottom-left",
						"newestOnTop": true,
						"progressBar": true,
						"preventDuplicates": false,
						"onclick": null,
						"showDuration": "30000",
						"hideDuration": "1000",
						"timeOut": "5000",
						"extendedTimeOut": "1000",
						"showEasing": "swing",
						"hideEasing": "linear",
						"showMethod": animation,
						"hideMethod": "fadeOut"
					});
				$('.toast-success').css("background-color", messageColor);
			}

			if (publicProductBidBox) {
				handleAuctionUpdate(auctionStatusNotification, productToAuctionDto.id);
			}

			if (liveScreen) {
				reloadOnUpdate();
			}

			if (publicInfo) {
				var functionName = "refreshPublicInfo" + productToAuctionDto.productId;
				window[functionName](auctionDto);
			}

			// var publicProductBidBox = document.getElementById("publicProductBidBox");
			// if (publicProductBidBox) 
			// {
			//  refreshPublicBidBox(myObject);
			// }
		},

		//openItemMessage: function(messageWrapper) {
		//	var openItemNotification = JSON.parse(messageWrapper.data);

		//	if (!messageWrapper.hideToaster) {
		//		toastr.success(
		//			`<div class="item bidToast"><p>${openItemNotification.nextBidPrice}</p><p>${openItemNotification
		//			.productName}</p></div>`,
		//			"Item auction is OPENED!",
		//			{
		//				"closeButton": true,
		//				"positionClass": "toast-top-right",
		//				"newestOnTop": true,
		//				"progressBar": true,
		//				"preventDuplicates": false,
		//				"onclick": null,
		//				"showDuration": "30000",
		//				"hideDuration": "1000",
		//				"timeOut": "5000",
		//				"extendedTimeOut": "1000",
		//				"showEasing": "swing",
		//				"hideEasing": "linear",
		//				"showMethod": animation,
		//				"hideMethod": "fadeOut"
		//			});
		//		$('.toast-success').css("background-color", "#4caf50");
		//	}

		//	var publicProductBidBox = document.getElementById("publicProductBidBox");
		//	if (publicProductBidBox) {
		//		refreshPublicBidBox(openItemNotification);
		//	}
		//},

		// Add more handlers as needed
		default: function(messageWrapperJson) {
			console.warn("Unhandled message type:", messageWrapperJson);
		}
	};

	// Message router to route to the appropriate handler based on message type
	function messageRouter(messageWrapperJson) {
		// Parse the JSON message
		try {
			console.log(messageWrapperJson);

			var messageWrapper = JSON.parse(messageWrapperJson);
			var messageType = messageWrapper.messageType;
			//var senderId = messageWrapper.senderId;

			//console.log("Message type:" + messageType);
			//console.log("Message sender:" + senderId);
			//console.log("Message hideToaster:" + messageWrapper.hideToaster);
			//console.log("Message content" + messageWrapper.data);

			// Route to appropriate handler, default if no match
			(handlers[messageType] || handlers.default)(messageWrapper);
		} catch (e) {
			console.error("Error parsing message:", e);
		}
	}

	return {
		handle: messageRouter
	};
})();
