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

use SabaiApps\Directories\Context;
use SabaiApps\Directories\Component\Form;

class VoteEntityForm extends Form\Controller {

	protected function _doGetFormSettings( Context $context, array &$storage ) {
		$this->_ajaxOnSuccess         = 'function (result, target, trigger) {
    if (result.messages) DRTS.flash(result.messages, "success");
}';
		$this->_ajaxOnSuccessRedirect = $this->_ajaxOnErrorRedirect = false;

		$votes        = $this->Voting_Votes_current( $context->entity->getBundleName(), $context->entity->getId(), 'voting_' . $context->voting_type );
		$current_vote = $votes[''] ?? null;

		$type_impl              = $this->Voting_Types_impl( $context->voting_type );
		$this->_submitButtons[] = array(
			'#btn_label' => sprintf( $type_impl->votingTypeInfo( 'label_action' ), $context->bundle->getLabel( 'singular' ) ),
			'#btn_color' => 'primary',
			'#btn_size'  => 'lg',
		);

		// Check voting allowed
		if ( ! $this->Voting_CanVote( $context->entity, $context->voting_type ) ) {
			if ( $this->getUser()->isAnonymous() ) {
				$context->setUnauthorizedError( $this->Entity_PermalinkUrl( $context->entity ) );
			} else {
				$context->setError( __( 'You do not have the permission to perform this action.', 'directories-pro' ) );
			}
			return;
		}

		$form = array(
			'entity_id'   => array(
				'#type'  => 'hidden',
				'#value' => $context->entity->getId(),
			),
			'voting_type' => array(
				'#type'  => 'hidden',
				'#value' => $context->voting_type,
			),
		);
		if ( $type_impl->votingTypeInfo( 'allow_comment' ) ) {
			$form['comment'] = array(
				'#type'          => 'textarea',
				'#title'         => __( 'Comment', 'directories-pro' ),
				'#required'      => true,
				'#weight'        => 5,
				'#default_value' => $current_vote ? $current_vote->comment : null,
				'#max_length'    => 255,
				'#rows'          => 2,
				'#disabled'      => isset( $current_vote ) && $current_vote->marked,
			);
		}
		if ( ! $context->getRequest()->isAjax() ) {
			$form['entity'] = array(
				'#type'   => 'item',
				'#title'  => $context->bundle->getLabel( 'singular' ),
				'#markup' => $this->Entity_Permalink( $context->entity ),
				'#weight' => -1,
			);
		}
		if ( isset( $current_vote ) ) {
			if ( $current_vote->marked ) {
				$this->_submitable = false;
				$form['#header'][] = array(
					'level'   => 'warning',
					'message' => __( 'Your submission has already been reviewed, so it can no longer be edited.', 'directories-pro' ),
				);
			} elseif ( $type_impl->votingTypeInfo( 'allow_comment' ) ) {
					$form['#header'][] = array(
						'level'   => 'info',
						'message' => __( 'You have already submitted your vote. You can edit your submission until it is reviewed.', 'directories-pro' ),
					);
			}
		}
		return $type_impl->votingTypeOnVoteEntityForm( $form, $context->entity, $current_vote );
	}

	public function submitForm( Form\Form $form, Context $context ) {
		$type_impl = $this->Voting_Types_impl( $context->voting_type );
		$this->Voting_Votes_cast(
			$context->entity,
			'voting_' . $context->voting_type,
			array( '' => ( null === $value = $context->getRequest()->get( 'value' ) ) ? 1 : $value ),
			$type_impl->votingTypeInfo( 'allow_comment' ) ? array( 'comment' => $form->values['comment'] ?? '' ) : array()
		);

		$context->setSuccess( $context->getRequest()->isAjax() ? false : $this->Entity_PermalinkUrl( $context->entity ) ) // set false to send back message when ajax
			->addFlash( __( 'Thank you for your submission.', 'directories-pro' ), 'success' );
	}
}
