<?php namespace MeowCrew\CancellationOffers\Offer\CPT\Form\Metaboxes;

use MeowCrew\CancellationOffers\Offer\CPT\CancellationOffersCPT;
use MeowCrew\CancellationOffers\Offer\Entity\Offer;

class RetentionRateMetabox {
	
	const SLUG = 'cos-retention-rate';
	
	/**
	 * Offer
	 *
	 * @var Offer
	 */
	protected $offer;
	
	public function __construct( Offer $offer ) {
		
		$this->offer = $offer;
		
		add_meta_box( self::SLUG, __( 'Retention Rate', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
			array( $this, 'render' ), CancellationOffersCPT::SLUG, 'side' );
	}
	
	public function render() {
		
		$retentionRateAllCases  = $this->offer->getStatistics()->getRetentionRate();
		$discountsRetentionRate = $this->offer->getStatistics()->getRetentionRate( true );
		
		if ( ! $retentionRateAllCases || ! $this->offer->getDiscountOffer()->isEnabled() ) {
			
			?>
			<style>
				#cos-retention-rate {
					display: none;
				}
			</style>
			<?php
			return;
		}
		?>

		<div style="display: flex; justify-content: space-around">
			<div style="width: 40%">

				<div class="cancellation-offer-popup-survey__retention-rate"
					 data-retention-rate="<?php echo esc_attr( $retentionRateAllCases ); ?>"
					 style="--retention-rate:<?php echo esc_attr( $retentionRateAllCases ); ?>%;">
					<progress value="<?php echo esc_attr( $retentionRateAllCases ); ?>" max="100"
							  style="visibility:hidden;height:0;width:0;">
						<?php echo esc_html( $retentionRateAllCases ); ?>%
					</progress>
				</div>

				<div style="text-align: center">
					<?php esc_html_e( 'All Cases', 'cancellation-surveys-offers-woocommerce-subscriptions' ); ?>
				</div>
			</div>

			<div style="width: 40%">

				<div class="cancellation-offer-popup-survey__retention-rate"
					 data-retention-rate="<?php echo esc_attr( $discountsRetentionRate ); ?>"
					 style="--retention-rate:<?php echo esc_attr( $discountsRetentionRate ); ?>%;">
					<progress value="<?php echo esc_attr( $discountsRetentionRate ); ?>" max="100"
							  style="visibility:hidden;height:0;width:0;">
						<?php echo esc_html( $discountsRetentionRate ); ?>%
					</progress>
				</div>

				<div style="text-align: center">
					<?php
						esc_html_e( 'When discount was offered',
							'cancellation-surveys-offers-woocommerce-subscriptions' );
					?>
				</div>
			</div>
		</div>
		<br>
		<p style="text-align: center">
			<?php
				esc_html_e( 'The rate represents the percentage of successful subscription retentions .',
					'cancellation-surveys-offers-woocommerce-subscriptions' );
			?>
		</p>
		<?php
	}
}