<?php
namespace SPAATG\Model\AdminNotices;

if ( ! defined( 'ABSPATH' ) ) {
 exit; // Exit if accessed directly.
}

use SPAATG\Controller\StatsController as StatsController;
use SPAATG\Controller\ApiKeyController as ApiKeyController;
use SPAATG\Controller\AdminNoticesController as AdminNoticesController;
use SPAATG\Controller\QuotaController as QuotaController;


class QuotaNoticeReached extends \SPAATG\Model\AdminNoticeModel
{
	protected $key = 'MSG_QUOTA_REACHED';
	protected $errorLevel = 'error';


	public function load()
	{
     parent::load();
	}

	protected function checkTrigger()
	{
			$quotaController = QuotaController::getInstance();

			if ($quotaController->hasQuota() === true)
				return false;

		  $this->reset('MSG_UPGRADE_MONTH');
			$this->reset('MSG_UPGRADE_BULK');
	    return true;

	}

	protected function getMessage()
	{
		$statsControl = StatsController::getInstance();
		$averageCompression = $statsControl->getAverageCompression();
		$quotaController = QuotaController::getInstance();

		$keyControl = ApiKeyController::getInstance();

		//$keyModel->loadKey();

		$login_url = 'https://shortpixel.com/login/';
		$friend_url = $login_url;

		if ($keyControl->getKeyForDisplay())
		{
			$login_url .= $keyControl->getKeyForDisplay() . '/spaatg-unlimited';
			$friend_url = $login_url . 'tell-a-friend';
		}

	 $message = '<div class="sp-quota-exceeded-alert"  id="spaatg-notice-exceed">';

	 if($averageCompression) {
		    wp_add_inline_script(
		        'spaatg',
		        'jQuery(function(){if(window.SPAATG){SPAATG.percentDial("#sp-avg-optimization-dial",60);}});'
		    );

				$message .= '<div style="float:right;">
						<div class="bulk-progress-indicator" style="height: 110px">
								<div style="margin-bottom:5px">' . __('Average image<br>reduction until now:','shortpixel-ai-alt-text-generator') . '</div>
								<div id="sp-avg-optimization"><input type="text" id="sp-avg-optimization-dial" value="' . round($averageCompression) . '" class="dial percentDial" data-dialsize="60"></div>
						</div>
				</div>';

		}

			$message .= '<h3>' . __('Quota Exceeded','shortpixel-ai-alt-text-generator') . '</h3>';

			$quota = $quotaController->getQuota();

			$creditsUsed = number_format($quota->monthly->consumed + $quota->onetime->consumed);
			$totalOptimized = $statsControl->find('total', 'images');
			$totalImagesToOptimize = number_format($statsControl->totalImagesToOptimize());

			/* translators: %s: optimized image count. */
			$message .= '<p>' . sprintf(__('The plugin has optimized <strong>%s images</strong> and has been stopped because it has reached the available quota limit.','shortpixel-ai-alt-text-generator'),
						$creditsUsed);

			if($totalImagesToOptimize > 0) {

						/* translators: %s: unoptimized image and thumbnail count. */
						$message .= sprintf(__('<strong> %s images and thumbnails</strong> have not been optimized by SPAATG yet.','shortpixel-ai-alt-text-generator'), $totalImagesToOptimize  );
				}

			 $message .= sprintf('</p>
					<div>
						<a class="button button-primary" href="%s"
							 title="' . __('Go to My Account and choose a plan','shortpixel-ai-alt-text-generator') . '" target="_blank" style="margin-right:10px;">
								<strong>' . __('Upgrade','shortpixel-ai-alt-text-generator') . '</strong>
						</a>
				</div>', esc_url($login_url));

			$message .= '</div>'; /// closing div
			return $message;
	}

}
