<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Featured Product Toast Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class WC_Featured_Product_Toast_Editor extends WC_Featured_Product_Toast_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'], 'fab_setting_woocommerce_featured_product_toast_product', true );
        $product    = wc_get_product( $product_id );

        // Set featured product toast product field.
        $tabs['setting']['fields']['featured_product_toast_product'] = array(
            'type'       => 'select',
            'name'       => 'fab_setting_woocommerce_featured_product_toast_product',
            'label'      => __( 'Products', 'floating-awesome-button' ),
            'tooltip'    => __( 'Select product to show featured product toast', '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(
                'placeholder'        => __( 'Search products...', 'floating-awesome-button' ),
                'allowClear'         => true,
                'minimumInputLength' => 3,
                'ajax'               => array(
                    'url'      => admin_url( 'admin-ajax.php' ),
                    'action'   => 'fab_search_products',
                    'dataType' => 'json',
                    'delay'    => 250,
                    'cache'    => true,
                ),
            ),
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Hide button url, because the button url should be the product url.
        $tabs['toast']['fields']['url_button']['visible_if'] = array(
            array(
                'tab'      => 'setting',
                'field'    => 'type',
                'operator' => '!=',
                'value'    => $this->type,
            ),
        );

        return $tabs;
    }

    /**
     * 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_featured_product_toast_product' => array( 'default' => '' ),
        );

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

    /**
     * Filter template postmeta.
     *
     * @param array  $postmeta The postmeta.
     * @param string $type The type.
     * @param object $data The data.
     * @return array
     */
    public function filter_template_postmeta( $postmeta, $type, $data ) {
        if ( $this->type !== $type ) {
            return $postmeta;
        }

        $postmeta['fab_setting_toast']['window']            = isset( $data->settings->open_in_new_tab ) ? \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $data->settings->open_in_new_tab ) : 'no';
        $postmeta['fab_setting_toast']['remember_on_click'] = isset( $data->settings->remember_on_click ) ? \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $data->settings->remember_on_click ) : 'no';
        $postmeta['fab_trigger']['cookie']['expiration']    = isset( $data->cookie->expiration ) ? $data->cookie->expiration : 0;

        return $postmeta;
    }

    /**
     * Add FAB setting post meta for WooCommerce features.
     *
     * @param array $post_metas Existing post meta settings.
     * @return array Modified post meta settings with new WooCommerce meta.
     */
    public function add_fab_setting_post_metas( $post_metas ) {
        // Define new WooCommerce post meta settings.
        $woocommerce_post_metas = array(
            'woocommerce_featured_product_toast_product' => array(
                'meta_key' => 'fab_setting_woocommerce_featured_product_toast_product',
            ),
        );

        // Merge new post meta settings into the existing post metas.
        return array_merge( $post_metas, $woocommerce_post_metas );
    }

    /*
    |--------------------------------------------------------------------------
    | 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;
        }

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

        // Add FAB setting input.
        add_filter( 'fab_setting_input', array( __CLASS__, 'jit_add_fab_setting_input' ) );

        // Add FAB setting post metas.
        add_filter( 'fab_setting_post_metas', array( __CLASS__, 'jit_add_fab_setting_post_metas' ) );

        // Filter template postmeta.
        add_filter( 'fab_template_postmeta', array( __CLASS__, 'jit_filter_template_postmeta' ), 10, 3 );
    }
}
