<?php

namespace Fab\Controller;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Metabox\FABMetaboxSetting;
use Fab\Interfaces\JIT_Interface;

/**
 * Search Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Search_Editor extends Search_Type implements JIT_Interface {

    /**
     * Save Metabox Data.
     *
     * @param int    $post_id Post ID.
     * @param object $post Post object.
     * @param bool   $update Whether the post is being updated.
     * @return void
     */
    public function save_metabox_data( $post_id, $post, $update ) {
        $params = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
        $type   = isset( $params['fab_setting_type'] ) ? sanitize_text_field( wp_unslash( $params['fab_setting_type'] ) ) : '';

        // If type is not post recommendation, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save post search label.
        $post_meta = $this->post_meta['label'];
        $label     = isset( $params['fab_setting_search_label'] ) ? $params['fab_setting_search_label'] : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $label );

        // Save post search pagination enable.
        $post_meta         = $this->post_meta['pagination_enable'];
        $pagination_enable = isset( $params['fab_setting_search_pagination_enable'] ) ? $params['fab_setting_search_pagination_enable'] : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $pagination_enable ) );

        // Save post search pagination per page.
        $post_meta           = $this->post_meta['pagination_per_page'];
        $pagination_per_page = isset( $params['fab_setting_search_pagination_per_page'] ) ? intval( $params['fab_setting_search_pagination_per_page'] ) : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $pagination_per_page );
    }

    /**
     * Add fab metabox setting tabs fields.
     *
     * @param array $tabs The tabs.
     * @param array $fab The fab.
     * @return array
     */
    public function add_fab_metabox_setting_tabs_fields( $tabs, $fab ) {
        // Set label field.
        $tabs['setting']['fields']['search_label'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_search_label',
            'label'      => __( 'Search Label', 'floating-awesome-button' ),
            'tooltip'    => __( 'This label will be displayed in the search field popup', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['search']['label'] ) ?
                $fab['extraOptions']['search']['label'] : $this->post_meta['label']['default'],
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set pagination heading.
        $tabs['setting']['fields']['search_pagination_heading'] = array(
            'type'       => 'heading',
            'label'      => __( 'Pagination', 'floating-awesome-button' ),
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set pagination enable field.
        $tabs['setting']['fields']['search_pagination_enable'] = array(
            'type'       => 'switch',
            'name'       => 'fab_setting_search_pagination_enable',
            'label'      => __( 'Enable Pagination', 'floating-awesome-button' ),
            'tooltip'    => __( 'Enable this option to paginate search results in the popup.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['search']['pagination_enable'] ) ?
                $fab['extraOptions']['search']['pagination_enable'] : $this->post_meta['pagination_enable']['default'],
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set pagination per page field.
        $tabs['setting']['fields']['search_pagination_per_page'] = array(
            'type'       => 'number',
            'name'       => 'fab_setting_search_pagination_per_page',
            'label'      => __( 'Per Page', 'floating-awesome-button' ),
            'tooltip'    => __( 'Set how many search results are displayed per page in the popup.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['search']['pagination_per_page'] ) ?
                $fab['extraOptions']['search']['pagination_per_page'] : $this->post_meta['pagination_per_page']['default'],
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        return $tabs;
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Save Metabox Data.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_save_metabox_data' ), 10, 3 );

        // Add fab metabox setting tabs fields.
        add_filter( 'fab_metabox_setting_tabs', array( __CLASS__, 'jit_add_fab_metabox_setting_tabs_fields' ), 11, 2 );
    }
}
