<?php
namespace SabaiApps\Directories\Component\PaymentGateway\FieldWidget;

use SabaiApps\Directories\Component\Field\Widget\AbstractWidget;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Form\Form;
use SabaiApps\Directories\Component\PaymentGateway\PaymentSubscription;

class SubscriptionFieldWidget extends AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'label'       => __( 'Subscription Information', 'directories-pro' ),
			'field_types' => array( $this->_name ),
		);
	}

	protected function _getPossibleStatuses( $status ) {
		switch ( $status ) {
			case 'active':
				return array( 'cancelled' );
			default:
				return array();
		}
	}

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?IEntity $entity = null, array $parents = array(), $language = null ) {
		if ( ! isset( $entity ) ) {
			return;
		}

		$status = isset( $value['status'] ) ? $value['status'] : null;
		$form   = array(
			'#admin_only_main'  => true,
			'#element_validate' => array(
				function ( Form $form, &$value ) use ( $entity, $status ) {
					if ( $value['status'] === $status ) {
						return;
					}

					if ( ! $new_values = $this->_application->PaymentGateway_Subscription_updateStatus(
						$entity,
						$value['status'],
						array(
							'user_id' => $this->_application->getUser()->id,
							'save'    => false,
							'notes'   => $value['notes'],
						)
					)
						) {
						return;
					}

					$value = $new_values + $value;
				},
			),
			'number'            => array(
				'#type'       => 'item',
				'#title'      => $field->Bundle->getLabel( 'singular' ),
				'#markup'     => $this->_application->H( $value['number'] ),
				'#horizontal' => true,
			),
		);
		if ( ! empty( $value['number'] ) ) {
			$form['number'] = array(
				'#type'       => 'item',
				'#title'      => $field->Bundle->getLabel( 'singular' ),
				'#markup'     => $this->_application->H( $value['number'] ),
				'#horizontal' => true,
			);
			if ( ! empty( $value['gateway'] )
				&& ( $gateway = $this->_application->PaymentGateway_Util_gateway( $value['gateway'], true ) )
				&& ( $subscription_url = $gateway->paymentGatewaySubscriptionUrl( $entity ) )
			) {
				$form['number']['#markup'] = '<a target="_blank" rel="external nofollow noopener noreferrer" href="' . $this->_application->H( $subscription_url ) . '">' . $form['number']['#markup'] . '</a>';
			}
		}
		if ( ! empty( $value['parent_entity_type'] )
			&& ! empty( $value['parent_entity_id'] )
			&& ( $content_entity = $this->_application->Entity_Entity( $value['parent_entity_type'], $value['parent_entity_id'] ) )
			&& ( $content_bundle = $this->_application->Entity_Bundle( $content_entity ) ) ) {
			$form['content'] = array(
				'#type'       => 'item',
				'#title'      => $content_bundle->getLabel( 'singular' ),
				'#markup'     => '<a href="' . $this->_application->Entity_AdminUrl( $content_entity ) . ' ">' . $this->_application->H( $this->_application->Entity_Title( $content_entity ) ) . '</a>',
				'#horizontal' => true,
			);
		}

		if ( ( $possible_statuses = $this->_getPossibleStatuses( $status ) ) ) {
			$statuses            = $this->_application->PaymentGateway_Subscription_statuses();
			$possible_statuses[] = $status;
			$options             = array();
			foreach ( $statuses as $status_name => $_status ) {
				if ( in_array( $status_name, $possible_statuses ) ) {
					$options[ $status_name ] = $_status['label'];
				}
			}
			$form['status'] = array(
				'#type'          => 'select',
				'#title'         => __( 'Status', 'directories-pro' ),
				'#options'       => $options,
				'#default_value' => $status,
				'#horizontal'    => true,
			);
		} else {
			$status_markup  = $this->_application->PaymentGateway_Subscription_statusLabel( $status, true, true );
			$form['status'] = array(
				'#type'       => 'item',
				'#title'      => __( 'Status', 'directories-pro' ),
				'#markup'     => $status_markup,
				'#horizontal' => true,
			);
		}
		if ( $status === 'active'
			&& ! empty( $value['next_payment_at'] )
			&& $value['next_payment_at'] > time()
		) {
			$form['next_payment_at'] = array(
				'#type'       => 'item',
				'#title'      => __( 'Next Payment', 'directories-pro' ),
				'#markup'     => $this->_application->System_Date_datetime( $value['next_payment_at'] ),
				'#horizontal' => true,
			);
		}
		if ( ! empty( $value['data']['order_id'] )
			&& ( $order = $this->_application->Entity_Entity( 'post', $value['data']['order_id'] ) )
		) {
			$form['related_order'] = array(
				'#type'       => 'item',
				'#title'      => __( 'Related Order', 'directories-pro' ),
				'#markup'     => '<a href="' . $this->_application->Entity_AdminUrl( $order ) . '">' . $this->_application->H( $order->getSingleFieldValue( 'paymentgateway_order', 'number' ) ) . '</a>',
				'#horizontal' => true,
			);
		}
		$form['summary'] = array(
			'#title'      => __( 'Subscription summary', 'directories-pro' ),
			'#type'       => 'item',
			'#markup'     => $this->_application->PaymentGateway_Subscription_renderSummary( $entity ),
			'#horizontal' => true,
		);
		$form['notes']   = $this->_application->PaymentGateway_Util_notesForm(
			__( 'Subscription notes', 'directories-pro' ),
			$value['notes'] ?? null,
			(array) $entity->getSingleFieldValue( 'paymentgateway_subscription', 'notes' ),
			true
		);

		return $form;
	}
}
