<?php

use Prodigy\Includes\Helpers\Prodigy_Formatting;

defined( 'ABSPATH' ) || exit;
add_filter( 'prodigy_product_tabs', 'prodigy_default_product_tabs' );
add_filter( 'max_srcset_image_width', 'max_srcset_image_width_func' );

$is_elementor_active = did_action( 'elementor/loaded' ) || defined( 'ELEMENTOR_VERSION' ) || class_exists( '\Elementor\Plugin' );

$show_description   = ! $is_elementor_active || ! empty( $args['general_tabs_controls_content_description'] );
$show_additional    = ! $is_elementor_active || ! empty( $args['general_tabs_controls_content_additional_info'] );
$show_reviews       = ! $is_elementor_active || ! empty( $args['general_tabs_controls_content_reviews'] );
$show_tiered_prices = ! $is_elementor_active || ! empty( $args['general_tabs_controls_content_tiered_prices'] );

$is_enable_review = (bool) get_option( 'pg_product_review' );
if ( ! $is_enable_review ) {
	$show_reviews = false;
}

$tabs = get_active_product_tab( apply_filters( 'prodigy_product_tabs', $params ?? array() ), $params['settings'] ?? array() );

if ( ! empty( $tabs ) ) : ?>
	<div class="prodigy-custom-template">
		<div class="prodigy-tabs prodigy-tabs-js row" id="prodigy-tabs">
			<div class="d-none d-md-flex flex-md-column col-lg-12 desktop-resolution-js">
				<?php if ( count( $tabs ) > 1 ) : ?>
					<ul class="prodigy-tabs__tabs-list nav nav-tabs flex-wrap tabs pg-tabs" id="product-tab"
						role="tablist">
						<?php foreach ( $tabs as $key => $tab ) : ?>
							<?php
							$is_enable_review = get_option( 'pg_product_review' );
							if ( $key === 'reviews' && ! $is_enable_review ) {
								continue;
							}

							if ( isset( $params['settings']['idWidget'] ) ) {
								$callFuncActive = isset( $tabs[ $key ]['active'] ) || isset( $tabs[ $key ][ $tab['callback'] ?? '' ] );
							} else {
								$callFuncActive = true;
							}
							?>
							<?php if ( $callFuncActive ) : ?>
								<li role="presentation" class="prodigy-tabs__tabs-item nav-item <?php echo esc_attr( $key ); ?>_tab <?php echo isset( $tabs[ $key ]['active'] ) ? 'active' : ''; ?>"
									id="tab-li-<?php echo esc_attr( $key ); ?>">
									<a class="prodigy-tabs__tabs-link pl-0 pr-0 nav-link <?php echo esc_attr( $key ); ?>_tab <?php echo isset( $tabs[ $key ]['active'] ) ? 'active' : ''; ?>"
										id="tab-title-<?php echo esc_attr( $key ); ?>" data-toggle="tab"
										data-href="#tab-<?php echo esc_attr( $key ); ?>" role="tab" href="#tab-<?php echo esc_attr( $key ); ?>"
										aria-controls="tab-<?php echo esc_attr( $key ); ?>"
										aria-selected="<?php echo isset( $tabs[ $key ]['active'] ) ? 'true' : 'false'; ?>"
										<?php if ( isset( $is_show_tab ) ) : ?>
											style="display:<?php echo $is_show_tab[ $key ] ? 'block' : 'none'; ?>" <?php endif; ?>>
										<?php
										if ( isset( $tab['title'] ) ) {
											echo wp_kses( apply_filters( 'prodigy_product_' . $key . '_tab_title', esc_html( $tab['title'] ), esc_attr( $key ) ), Prodigy_Formatting::get_allowed_html() );
										}
										?>
									</a>
								</li>
							<?php endif; ?>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>
				<?php foreach ( $tabs as $key => $tab ) : ?>
					<div class="panel entry-content pg-tab" id="tab-<?php echo esc_attr( $key ); ?>" role="tabpanel"
						aria-labelledby="tab-title-<?php echo esc_attr( $key ); ?>"
						aria-hidden="<?php echo isset( $tabs[ $key ]['active'] ) ? 'false' : 'true'; ?>"
						style="display:<?php echo isset( $tabs[ $key ]['active'] ) ? 'block' : 'none'; ?>">
						<div class="prodigy-tabs__desc" id="prodigy-tabs__desc-<?php echo esc_attr( $key ); ?>">
							<?php

							if ( isset( $params['settings']['idWidget'] ) ) {
								$callFuncCall = ( isset( $tab['callback'] ) && isset( $tabs[ $key ][ $tab['callback'] ] ) );
							} else {
								$callFuncCall = true;
							}

							if (
								isset( $tab['callback'] )
								&& $callFuncCall
							) {
								call_user_func( $tab['callback'], $tab['settings'] );
							}
							?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
			<div class="col-12 d-md-none pl-0 pr-0 mobile-resolution-js">
				<div class="prodigy-product__accordion">
					<!-- description -->
					<?php if ( $show_description ) : ?>
						<div class="prodigy-product__accordion-card mb-0">
							<button type="button" class="prodigy-product__accordion-card-button position-relative d-flex justify-content-between align-items-center"
								data-toggle="collapse"
								data-target="#collapse_description"
								aria-controls="collapse_description"
								aria-expanded="true">
								<span class="prodigy-product__accordion-subtitle"><?php esc_html_e( 'Description', 'prodigy-commerce' ); ?></span>
								<span class="prodigy-product__accordion-card-btn prodigy-icon-btn" aria-hidden="true">
									<i class="icon icon-arrow-down"></i>
								</span>
							</button>
							<div class="collapse show" id="collapse_description" aria-hidden="false">
								<div class="prodigy-product__accordion-card-body">
									<div class="prodigy-product__card-desc" id="prodigy-product__card-desc">
										<?php echo '<section>' . wp_kses( $params['description'], Prodigy_Formatting::get_allowed_html() ) . '</section>'; ?>
									</div>
								</div>
							</div>
						</div>
					<?php endif; ?>
					<!-- end description -->
					<!-- additional -->
					<?php if ( $show_additional ) : ?>
						<div class="prodigy-product__accordion-card prodigy-product__accordion-card-info mb-0">
							<button type="button" class="prodigy-product__accordion-card-button position-relative d-flex justify-content-between align-items-center"
								data-toggle="collapse"
								data-target="#collapse_additional"
								aria-controls="collapse_additional"
								aria-expanded="false">
								<span class="prodigy-product__accordion-subtitle"><?php esc_html_e( 'Additional Information', 'prodigy-commerce' ); ?></span>
								<span class="prodigy-product__accordion-card-btn prodigy-icon-btn" aria-hidden="true">
									<i class="icon icon-arrow-down"></i>
								</span>
							</button>
							<div class="collapse" id="collapse_additional" aria-hidden="true">
								<div class="prodigy-product__accordion-card-body">

									<div class="prodigy-tabs__desc" id="prodigy-tabs__desciption-additional">
										<table class="prodigy-product__additional">
											<?php
											if ( isset( $params['settings'] ) ) {
												$settings['settings']['product'] = $GLOBALS['prodigy_product'];
												do_action( 'prodigy_product_additional_information', $params['settings'] );
											} else {
												do_action( 'prodigy_product_additional_information' );
											}
											?>
										</table>
									</div>
								</div>
							</div>
						</div>
					<?php endif; ?>
					<!-- end additional -->
					<!-- review -->
					<?php if ( $show_reviews ) : ?>
						<div class="prodigy-product__accordion-card prodigy-product__accordion-card-review mb-0">
							<button type="button" class="prodigy-product__accordion-card-button position-relative d-flex justify-content-between align-items-center"
								data-toggle="collapse"
								data-target="#collapse_reviews"
								aria-controls="collapse_reviews"
								aria-expanded="false">
								<span class="prodigy-product__accordion-subtitle"><?php esc_html_e( 'Reviews', 'prodigy-commerce' ); ?></span>
								<span class="prodigy-product__accordion-card-btn prodigy-icon-btn" aria-hidden="true">
									<i class="icon icon-arrow-down"></i>
								</span>
							</button>
							<div class="collapse" id="collapse_reviews" aria-hidden="true">
								<div class="prodigy-product__accordion-card-body">
									<!-- content here -->
									<div class="prodigy-tabs__desc" id="prodigy-tabs__desc-reviews-mobile">
										<?php do_action( 'prodigy_product_review_tab', $params ); ?>
									</div>
								</div>
							</div>
						</div>
					<?php endif; ?>
					<!-- tiered price -->
					<?php if ( $show_tiered_prices ) : ?>
						<div class="prodigy-product__accordion-card prodigy-product__accordion-card-price mb-0">
							<button type="button" class="prodigy-product__accordion-card-button position-relative d-flex justify-content-between align-items-center"
								data-toggle="collapse"
								data-target="#collapse_tired"
								aria-controls="collapse_tired"
								aria-expanded="false">
								<span class="prodigy-product__accordion-subtitle"><?php esc_html_e( 'QTY and Price Breakes', 'prodigy-commerce' ); ?></span>
								<span class="prodigy-product__accordion-card-btn prodigy-icon-btn" aria-hidden="true">
									<i class="icon icon-arrow-down"></i>
								</span>
							</button>
							<div class="collapse" id="collapse_tired" aria-hidden="true">
								<div class="prodigy-product__accordion-card-body">
									<!-- content here -->
									<div class="prodigy-tabs__desc" id="prodigy-tabs__desc-qty-pb">
										<?php do_action( 'prodigy_product_tiered_tab', $params ); ?>
									</div>
								</div>
							</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php if ( $show_reviews ) : ?>
			<?php do_action( 'prodigy_review_modals', array( 'product' => $params['product'] ) ); ?>
			<?php do_action( 'prodigy_success_review_modals' ); ?>
		<?php endif; ?>
	</div>
<?php endif; ?>
