<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

function mobile_api_load_more_vehicles() {
	if ( empty( $_REQUEST['mobile_api_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['mobile_api_nonce'] ) ), 'mobile_api-ajax-nonce' ) )
		wp_die( esc_html__( 'Your session has expired. Please reload page and try again.', 'ad-integration-for-mobile-de-api' ) );

	$category = isset( $_REQUEST['category'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['category'] ) ) : '';
	$posts = isset( $_REQUEST['posts'] ) ? intval( $_REQUEST['posts'] ) : 0;
	$page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 0;
	$limit = isset( $_REQUEST['limit'] ) ? intval( $_REQUEST['limit'] ) : 0;
	$meta = isset( $_REQUEST['meta'] ) ? maybe_unserialize( sanitize_text_field( wp_unslash( $_REQUEST['meta'] ) ) ) : array();

	$atts = array(
		'category' => $category,
		'posts' => $posts,
		'page' => $page,
		'limit' => $limit,
		'ajax' => true
	);

	if ( !empty( $meta ) ) :
		foreach ( $meta AS $key => $value )
			$atts[$key] = $value;
	endif;

	echo wp_kses_post( mobile_api_shortcode_vehicle_archive( $atts ) );

	wp_die();
}
add_action( 'wp_ajax_mobile_api_load_more_vehicles', 'mobile_api_load_more_vehicles' );
add_action( 'wp_ajax_nopriv_mobile_api_load_more_vehicles', 'mobile_api_load_more_vehicles' );

function mobile_api_import_vehicles() {
	if ( empty( $_REQUEST['mobile_api_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['mobile_api_nonce'] ) ), 'mobile_api-ajax-nonce' ) )
		wp_die( esc_html__( 'Your session has expired. Please reload page and try again.', 'ad-integration-for-mobile-de-api' ) );

	$page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 0;
	$total = isset( $_REQUEST['total'] ) ? intval( $_REQUEST['total'] ) : 0;
	$limit = isset( $_REQUEST['limit'] ) ? intval( $_REQUEST['limit'] ) : 0;

	$url = 'https://services.mobile.de/search-api/search?page.size=25&page.number='.$page;
	$json = mobile_api_request( $url );
	if ( property_exists( $json, 'ads' ) && !empty( $json->ads ) ) :
		$total = $json->total;
		$limit = $json->maxPages;

		foreach ( $json->ads AS $ad ) :
			$url = 'https://services.mobile.de/search-api/ad/'.$ad->mobileAdId;
			$json = mobile_api_request( $url );

			$args = array(
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
				'meta_key' => 'mobile_api_mobileAdId',
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
				'meta_value' => $ad->mobileAdId,
				'post_type' => 'mobile-api-vehicle',
				'posts_per_page' => 1
			);
			$posts = get_posts( $args );

			if ( $posts )
				mobile_api_update_vehicle( $json, $posts[0]->ID );
			else
				mobile_api_create_vehicle( $json );
		endforeach;
	endif;

	if ( $page == $limit ) :
		$date_format = get_option( 'date_format' );
		$time_format = get_option( 'time_format' );

		$now = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
		update_option( 'mobile_api_last_sync', $now->format( 'Y-m-d\TH:i:s.0\Z' ) ); // ISO-8601
		echo esc_html__( 'Import complete:', 'ad-integration-for-mobile-de-api' ).' '.esc_html( date_i18n( $date_format.', '.$time_format, strtotime( $now->format('Y-m-d\TH:i:s.0\Z') ) ) ).'<br><a href="'.esc_url( admin_url( 'edit.php?post_type=mobile-api-vehicle' ) ).'">» '.esc_html__( 'Go to vehicles', 'ad-integration-for-mobile-de-api' ).'</a><style>#ajax-progress{ display: none; }</style>';
	endif;

	wp_die();
}
add_action( 'wp_ajax_mobile_api_import_vehicles', 'mobile_api_import_vehicles' );
add_action( 'wp_ajax_nopriv_mobile_api_import_vehicles', 'mobile_api_import_vehicles' );