<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template files receive local context variables from their controller.
if ( ! defined( 'ABSPATH' ) ) {
 exit; // Exit if accessed directly.
}

use SPAATG\Controller\ApiKeyController;
use SPAATG\Controller\QuotaController;
use SPAATG\Model\AiDataModel;

$icon_url = plugins_url( '/res/images/icon/', SPAATG_PLUGIN_FILE );
$robo_icon = $icon_url . '/shortpixel.svg';
$ai_icon = $icon_url . '/ai.svg';

$item_id = absint($this->data['item_id']);
$has_data = $this->data['has_data'];
//$isSupported = $this->data['isSupported'];
$isDifferent = $this->data['isDifferent'];
$dataItems = implode(', ',$this->data['dataItems']);

$is_processable = $this->data['is_processable'];
$processable_reason = $this->data['processable_reason'];
$processable_status = $this->data['processable_status'];
$is_in_queue = ! empty($this->data['is_in_queue']);

$quotaControl = QuotaController::getInstance();
$keyControl = ApiKeyController::getInstance();
$api_key = $keyControl->forceGetApiKey();
$pricing_url = 'https://shortpixel.com/' . ($api_key ? 'login/' . rawurlencode($api_key) . '/pricing' : 'pricing');
$disabled_title = __('This image is already waiting in queue.', 'shortpixel-ai-alt-text-generator');
$disabled_class = $is_in_queue ? ' disabled' : '';
$disabled_attrs = $is_in_queue ? ' aria-disabled="true" tabindex="-1" data-spaatg-disabled="queue"' : '';
$generate_href = $is_in_queue ? 'javascript:void(0)' : 'javascript:window.SPAATGProcessor.screen.RequestAlt(' . $item_id . ')';
$revert_href = $is_in_queue ? 'javascript:void(0)' : 'javascript:window.SPAATGProcessor.screen.UndoAlt(' . $item_id . ', \'undo\')';
$regenerate_href = $is_in_queue ? 'javascript:void(0)' : 'javascript:window.SPAATGProcessor.screen.UndoAlt(' . $item_id . ', \'redo\')';


if (true === apply_filters('spaatg/settings/no_ai', false)):

	?>
	<p class='hidden' id='shortpixel-noai'></p>
<?php
endif;

if (false === $is_processable && $processable_status !== AiDataModel::P_ALREADYDONE):

	if (true === in_array($processable_status, [AiDataModel::P_NOJOB])) // Silent fail if all done
	{

	}
	else
	{
			printf('<p>%s</p>', wp_kses_post($processable_reason));
	}
	

elseif (false === $has_data): 

	if (false === $keyControl->keyIsVerified() && false === $has_data):
	
			echo wp_kses(
				__('Invalid API Key. <a href="options-general.php?page=spaatg-settings">Check your Settings</a>','shortpixel-ai-alt-text-generator'),
				array(
					'a' => array(
						'href' => array(),
					),
				)
			);

	elseif (false === $quotaControl->hasQuota() && false === $has_data):
	?>
			<p>
				<?php echo wp_kses(
					sprintf(
						/* translators: 1: opening pricing link, 2: closing pricing link. */
						__('ShortPixel quota exhausted, the AI SEO data cannot be generated. %1$sSee pricing and upgrade options%2$s.', 'shortpixel-ai-alt-text-generator'),
						'<a href="' . esc_url($pricing_url) . '" target="_blank" rel="noopener noreferrer">',
						'</a>'
					),
					array(
						'a' => array(
						'href' => array(),
						'target' => array(),
						'rel' => array(),
					),
				)
			); ?></p>

	<?php else: ?>

		    <a class='button button-secondary shortpixel-ai-action-button shortpixel-ai-generate-button<?php echo esc_attr($disabled_class); ?>' title="<?php echo esc_attr($is_in_queue ? $disabled_title : __('Generate image SEO data with ShortPixel AI', 'shortpixel-ai-alt-text-generator')); ?>" href="<?php echo esc_attr($generate_href); ?>" data-spaatg-ai-action-id="<?php echo esc_attr($item_id); ?>"<?php echo $disabled_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static disabled attributes. ?>>
				<img class="shortpixel-ai-icon shortpixel" src="<?php echo esc_url($robo_icon); ?>">
				<span><?php esc_html_e('Generate AI SEO', 'shortpixel-ai-alt-text-generator'); ?></span>
			</a>
	        <div class="shortpixel-alt-messagebox" id="shortpixel-ai-messagebox-<?php echo esc_attr($item_id); ?>">&nbsp;</div>
	<?php endif; ?>   
