<?php namespace MeowCrew\CancellationOffers\Offer\CPT\Columns;

use MeowCrew\CancellationOffers\Core\ServiceContainerTrait;
use MeowCrew\CancellationOffers\Offer\Entity\Offer;

class OfferColumn {

	use ServiceContainerTrait;

	public function getName(): string {
		return __( 'Discount Offer', 'cancellation-surveys-offers-woocommerce-subscriptions' );
	}

	public function render( Offer $offer ) {


		if ( ! csows_fs()->can_use_premium_code() ) {
			printf( __( 'Available in %s', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
					'<strong><a href="' . esc_url( csows_upgrade_url() ) . '" target="_blank">' . __( 'Premium Version',
							'cancellation-surveys-offers-woocommerce-subscriptions' ) . '</a></strong>' );

			return;
		}

		$discountOffers = $offer->getDiscountOffers();

		if ( empty( $discountOffers ) ) {
			esc_html_e( 'No offers configured.', 'cancellation-surveys-offers-woocommerce-subscriptions' );

			return;
		}

		foreach ( $discountOffers as $index => $discountOffer ) {

			if ( $index > 0 ) {
				echo '<hr style="margin: 10px 0; border: 0; border-top: 1px solid #ddd;">';
			}

			if ( ! $discountOffer->isEnabled() ) {
				echo '<div style="opacity: 0.6;">';
				echo '<strong>' . esc_html( $discountOffer->getTitle() ?: '' ) . '</strong>';
				echo '<br>';
				echo '<strong>' . esc_html_e( 'Offer is disabled',
								'cancellation-surveys-offers-woocommerce-subscriptions' ) . '</strong>';
				echo '</div>';
				continue;
			}

			// translators: %d is the offer number (starting from 1)
			echo '<div style="margin-bottom: 5px;"><strong>' . esc_html( $discountOffer->getTitle() ?: sprintf( __( 'Offer #%d',
							'cancellation-surveys-offers-woocommerce-subscriptions' ),
							$index + 1 ) ) . '</strong></div>';

			if ( $discountOffer->getOfferType() == 'skip_renewal' ) {
				?>
				<code style="
				display: block;
				text-align: center;
				border-radius: 6px;
				margin-bottom: 5px;">
					<?php esc_html_e( 'Skip renewal', 'cancellation-surveys-offers-woocommerce-subscriptions' ); ?>
				</code>
				<?php
			} else {
				$coupon = $discountOffer->getCoupon();
				if ( $coupon ) {
					?>
					<a href="<?php echo esc_html( get_edit_post_link( $coupon->get_id() ) ); ?>"
					   style="text-decoration: none;">
						<code style="display: block;
								text-align: center;
								border-radius: 6px;
								margin-bottom: 5px;">
							<b><?php echo esc_html( $coupon->get_code() ); ?></b>
						</code>
					</a>
					<?php
				} else {
					echo '<div style="color: #a00; font-style: italic; font-size: 0.9em; margin-bottom: 5px;">';
					esc_html_e( 'Coupon missing', 'cancellation-surveys-offers-woocommerce-subscriptions' );
					echo '</div>';
				}
			}

			// Limitations
			$limitations = [];
			if ( $discountOffer->getMinimumRenewalsCount() ) {
				// translators: %d minimum number of renewals required for the offer to be applicable
				$limitations[] = sprintf( __( 'Min renewals: %d',
						'cancellation-surveys-offers-woocommerce-subscriptions' ),
						$discountOffer->getMinimumRenewalsCount() );
			}
			if ( $discountOffer->getMinimumSubscriptionDaysActive() ) {
				// translators: %d minimum number of days the subscription must be active for the offer to be applicable
				$limitations[] = sprintf( __( 'Min days: %d', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
						$discountOffer->getMinimumSubscriptionDaysActive() );
			}

			if ( ! empty( $limitations ) ) {
				echo '<div style="font-size: 0.85em; color: #666;">';
				echo implode( '<br>', array_map( 'esc_html', $limitations ) );
				echo '</div>';
			}
		}
	}
}