<?php
/**
 * Views: Tripzzy Themes.
 *
 * @package tripzzy
 */

namespace Tripzzy\Admin\Views;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
use Tripzzy\Core\Helpers\Transient;

if ( ! class_exists( 'Tripzzy\Admin\Views\ThemesView' ) ) {
	/**
	 * ThemesView Class.
	 *
	 * @since 1.1.2
	 */
	class ThemesView {

		/**
		 * Themes page html.
		 *
		 * @since 1.4.0 Set Transient expire time to 6 hours.
		 * @since 1.1.2
		 */
		public static function render() {

			$addons_data = Transient::get( 'theme_infos' );
			$expire_time = Transient::get_timeout( 'theme_infos' );
			if ( ! $addons_data || ! $expire_time ) {
				$response = wp_remote_get( 'https://wptripzzy.com/wp-json/wp/v2/tripzzy-themes' );
				if ( is_wp_error( $response ) ) {
					return;
				}
				if ( isset( $response['body'] ) ) {
					$addons_data = (array) json_decode( $response['body'], true );
				}
				Transient::set( 'theme_infos', $addons_data, HOUR_IN_SECONDS * 6 );
			}
			if ( count( $addons_data ) > 0 ) {
				$comming_soon_url = sprintf( '%sassets/images/coming-soon.png', esc_url( TRIPZZY_PLUGIN_DIR_URL ) );
				?>
				<div class="wrap">
				<hr class="wp-header-end">
					<div class="tripzzy-page-wrapper">
						<div id="tripzzy-themes-page" class="tripzzy-page tripzzy-themes-page" >
							<ul class="tripzzy-themes-list">
								<?php
								foreach ( $addons_data as $theme_info ) :
									$title         = $theme_info['title']['rendered'] ?? '';
									$thumbnail_url = $comming_soon_url;

									if ( isset( $theme_info['featured_media'] ) && $theme_info['featured_media'] ) {
										$thumbnail_url = isset( $theme_info['featured_media_url'] ) && $theme_info['featured_media_url'] ? $theme_info['featured_media_url'] : $comming_soon_url;
									}
									?>
								<li>
									<div class="tripzzy-themes-list-thumbnail">
										<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt="<?php echo esc_attr( $title ); ?>" />
									</div>
									<div class="tripzzy-themes-list-title">
										<h3><?php echo esc_html( $title ); ?></h3>
										<span>
											<a href="<?php echo esc_url( $theme_info['theme_url'] ?? '' ); ?>" target="_blank"><i class="fa-solid fa-download"></i></a>
										</span>
									</div>
								</li>
								<?php endforeach; ?>
								<?php for ( $i = 1;$i <= 3; $i++ ) : ?>
									<li class="tripzzy-theme-comming-soon">
										<div class="tripzzy-themes-list-thumbnail">
											<img src="<?php echo esc_url( $comming_soon_url ); ?>" />
										</div>
										<div class="tripzzy-themes-list-title">
											<h3><?php esc_html_e( 'Coming Soon', 'tripzzy' ); ?></h3>
										</div>
									</li>
								<?php endfor; ?>
							</ul>
						</div>
					</div>
				</div>
				<?php
			}
		}
	}
}
