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

use MeowCrew\CancellationOffers\Offer\CPT\Form\FormTab;
use MeowCrew\CancellationOffers\Offer\CPT\Form\LookupService;
use MeowCrew\CancellationOffers\Utils\Formatter;
use MeowCrew\CancellationOffers\Offer\Entity\Offer;

class LimitationsTab extends FormTab {
	
	public function getId(): string {
		return 'limitations';
	}
	
	public function getTitle(): string {
		return __( 'Limits by Products And Users', 'cancellation-surveys-offers-woocommerce-subscriptions' );
	}
	
	public function getDescription(): string {
		return __( 'Select products or product categories the rule will work for.', 'cancellation-surveys-offers-woocommerce-subscriptions' );
	}

	public function getIcon(): string {
		return '<span class="dashicons dashicons-shield-alt"></span>';
	}

	public function render( Offer $offer ) {
		?>
		<style>
			#cancellation-offer-limitations-tab--exclusions-toggle {
				background: #f5f5f5;
				padding: 10px;
				font-weight: bold;
				cursor: pointer;
				text-align: center;
			}

			#cancellation-offer-limitations-tab-exclusions {
				display: none;
			}

			#cancellation-offer-limitations-tab--exclusions-toggle:hover {
				background: #ccc;
			}

			#cancellation-offer-limitations-tab--exclusions-toggle-down {
				display: none;
			}
		</style>
		<script>
			jQuery(document).ready(function ($) {
				jQuery('.cos-select-woo').selectWoo();

				jQuery('#cancellation-offer-limitations-tab--exclusions-toggle').click(function () {
					jQuery('#cancellation-offer-limitations-tab-exclusions').toggle();
					jQuery('#cancellation-offer-limitations-tab--exclusions-toggle-up').toggle();
					jQuery('#cancellation-offer-limitations-tab--exclusions-toggle-down').toggle();
				});
			});
		</script>

		<div id="cancellation-offer-limitations-tab">
			<?php
				
			if ( empty( $offer->getMatchingRule()->getIncludedCategories() ) && empty( $offer->getMatchingRule()->getIncludedProducts() ) ) {
				$this->form->renderHint( __( 'If you do not specify products or product categories, the this offer will work for all products in your store. (excluding products selected in the exclusions section)',
					'cancellation-surveys-offers-woocommerce-subscriptions' ) );
			}
				
				FormTab::renderInputRow( array(
					'id'          => 'matching_rule_included_categories',
					'type'        => 'multiple-select',
					'action'      => 'woocommerce_json_search_cos_categories',
					'placeholder' => 'Search for a category...',
					'label'       => __( 'Included Product Categories', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
					'options'     => ( function () use ( $offer ) {
						
						$options = array();
						
						foreach ( $offer->getMatchingRule()->getIncludedCategories() as $categoryId ) {
							$category = get_term_by( 'id', $categoryId, 'product_cat' );
							
							if ( ! $category ) {
								continue;
							}
							
							$options[ $categoryId ] = array(
								'is_selected' => true,
								'label'       => LookupService::getCategoryLabel( $category ),
							);
						}
						
						return $options;
						
					} )(),
				) );
				
				FormTab::renderInputRow( array(
					'id'          => 'matching_rule_included_products',
					'type'        => 'multiple-select',
					'action'      => 'woocommerce_json_search_products_and_variations',
					'placeholder' => 'Search for a product...',
					'label'       => __( 'Included Products', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
					'options'     => ( function () use ( $offer ) {
						
						$options = array();
						
						foreach ( $offer->getMatchingRule()->getIncludedProducts() as $productID ) {
							$product = wc_get_product( $productID );
							
							if ( ! $product ) {
								continue;
							}
							
							$options[ $productID ] = array(
								'is_selected' => true,
								'label'       => $product->get_name(),
							);
						}
						
						return $options;
					} )(),
				) );
			
			?>

			<hr class="cancellation-offers-components__separator" data-title="
			<?php
			esc_html_e( 'Users',
				'cancellation-surveys-offers-woocommerce-subscriptions' );
			?>
				">
		  
			<?php
			if ( empty( $offer->getMatchingRule()->getIncludedUsers() ) && empty( $offer->getMatchingRule()->getIncludedUserRoles() ) ) {
				$this->form->renderHint( __( 'The offer will work for all users if you do not specify user roles or specific customers. (excluding users selected in the exclusions section)',
					'cancellation-surveys-offers-woocommerce-subscriptions' ) );
			}
				
				FormTab::renderInputRow( array(
					'id'          => 'matching_rule_included_user_roles',
					'type'        => 'multiple-select',
					'css_class'   => 'cos-select-woo',
					'placeholder' => 'Search for a user role...',
					'label'       => __( 'Included User Roles', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
					'options'     => ( function () use ( $offer ) {
						$roles = array();
						
						foreach ( wp_roles()->roles as $key => $WPRole ) {
							$roles[ $key ] = array(
								'label'       => Formatter::formatRoleString( $key ),
								'is_selected' => in_array( $key, $offer->getMatchingRule()->getIncludedUserRoles() ),
							);
						}
						
						return $roles;
						
					} )(),
				) );
				
				FormTab::renderInputRow( array(
					'id'          => 'matching_rule_included_users',
					'type'        => 'multiple-select',
					'action'      => 'woocommerce_json_search_cos_customers',
					'placeholder' => 'Search for a user...',
					'label'       => __( 'Included Users', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
					'options'     => ( function () use ( $offer ) {
						$options = array();
						
						foreach ( $offer->getMatchingRule()->getIncludedUsers() as $userId ) {
							$user = get_user_by( 'id', $userId );
							
							if ( ! $user ) {
								continue;
							}
							
							$options[ $userId ] = array(
								'is_selected' => true,
								'label'       => Formatter::formatCustomerString( new \WC_Customer( $userId ) ),
							);
						}
						
						return $options;
						
					} )(),
				) );
			
			?>

			<div id="cancellation-offer-limitations-tab--exclusions-toggle">
				<?php esc_html_e( 'Exclusions', 'cancellation-surveys-offers-woocommerce-subscriptions' ); ?>
				<span id="cancellation-offer-limitations-tab--exclusions-toggle-down">
					<span class="dashicons dashicons-arrow-up-alt2"></span>
				</span>
				<span id="cancellation-offer-limitations-tab--exclusions-toggle-up">
					<span class="dashicons dashicons-arrow-down-alt2"></span>
				</span>
			</div>
			
			<?php $displayed = $this->hasExclusion( $offer ) ? 'block' : 'none'; ?>

			<div style="margin-top: 20px; display: <?php echo esc_html( $displayed ); ?>"
				 id="cancellation-offer-limitations-tab-exclusions">
				<?php
					FormTab::renderInputRow( array(
						'id'          => 'matching_rule_excluded_categories',
						'type'        => 'multiple-select',
						'action'      => 'woocommerce_json_search_cos_categories',
						'placeholder' => 'Search for a category...',
						'label'       => __( 'Excluded Product Categories', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
						'options'     => ( function () use ( $offer ) {
							
							$options = array();
							
							foreach ( $offer->getMatchingRule()->getExcludedCategories() as $categoryId ) {
								$category = get_term_by( 'id', $categoryId, 'product_cat' );
								
								if ( ! $category ) {
									continue;
								}
								
								$options[ $categoryId ] = array(
									'is_selected' => true,
									'label'       => LookupService::getCategoryLabel( $category ),
								);
							}
							
							return $options;
							
						} )(),
					) );
					
					FormTab::renderInputRow( array(
						'id'          => 'matching_rule_excluded_products',
						'type'        => 'multiple-select',
						'action'      => 'woocommerce_json_search_products_and_variations',
						'placeholder' => 'Search for a product...',
						'label'       => __( 'Excluded Products', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
						'options'     => ( function () use ( $offer ) {
							$options = array();
							
							foreach ( $offer->getMatchingRule()->getExcludedProducts() as $productID ) {
								$product = wc_get_product( $productID );
								
								if ( ! $product ) {
									continue;
								}
								
								$options[ $productID ] = array(
									'is_selected' => true,
									'label'       => $product->get_name(),
								);
							}
							
							return $options;
						} )(),
					) );
				
				
				?>

				<hr class="cancellation-offers-components__separator" data-title="
				<?php
				esc_html_e( 'Users',
					'cancellation-surveys-offers-woocommerce-subscriptions' );
				?>
					">
				
				<?php
					FormTab::renderInputRow( array(
						'id'          => 'matching_rule_excluded_user_roles',
						'type'        => 'multiple-select',
						'css_class'   => 'cos-select-woo',
						'placeholder' => 'Search for a user role...',
						'label'       => __( 'Excluded User Roles', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
						'options'     => ( function () use ( $offer ) {
							$roles = array();
							
							foreach ( wp_roles()->roles as $key => $WPRole ) {
								$roles[ $key ] = array(
									'label'       => Formatter::formatRoleString( $key ),
									'is_selected' => in_array( $key,
										$offer->getMatchingRule()->getExcludedUserRoles() ),
								);
							}
							
							return $roles;
							
						} )(),
					) );
					
					FormTab::renderInputRow( array(
						'id'          => 'matching_rule_excluded_users',
						'type'        => 'multiple-select',
						'action'      => 'woocommerce_json_search_cos_customers',
						'placeholder' => 'Search for a user...',
						'label'       => __( 'Excluded Users', 'cancellation-surveys-offers-woocommerce-subscriptions' ),
						'options'     => ( function () use ( $offer ) {
							$options = array();
							
							foreach ( $offer->getMatchingRule()->getExcludedUsers() as $userId ) {
								$user = get_user_by( 'id', $userId );
								
								if ( ! $user ) {
									continue;
								}
								
								$options[ $userId ] = array(
									'is_selected' => true,
									'label'       => Formatter::formatCustomerString( new \WC_Customer( $userId ) ),
								);
							}
							
							return $options;
						} )(),
					) );
				
				?>
			</div>
		</div>
		<?php
	}
	
	public function hasExclusion( Offer $offer ): bool {
		return ! empty( $offer->getMatchingRule()->getExcludedCategories() ) || ! empty( $offer->getMatchingRule()->getExcludedProducts() ) || ! empty( $offer->getMatchingRule()->getExcludedUsers() ) || ! empty( $offer->getMatchingRule()->getExcludedUserRoles() );
	}
}