<?php
namespace SabaiApps\Directories\Component\Payment\Feature;

use SabaiApps\Directories\Component\Payment\IPlan;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class PlanFeature extends AbstractFeature {

	protected function _paymentFeatureInfo() {
		return array(
			'label'            => __( 'Plan Duration Settings', 'directories-pro' ),
			'weight'           => -1,
			'default_settings' => array(
				'duration_unlimited' => false,
				'duration'           => 365,
			),
			'entity_levelable' => false,
		);
	}

	public function paymentFeatureSettingsForm( Bundle $bundle, array $settings, $planType = 'base', array $parents = array() ) {
		if ( $planType !== 'base' ) {
			return;
		}

		$max_duration = $this->_application->Filter( 'payment_feature_plan_max_duration', 365, array( $bundle, $planType ) );
		return array(
			'duration_unlimited' => array(
				'#title'         => __( 'Duration in days', 'directories-pro' ),
				'#on_label'      => __( 'Unlimited', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#switch'        => false,
				'#default_value' => ! empty( $settings['duration_unlimited'] ),
				'#horizontal'    => true,
			),
			'duration'           => array(
				'#type'          => 'slider',
				'#default_value' => isset( $settings['duration'] ) ? $settings['duration'] : $max_duration,
				'#integer'       => true,
				'#field_suffix'  => __( 'day(s)', 'directories-pro' ),
				'#min_value'     => 1,
				'#max_value'     => $max_duration,
				'#step'          => $max_duration > 365 ? 5 : 1,
				'#states'        => array(
					'invisible' => array(
						sprintf( 'input[name="%s[duration_unlimited][]"]', $this->_application->Form_Form_fieldName( $parents ) ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
				'#horizontal'    => true,
			),
		);
	}

	public function paymentFeatureOnAdded( IEntity $entity, array $settings, IPlan $plan, array &$values ) {
		$settings = array(
			'plan_id'    => $plan->paymentPlanId(),
			'duration'   => $duration = ( empty( $settings['duration_unlimited'] ) && ! empty( $settings['duration'] ) ? (int) $settings['duration'] : 0 ),
			'extra_data' => array(
				'duration' => $duration,
			),
		);
		if ( $current = $entity->getSingleFieldValue( 'payment_plan' ) ) {
			$settings['prev_value'] = $current;
		}
		return $settings;
	}

	public function paymentFeatureApply( IEntity $entity, $featureName, array $featureSettings, array &$values, $orderId, $isAddon = false ) {
		if ( empty( $featureSettings['plan_id'] ) ) {
			return false;
		}

		if ( ! empty( $featureSettings['duration'] ) ) {
			if ( isset( $featureSettings['prev_value']['expires_at'] )
				&& $featureSettings['prev_value']['expires_at'] > time()
			) {
				$expires_at = $featureSettings['prev_value']['expires_at'];
			} else {
				$expires_at = time();
			}
			$expires_at += 86400 * $featureSettings['duration'];
		} else {
			$expires_at = 0;
		}

		$values['payment_plan'] = $this->_application->Filter(
			'payment_plan_value',
			array(
				'plan_id'        => $featureSettings['plan_id'],
				'expires_at'     => $expires_at,
				'extra_data'     => $featureSettings['extra_data'] ?? array(),
				'deactivated_at' => 0,
				'component'      => $this->_application->getComponent( 'Payment' )->getPaymentComponent( false, true ),
			),
			array( $entity, $featureName, $featureSettings )
		);

		return true;
	}

	public function paymentFeatureUnapply( IEntity $entity, $featureName, array $featureSettings, array &$values, $orderId, $isAddon = false ) {
		// Revert back to previously configured payment plan values if any
		if ( isset( $featureSettings['prev_value'] ) ) {
			$values['payment_plan'] = $featureSettings['prev_value'];
		} else {
			$values['payment_plan'] = false;
		}

		return true;
	}

	public function paymentFeatureRender( Bundle $bundle, array $settings, ?IPlan $plan = null ) {
		if ( ! $plan || $plan->paymentPlanType() !== 'base' ) {
			return;
		}

		if ( empty( $settings['duration_unlimited'] ) && ! empty( $settings['duration'] ) ) {
			$label = sprintf(
				$this->_application->H( __( 'Duration: %s', 'directories-pro' ) ),
				'<em>' . sprintf( $this->_application->H( _n( '%s Day', '%s Days', $settings['duration'], 'directories-pro' ) ), $settings['duration'] ) . '</em>'
			);
		} else {
			$label = sprintf(
				$this->_application->H( __( 'Duration: %s', 'directories-pro' ) ),
				'<em>' . __( 'Unlimited', 'directories-pro' ) . '</em>'
			);
		}
		return array(
			array(
				'icon'     => 'fas fa-calendar-alt',
				'html'     => $label,
				'settings' => $settings,
			),
		);
	}
}
