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

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

class GuestFieldWidget extends Field\Widget\AbstractWidget {

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

	public function fieldWidgetForm( Field\IField $field, array $settings, $value = null, ?IEntity $entity = null, array $parents = array(), $language = null ) {
		if ( isset( $entity )
			&& $entity->getAuthorId()
		) {
			return;
		}

		if ( $this->_application->getPlatform()->isAdmin() ) {
			// Backend
			if ( ! $this->_application->getUser()->isAdministrator() ) {
				return;
			}

			$form = array(
				'name'  => array(
					'#type'          => 'textfield',
					'#default_value' => isset( $value['name'] ) ? $value['name'] : null,
					'#title'         => __( 'Guest Name', 'directories-pro' ),
					'#max_length'    => 255,
					'#weight'        => 2,
				),
				'email' => array(
					'#type'            => 'email',
					'#title'           => __( 'E-mail Address', 'directories-pro' ),
					'#default_value'   => isset( $value['email'] ) ? $value['email'] : null,
					'#char_validation' => 'email',
					'#weight'          => 4,
				),
				'url'   => array(
					'#type'            => 'url',
					'#title'           => __( 'Website URL', 'directories-pro' ),
					'#default_value'   => isset( $value['url'] ) ? $value['url'] : null,
					'#char_validation' => 'url',
					'#weight'          => 6,
				),
			);
			if ( isset( $value['ip_hash'] )
				&& strlen( $value['ip_hash'] )
			) {
				$form['ip_hash'] = array(
					'#type'          => 'textfield',
					'#title'         => __( 'IP (hashed)', 'directories-pro' ),
					'#default_value' => $value['ip_hash'],
					// '#disabled' => true,
					'#weight'        => 8,
				);
			}
			if ( isset( $value['guid'] )
				&& strlen( $value['guid'] )
			) {
				$form['guid'] = array(
					'#type'          => 'textfield',
					'#title'         => __( 'GUID', 'directories-pro' ),
					'#default_value' => $value['guid'],
					'#disabled'      => true,
					'#weight'        => 10,
				);
			}

			return $form;
		} else {
			// Frontend
			if ( ! $this->_application->getUser()->isAnonymous() ) {
				return;
			}

			$form   = array();
			$config = $this->_application->getComponent( 'FrontendSubmit' )->getConfig( 'guest' );
			if ( ! empty( $config['collect_name'] )
				|| ! isset( $config['collect_name'] )
			) {
				$form['name'] = array(
					'#type'          => 'textfield',
					'#title'         => __( 'Your Name', 'directories-pro' ),
					'#weight'        => 1,
					'#default_value' => isset( $value['name'] ) ? $value['name'] : null,
					'#max_length'    => 255,
					'#required'      => ! empty( $config['require_name'] ),
				);
			}
			if ( ! empty( $config['collect_email'] ) ) {
				$form['email'] = array(
					'#type'          => 'email',
					'#title'         => __( 'E-mail Address', 'directories-pro' ),
					'#weight'        => 3,
					'#default_value' => isset( $value['email'] ) ? $value['email'] : ( ( $email = $this->_application->Dashboard_GuestAccess_isGranted() ) ? $email : null ),
					'#check_mx'      => ! empty( $config['check_mx'] ),
					'#check_exists'  => ! empty( $config['check_exists'] ),
					'#max_length'    => 255,
					'#required'      => ! empty( $config['require_email'] ),
				);
			}
			if ( ! empty( $config['collect_url'] ) ) {
				$form['url'] = array(
					'#type'          => 'url',
					'#title'         => __( 'Website URL', 'directories-pro' ),
					'#weight'        => 5,
					'#default_value' => isset( $value['url'] ) ? $value['url'] : null,
					'#max_length'    => 255,
					'#required'      => ! empty( $config['require_url'] ),
				);
			}

			return $form;
		}
	}
}