<?php else : 
	
	$lb = '%0D%0A';
	$mailtolink = "mailto:support@shortpixel.com?Subject=Wrong AI SEO data generated by ShortPixel AI&Body=ImageURL:  " . $this->data['image_url'] . "$lb $lb Caption:  " . $this->data['result_alt'];
?>

				<p><img class="shortpixel-ai-caption shortpixel-ai-icon shortpixel" src="<?php echo esc_url($robo_icon); ?>">
				<?php echo wp_kses(
					sprintf(
						/* translators: %s: generated SEO data fields. */
						__('The following image SEO data was generated by ShortPixel AI: %s', 'shortpixel-ai-alt-text-generator'),
						'<strong>' . esc_html($dataItems) . '</strong>'
					),
					array(
						'strong' => array(),
				)
			); ?></p>
	
			<a class='button button-secondary shortpixel-ai-action-button<?php echo esc_attr($disabled_class); ?>' title="<?php echo esc_attr($is_in_queue ? $disabled_title : __('Restores the previous image SEO data, if available.', 'shortpixel-ai-alt-text-generator')); ?>" href="<?php echo esc_attr($revert_href); ?>" data-spaatg-ai-action-id="<?php echo esc_attr($item_id); ?>"<?php echo $disabled_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static disabled attributes. ?>><?php esc_html_e('Revert AI SEO', 'shortpixel-ai-alt-text-generator'); ?></a>

		<a class='button button-secondary shortpixel-ai-action-button shortpixel-ai-regenerate-button<?php echo esc_attr($disabled_class); ?>' title="<?php echo esc_attr($is_in_queue ? $disabled_title : __('Regenerate image SEO data with ShortPixel AI.', 'shortpixel-ai-alt-text-generator')); ?>" href="<?php echo esc_attr($regenerate_href); ?>" data-spaatg-ai-action-id="<?php echo esc_attr($item_id); ?>"<?php echo $disabled_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static disabled attributes. ?>><?php esc_html_e('Regenerate SEO', 'shortpixel-ai-alt-text-generator'); ?></a>

	<?php if (true === $isDifferent): ?>

		<a class='button button-secondary shortpixel-ai-action-button<?php echo esc_attr($disabled_class); ?>' title="<?php echo esc_attr($is_in_queue ? $disabled_title : __('Redo', 'shortpixel-ai-alt-text-generator')); ?>" href="<?php echo esc_attr($regenerate_href); ?>" data-spaatg-ai-action-id="<?php echo esc_attr($item_id); ?>"<?php echo $disabled_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static disabled attributes. ?>><?php esc_html_e('Redo', 'shortpixel-ai-alt-text-generator'); ?></a>
	<?php endif; ?>

		<a class='button button-secondary shortpixel-ai-action-button<?php echo esc_attr($disabled_class); ?>' title="<?php echo esc_attr($is_in_queue ? $disabled_title : __('Report to us, via e-mail, an incorrect image SEO data for this image', 'shortpixel-ai-alt-text-generator') . '.'); ?>" href="<?php echo $is_in_queue ? esc_attr('javascript:void(0)') : esc_url($mailtolink, array('mailto')); ?>" data-spaatg-ai-action-id="<?php echo esc_attr($item_id); ?>"<?php echo $disabled_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static disabled attributes. ?>><?php esc_html_e('Report', 'shortpixel-ai-alt-text-generator'); ?></a>

		<a class='button button-secondary shortpixel-ai-action-button' title="<?php esc_attr_e('Send us feedback about this feature, suggest other features, or vote on existing feature requests', 'shortpixel-ai-alt-text-generator'); ?> ." target="_blank" href="https://ideas.shortpixel.com/"><?php esc_html_e('Feedback', 'shortpixel-ai-alt-text-generator'); ?></a>

		<?php if (true === $isDifferent)
		{
					 echo wp_kses(
						 sprintf(
							 /* translators: 1: opening paragraph tag, 2: closing paragraph tag. */
							 __('%1$s The plugin has detected some fields were changed after generating AI Data. You can use the redo button to regenerate AI Data %2$s ', 'shortpixel-ai-alt-text-generator'),
							 '<p>',
							 '</p>'
						 ),
						 array(
							 'p' => array(),
					 )
				 );
	} ?>

<?php endif; ?>
