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

use SabaiApps\Directories\Component\Field;
use SabaiApps\Directories\Component\Entity;

class FieldWidget extends Field\Widget\AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'field_types' => array( $this->_name ),
		);
	}

	public function fieldWidgetForm( Field\IField $field, array $settings, $value = null, ?Entity\Type\IEntity $entity = null, array $parents = array(), $language = null ) {
		if ( ! isset( $entity )
			|| strpos( $field->getFieldName(), 'voting_' ) !== 0
		) {
			return;
		}

		$type = substr( $field->getFieldName(), strlen( 'voting_' ) );
		if ( ! $type_impl = $this->_application->Voting_Types_impl( $type, true ) ) {
			return;
		}

		$table_headers = $type_impl->votingTypeInfo( 'vote_table_headers' );
		unset( $table_headers['entity'] );
		$votes   = $this->_application->getModel( 'Vote', 'Voting' )
			->fieldName_is( $field->getFieldName() )
			->entityId_is( $entity->getId() )
			->marked_is( 0 )
			->fetch();
		$options = array();
		foreach ( $votes as $vote ) {
			$options[ $vote->id ] = $type_impl->votingTypeTableRow( $vote, $table_headers );
		}
		if ( empty( $options ) ) {
			$value = $entity->getSingleFieldValue( $field->getFieldName() );
			if ( ! isset( $value[''] ) ) {
				return;
			}

			if ( empty( $value['']['count'] )
				&& empty( $value['']['sum'] )
				&& empty( $value['']['average'] )
			) {
				return;
			}
		}

		$admin_path = $this->_application->Entity_BundleTypeInfo_adminPath( $field->Bundle );
		$form       = array(
			'#admin_title' => $type_impl->votingTypeInfo( 'label' ),
			'#disabled'    => true, // disable so that not values are submitted
			'#group'       => true,
			'#class'       => 'drts-voting-' . $type . '-' . $entity->getId(), // for ajax delete target
			'clear'        => array(
				'#type'   => 'markup',
				'#weight' => 2,
				'#markup' => $this->_application->LinkTo(
					__( 'Clear all', 'directories-pro' ),
					$this->_application->AdminUrl(
						$admin_path . '/votes/clear',
						array(
							'entity_id'  => $entity->getId(),
							'field_name' => $field->getFieldName(),
						),
						'',
						'&'
					),
					array(
						'container' => 'modal',
						'modalSize' => 'lg',
					),
					array(
						'class'            => DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-link ' . DRTS_BS_PREFIX . 'btn-sm ' . DRTS_BS_PREFIX . 'text-danger',
						'data-modal-title' => $field->getFieldLabel() . ' - ' . __( 'Clear all', 'directories-pro' ),
					)
				),
			),
		);
		if ( ! empty( $options ) ) {
			$form['votes'] = array(
				'#type'     => 'tableselect',
				'#weight'   => 1,
				'#header'   => $table_headers,
				'#options'  => $options,
				'#disabled' => true,
				'#class'    => 'drts-data-table',
				'#size'     => 'sm',
			);
		} elseif ( isset( $value['']['count'] ) ) {
				$form['votes'] = array(
					'#type'   => 'markup',
					'#weight' => 1,
					'#markup' => '<div>' . sprintf( $type_impl->votingTypeFormat( $value[''] ), $value['']['count'] ) . '</div>',
				);
		}

		return $form;
	}
}
