<?php

namespace Fab\Controller;

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

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

/**
 * Featured Product Editor.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WC_Featured_Product_Editor extends WC_Featured_Product_Type implements JIT_Interface {

    /**
     * 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 ) {
        $product_id = get_post_meta( $fab['ID'], FABMetaboxSetting::get_post_metas()['woocommerce_featured_product']['meta_key'], true );
        $product    = wc_get_product( $product_id );

        // Set featured product field.
        $tabs['setting']['fields']['featured_product'] = array(
            'type'        => 'select',
            'name'        => 'fab_setting_woocommerce_featured_product',
            'label'       => __( 'Featured Product', 'floating-awesome-button' ),
            'tooltip'     => __( 'Select a product to feature', 'floating-awesome-button' ),
            'placeholder' => __( 'Search for a product...', 'floating-awesome-button' ),
            'value'       => $product ? $product->get_id() : '',
            'options'     => $product ? array(
                array(
                    'id'   => $product->get_id(),
                    'text' => sprintf( '%s #%s (%s)', $product->get_name(), $product->get_id(), 'product' ),
                ),
            ) : array(),
            'select2'     => array(
                'allowClear'         => false,
                'minimumInputLength' => 3,
                'ajax'               => array(
                    'url'            => admin_url( 'admin-ajax.php' ),
                    'action'         => 'fab_search_products',
                    'dataType'       => 'json',
                    'delay'          => 250,
                    'processResults' => true,
                    'cache'          => 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() {

        /**
         * |--------------------------------------------------------------------------
         * | WOOCOMMERCE HOOKS
         * | - Prevent error if woocommerce plugin is not active.
         * |--------------------------------------------------------------------------
         */
        if ( ! FAB_WOOCOMMERCE_PLUGIN_ACTIVE ) {
            return;
        }

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