<?php

namespace Simple_Giveaways;

/**
 * Class SG_Action_Visit
 */
class SG_Action_Comment extends SG_Extra_Action {

	protected $has_action_button = false;

	/**
	 * @return false
	 */
	public function needs_validation() {
		return false;
	}

	/**
	 * SG_Action_Visit constructor.
	 */
	public function __construct() {
		$this->slug        = 'comment';
		$this->category    = 'general';
		$this->title       = __( 'Post a Comment', 'giveasap' );
		$this->description = __( 'Rewards the user for commenting on an article.', 'giveasap' );
		$this->icon        = 'fal fa-comment';
	}

	/**
	 * Define the extra fields.
	 *
	 * @return array
	 */
	public function get_fields() {
		$this->fields = array(
			'url'         => array(
				'type'     => 'url',
				'label'    => __( 'Article URL', 'giveasap' ),
				'id'       => 'url',
				'required' => true,
			),
			'description' => array(
				'type'     => 'text',
				'label'    => __( 'Description', 'giveasap' ),
				'id'       => 'description',
				'required' => true,
			),
		);

		return parent::get_fields();
	}

	/**
	 * @param array $action
	 *
	 * @return array|\WP_Error
	 */
	public function complete_action( $action, $data = array() ) {

		if ( ! isset( $data['comment_url'] ) ) {
			return new \WP_Error( 'no-url', __( 'Please enter your comment url', 'giveasap' ) );
		}

		$comment_url = sanitize_text_field( $data['comment_url'] );

		if ( ! $comment_url ) {
			return new \WP_Error( 'no-answer', __( 'Please enter your comment url', 'giveasap' ) );
		}

		if ( strpos( $comment_url, 'http' ) !== 0 ) {
			return new \WP_Error( 'no-answer', __( 'Please enter your comment url', 'giveasap' ) );
		}

		// Let's take the correct URL with HTTP.
		parse_str( wp_unslash( $_REQUEST['data'] ), $comment_data );

		return array( 'comment_url' => esc_url_raw( $comment_data['comment_url'] ) );
	}

	/**
	 * This is the value that we will show on the Entries table.
	 */
	public function get_value_for_entries_table( $data ) {

		if ( isset( $data['comment_url'] ) ) {
			return '<strong>' . __( 'Comment:', 'giveasap' ) . '</strong> <a target="_blank" href="' . esc_url( $data['comment_url'] ) . '">' . esc_html( $data['comment_url'] ) . '</a>';
		}

		return '-';
	}

	/**
	 * GForm Public
	 */
	public function form_public_content() {
		$data  = $this->get_data();
		$admin = $this->get_admin_data();
		$id    = 'instance_' . esc_attr( $admin['instance_id'] ) . '_code';

		if ( isset( $admin['description'] ) && $admin['description'] ) {
			?>
			<p><?php echo esc_html( $admin['description'] ); ?></p>
			<?php
		}

		echo '<p>' . esc_html__( 'Article: ', 'giveasap' ) . '<a target="_blank" href="' . esc_url( $admin['url'] ) . '">' . esc_html( $admin['url'] ) . '</a></p>';

		if ( $this->is_completed() ) {
			if ( ! empty( $data['data'] ) && ! empty( $data['data']['invent_own_answer'] ) ) {
				echo '<em>' . wp_kses_post( $data['data']['invent_own_answer'] ) . '</em>';
			}
			return;
		}

		?>

		<input type="url" id="<?php echo esc_attr( $id ); ?>" name="comment_url" value="" placeholder="<?php esc_attr_e( 'Comment Link here', 'giveasap' ); ?>" />
		<?php

	}
}
