<?php

namespace Fab\Controller;

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

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

/**
 * WooCommerce Apply Coupon Editor
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WC_Apply_Coupon_Editor extends WC_Apply_Coupon_Type implements JIT_Interface {

    /**
     * Add FAB setting inputs for WooCommerce features.
     *
     * @param array $input Existing input settings.
     * @return array Modified input settings with new WooCommerce settings.
     */
    public function add_fab_setting_input( $input ) {
        // Define new WooCommerce input settings.
        $woocommerce_settings = array(
            'fab_setting_woocommerce_apply_coupon' => array( 'default' => '' ),
        );

        // Merge new settings into the existing inputs.
        return array_merge( $input, $woocommerce_settings );
    }

    /**
     * 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 ) {
        $coupon_id = get_post_meta( $fab['ID'], $this->post_metas['woocommerce_apply_coupon']['meta_key'], true );

        // Set apply coupon field.
        $tabs['setting']['fields']['apply_coupon'] = array(
            'type'        => 'select',
            'name'        => 'fab_setting_woocommerce_apply_coupon',
            'label'       => __( 'Coupon', 'floating-awesome-button' ),
            'tooltip'     => __( 'Coupon to be applied on cart or checkout page.', 'floating-awesome-button' ),
            'placeholder' => __( '--choose--', 'floating-awesome-button' ),
            'value'       => $coupon_id ? $coupon_id : '',
            'options'     => $coupon_id ? array(
                array(
                    'id'   => $coupon_id,
                    'text' => sprintf( '%s #%s (%s)', get_the_title( $coupon_id ), $coupon_id, 'shop_coupon' ),
                ),
            ) : array(),
            'select2'     => array(
                'minimumInputLength' => 2,
                'ajax'               => array(
                    'url'            => admin_url( 'admin-ajax.php' ),
                    'action'         => 'fab_search_coupons',
                    '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() {
        // Prevent error if woocommerce plugin is not active.
        if ( ! FAB_WOOCOMMERCE_PLUGIN_ACTIVE ) {
            return;
        }

        // @backend - Add fab setting input
        add_filter( 'fab_setting_input', array( __CLASS__, 'jit_add_fab_setting_input' ), 10, 1 );

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