<?php

namespace ICD\Hosting\Shortcodes;

use ICD\Hosting\ICD_Hosting;
use ICD\Hosting\Services\AppException;
use ICD\Hosting\Services\OrderHelper;
use ICD\Hosting\Services\OrderService;

class ICD_Hosting_Shortcode_Renew {

	/**
	 * Output Renew page
	 *
	 */
	public static function output() {
		global $wp;

		$related_resources = empty( $_GET['related_resources'] ) ? null : sanitize_text_field( wp_unslash( $_GET['related_resources'] ) );
		$store_resource_id = empty( $wp->query_vars['resource_id'] ) ? null : sanitize_text_field( wp_unslash( $wp->query_vars['resource_id'] ) );
		$order             = empty( $wp->query_vars['order'] ) ? [] : sanitize_text_field( wp_unslash( $wp->query_vars['order'] ) );

		try {
			$viewdata = [];
			$params   = explode( ':', $store_resource_id );
			if ( ! is_numeric( $params[0] ) || ! isset( $params[1] ) ) {
				icd_hosting_error( 'invalid.request_id' );
			}

			$include_related_resources = 1;

			if ( ! is_null( $related_resources ) ) {
				$include_related_resources = $related_resources ? 1 : 0;
			}

			$commands = array(
				array( 'command' => 'countries' ),
				array( 'command' => 'tlds' ),
				array(
					'command' => 'order-request-build',
					'params'  => array(
						'order'       => $order,
						'request'     => [ [ 'action' => 'renewal', 'resource_id' => $params[0] ] ],
						'options'     => [
							'include_related_resources' => $include_related_resources,
							'request_items_only'        => 1,
							'break_on_errors'           => 0
						],
						'get_catalog' => 1,
					)
				),
			);

			$results = \ICD\Hosting\ICD_Hosting()->getApp()->api->commands( $commands, 'POST' );

			foreach ( $results as $result ) {
				if ( empty( $result['status'] ) ) {
					icd_hosting_error( 'error.failed_to_retrieve_order_data' );
				}
			}

			if ( $params[1] != md5( $results[2]['data']['store']['id'] . ':' . $params[0] ) ) {
				icd_hosting_error( 'error.failed_to_retrieve_order_data' );
			}

			OrderHelper::init( $results[2]['data'] );

			$viewdata                   = array(
				'store'      => $results[2]['data']['store'],
				'countries'  => OrderHelper::translateCountries( $results[0]['data']['countries'] ),
				'order'      => $results[2]['data']['order'],
				'catalog'    => $results[2]['data']['catalog'],
				'products'   => $results[2]['data']['products'],
				'processors' => ICD_Hosting::instance()->getApp()->payment->processors(),
				'tld_info'   => $results[1]['data']['tlds'],
				'terms'      => 1,
				'icann'      => 0,
				'icann_show' => 0,
				'tlds'       => array(),
			);
			$viewdata['payment_method'] = $viewdata['processors'][ $viewdata['order']['payment_method'] ] ?? key( $viewdata['processors'] );
			$viewdata                   = \ICD\Hosting\ICD_Hosting()->order_service->requestCommonViewData( $viewdata );

			$viewdata['ajax_url']            = icd_hosting_admin_ajax_url();
			$viewdata['request_prices']      = 'icd_hosting_request_prices';
			$viewdata['domain_check']        = 'icd_hosting_domain_check';
			$viewdata['approver_emails']     = 'icd_hosting_approver_emails';
			$viewdata['request_submit']      = 'icd_hosting_request_submit';
			$viewdata['price_change_action'] = 'icd_hosting_request_prices';

			icd_hosting_get_template( 'request', $viewdata );
		} catch ( AppException $e ) {
			if ( ! is_admin() ) {
				icd_hosting_display_error( $e );
			}
		} catch ( \Exception $e ) {
			icd_hosting_display_error( 'error.failed_to_retrieve_order_data' );
		}
	}
}
