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

use SabaiApps\Directories\Component\Field\Type\AbstractType;
use SabaiApps\Directories\Component\Field\Type\IColumnable;
use SabaiApps\Directories\Component\Field\Type\IRestrictable;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Field\Type\IContent;
use SabaiApps\Directories\Component\PaymentGateway\PaymentPlan;

class PlanFieldType extends AbstractType implements
	IColumnable,
	IRestrictable,
	IContent {

	protected function _fieldTypeInfo() {
		return array(
			'label'      => __( 'Payment Plan Information', 'directories-pro' ),
			'creatable'  => false,
			'disablable' => false,
			'requirable' => false,
		);
	}

	public function fieldTypeSchema( array $settings = array() ) {
		return array(
			'columns' => array(
				'plan_type'     => array(
					'type'    => Application::COLUMN_VARCHAR,
					'length'  => 20,
					'notnull' => true,
					'was'     => 'plan_type',
					'default' => '',
				),
				'active'        => array(
					'type'    => Application::COLUMN_INTEGER,
					'length'  => 1,
					'notnull' => true,
					'was'     => 'active',
					'default' => 1,
				),
				'featured'      => array(
					'type'    => Application::COLUMN_INTEGER,
					'length'  => 1,
					'notnull' => true,
					'was'     => 'featured',
					'default' => 0,
				),
				'display_order' => array(
					'type'    => Application::COLUMN_INTEGER,
					'length'  => 2,
					'notnull' => true,
					'was'     => 'display_order',
					'default' => 0,
				),
				'content_name'  => array(
					'type'    => Application::COLUMN_VARCHAR,
					'length'  => 40,
					'notnull' => true,
					'was'     => 'content_name',
					'default' => '',
				),
				'data'          => array(
					'type'    => Application::COLUMN_JSON,
					'notnull' => false,
					'default' => null,
				),
			),
			'indexes' => array(
				'plan_type'     => array(
					'fields' => array( 'plan_type' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'plan_type',
				),
				'active'        => array(
					'fields' => array( 'active' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'active',
				),
				'featured'      => array(
					'fields' => array( 'featured' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'featured',
				),
				'content_name'  => array(
					'fields' => array( 'content_name' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'content_name',
				),
				'display_order' => array(
					'fields' => array( 'display_order' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'display_order',
				),
			),
		);
	}

	public function fieldTypeOnSaveValues( IField $field, array $values, ?array $currentValues = null, array &$extraArgs = array() ) {
		$value = array_shift( $values ); // single entry allowed for this field
		if ( ! is_array( $value ) ) {
			if ( $value === null ) {
				return; // do nothing
			}

			return array( false ); // delete
		}

		// Make sure required properties are set
		foreach ( array( 'plan_type', 'content_name' ) as $required_key ) {
			if ( ! isset( $value[ $required_key ] ) ) {
				if ( empty( $currentValues[0][ $required_key ] ) ) {
					return array( false ); // delete
				}

				$value[ $required_key ] = $currentValues[0][ $required_key ];
			} elseif ( empty( $value[ $required_key ] ) ) {
				return array( false ); // delete
			}
		}

		// Keep current values unless explicitly changed
		foreach ( array(
			'active'   => 1,
			'featured' => 0,
		) as $key => $default_value ) {
			if ( isset( $value[ $key ] ) ) {
				continue;
			}

			$value[ $key ] = $currentValues[0][ $key ] ?? $default_value;
		}

		// Update data
		if ( ! isset( $value['data'] ) ) {
			if ( ! empty( $currentValues[0]['data'] ) ) {
				$value['data'] = $currentValues[0]['data'];
			}
		}

		return array( $value );
	}

	public function fieldColumnableInfo( IField $field ) {
		return array(
			'plan_type'   => array(
				'label' => __( 'Plan type', 'directories-pro' ),
			),
			'description' => array(
				'label' => __( 'Description', 'directories-pro' ),
			),
			'price'       => array(
				'label' => __( 'Price', 'directories-pro' ),
			),
			'status'      => array(
				'label' => __( 'Status', 'directories-pro' ),
			),
			'featured'    => array(
				'label' => __( 'Featured', 'directories-pro' ),
			),
		);
	}

	public function fieldColumnableColumn( IField $field, IEntity $entity, $column = '' ) {
		if ( ! $value = $entity->getSingleFieldValue( $field->getFieldName() ) ) {
			return;
		}

		switch ( $column ) {
			case 'featured':
				if ( $value['plan_type'] === 'addon' ) {
					return '';
				}
				return '<span role="button" class="drts-action-button drts-payment-set-plan-status ' . DRTS_BS_PREFIX . 'text-' . ( $value['featured'] == 1 ? 'warning' : 'muted' ) . '"'
					. ' data-action-data-status="featured" data-action-status-color="warning"'
					. ' data-action-url="' . $this->_application->AdminUrl( '/_drts/paymentgateway/set_plan_status', array(), '', '&' ) . '"'
					. ' data-action-token="' . $this->_application->Form_Form_createToken( 'paymentgateway_admin_plans', 1800, true ) . '"'
					. ' data-action-data-id="' . $entity->getId() . '">'
					. '<i class="fas fa-star fa-xl"></i></span>';
			case 'plan_type':
				$plan_types = $this->_application->PaymentGateway_Util_planTypes();
				return isset( $plan_types[ $value['plan_type'] ] ) ? $this->_application->H( $plan_types[ $value['plan_type'] ] ) : '<span class="' . DRTS_BS_PREFIX . 'text-danger">' . $this->_application->H( $value['plan_type'] ) . '</span>';
			case 'description':
				return $this->_application->System_Text_summarize( $entity->getContent(), 80 );
			case 'price':
				return $this->_application->Payment_Plan_formattedPrice( new PaymentPlan( $entity ) );
			case 'status':
				return '<span role="button" class="drts-action-button drts-payment-set-plan-status ' . DRTS_BS_PREFIX . 'text-' . ( $value['active'] == 1 ? 'success' : 'muted' ) . '"'
					. ' data-action-url="' . $this->_application->AdminUrl( '/_drts/paymentgateway/set_plan_status', array(), '', '&' ) . '"'
					. ' data-action-token="' . $this->_application->Form_Form_createToken( 'paymentgateway_admin_plans', 1800, true ) . '"'
					. ' data-action-data-id="' . $entity->getId() . '"'
					. ' title="' . $this->_application->H( $value['active'] == 1 ? __( 'Active', 'directories-pro' ) : __( 'Inactive', 'directories-pro' ) ) . '">'
					. '<i class="fas fa-check-circle fa-xl"></i></span>';
		}
	}

	public function fieldRestrictableOptions( IField $field ) {
		return array(
			'plan_type' => array(
				'' => array( 'label' => __( 'Plan type', 'directories-pro' ) ),
			) + $this->_application->PaymentGateway_Util_planTypes(),
			'active'    => array(
				'' => array( 'label' => __( 'Status', 'directories-pro' ) ),
				1  => __( 'Active', 'directories-pro' ),
				0  => __( 'Inactive', 'directories-pro' ),
			),
		);
	}

	public function fieldRestrictableRestrict( IField $field, $value, $name ) {
		return array( 'column' => $name );
	}

	public function fieldContent( IField $field, array $values ) {
		return $values[0]['description'] ?? null;
	}
}
