<?php
/**
 * Load the personalisation of HTML of the main page containing the personalisation iframe.
 *
 * @package Fast-Events/Helper/Template/Personalise
 *
 * @since 2.1.0
 */

$iframe_resizer = <<<IFRAMERESIZER
	<style>
		#fe-personalise-parent-dialog{
			font-family: Verdana, sans-serif;
		}
		#fe-personalise-parent-dialog-cancel {
			border-width:1px;
			color:#ffffff;
			border-color:grey;
			font-weight:bold;
			border-radius: 6px;
			background: #6c757d;
		}
		#fe-personalise-parent-dialog-save {
			border-width:1px;
			color:#ffffff;
			border-color:#337fed;
			font-weight:bold;
			border-radius: 6px;
			background:#0d6efd
		}     
		#fe-personalise-action-right-button {
			font-family: Verdana, sans-serif;
			top: 30%;
			background-color: #dd3333;
			color: #ffff;
			border-style: solid;
			border-width: 0;
			display: none;
		}
		.fe-personalise-action-content {
			right: -1px;
			cursor: pointer;
			-webkit-transform-origin: 100% 100%;
			transform: rotate(-90deg);
			border-top-right-radius: 10px;
			border-top-left-radius: 10px;
		}
		.fe-personalise-action-content {
			position: fixed;
			margin: 0;
			padding: 6px 13px 8px 13px;
			text-decoration: none;
			text-align: center;
			font-size: 100%;
			font-weight: normal;
			z-index: 100000;
		}
	</style>
	<dialog id="fe-personalise-parent-dialog">
		<div id="fe-personalise-parent-dialog-label"></div>
		<div id="fe-personalise-parent-dialog-link" style="margin-top: 5px"></div>
		<div style="margin-top: 15px">
			<button id="fe-personalise-parent-dialog-cancel" type="reset">Cancel</button>
			<button id="fe-personalise-parent-dialog-save" type="submit">Copy link</button>
		</div>
	</dialog>
	<div 
		id="fe-personalise-action-right-button"
		class="fe-personalise-action-content fe-personalise-action-right">
	</div>
	<script>
		const btn = document.getElementById('fe-personalise-action-right-button')
		const dialog = document.getElementById('fe-personalise-parent-dialog');
		const dialogLabel = document.getElementById('fe-personalise-parent-dialog-label');
		const dialogLink = document.getElementById('fe-personalise-parent-dialog-link');
		const dialogCancel = document.getElementById('fe-personalise-parent-dialog-cancel');
		const dialogSave = document.getElementById('fe-personalise-parent-dialog-save');

        dialogCancel.onclick = async function () {
            dialog.close();
        }
        dialogSave.onclick = async function () {
            navigator.clipboard.writeText(dialogLink.innerText).then(() => {
			},(reason) => {
			  alert('No clipboard access, you have to select the link manually (' + reason + ')');
			});
            dialog.close();
        }
        
		let iframeObj;
		iframeResize(
			{
				license: 'GPLv3',
				waitForLoad: true,
				checkOrigin: false,
				onReady: function (iframe) {
					iframeObj = iframe;
				},
				onMessage: async function(messageData) {
				   const json = JSON.parse(messageData.message);
				   switch (json.type) {
					   // Download all tickets.
					   case 1:
					   // Download a single ticket
					   case 2:
					   // Download invoice
					   case 3:
						   window.open(json.url);
						   break;
					   case 4:
						   if (json.count === 0) {
							   btn.style.display = 'none';
						   } else {
							   btn.style.display = 'block';
							   btn.innerText = json.text.replace('{}', json.count); 
						   }
						   break;
                       case 5:
                           btn.style.display = 'none';
                           dialogLabel.innerText = json.label;
                           dialogLink.innerText = json.link;
                           dialogCancel.innerText = json.cancel;
                           dialogSave.innerText = json.copy;
                           dialog.showModal();
                           break
					   default:
						   alert('Unknow message');
				   }
	
				}
			},
			"#fast-events-personalisation-iframe"
		);
        btn.onclick = async function (ev) {
            iframeObj.iFrameResizer.sendMessage('Share');
        }
	</script>
IFRAMERESIZER;
