<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Plugin hooks in a backend
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WooCommerce extends Base implements JIT_Interface {

    /**
     * Autoload integration
     *
     * @return void
     */
    public function autoload_integration() {
        \FAB()->Helper->autoload_integration( FAB_WOOCOMMERCE_PLUGIN_FILE );
    }

    /**
     * Add integration to FAB
     *
     * @param array $integrations The integrations.
     * @return array The integrations.
     */
    public function add_integration( $integrations ) {
        $integrations['woocommerce'] = array(
            'name'        => __( 'WooCommerce', 'floating-awesome-button' ),
            'description' => __( 'WooCommerce is a powerful and flexible eCommerce plugin for WordPress.', 'floating-awesome-button' ),
            'url'         => 'https://wordpress.org/plugins/woocommerce/',
            'icon_url'    => 'https://ps.w.org/woocommerce/assets/icon.svg',
            'plugin_file' => FAB_WOOCOMMERCE_PLUGIN_FILE,
            'status'      => \Fab\Plugin\Helper::getInstance()->check_plugin_integration( FAB_WOOCOMMERCE_PLUGIN_FILE ),
        );
        return $integrations;
    }

    /**
     * Add WooCommerce placeholders.
     *
     * @param array    $placeholders The placeholders.
     * @param int|null $product_id The product ID.
     *
     * @return array The placeholders.
     */
    public function add_fab_placeholders( $placeholders, $product_id = null ) {
        if ( ! $product_id ) {
            global $post;
            if ( ! $post || ! isset( $post->ID ) ) {
                return $placeholders;
            }
            $product_id = $post->ID;
        }

        $product = wc_get_product( $product_id );
        if ( ! $product ) {
            return $placeholders;
        }

        // Add WooCommerce placeholders.
        $placeholders['{product_id}']              = $product->get_id();
        $placeholders['{product_title}']           = $product->get_title();
        $placeholders['{product_slug}']            = $product->get_slug();
        $placeholders['{product_status}']          = $product->get_status();
        $placeholders['{product_date}']            = $product->get_date_created();
        $placeholders['{product_type}']            = $product->get_type();
        $placeholders['{product_url}']             = $product->get_permalink();
        $placeholders['{product_excerpt}']         = $product->get_short_description();
        $placeholders['{product_content}']         = $product->get_description();
        $placeholders['{product_price}']           = $product->get_price();
        $placeholders['{product_quantity}']        = $product->get_stock_quantity();
        $placeholders['{product_sale_start_date}'] = $product->get_date_on_sale_from() ? $product->get_date_on_sale_from()->date( 'Y-m-d H:i:s' ) : '';
        $placeholders['{product_sale_end_date}']   = $product->get_date_on_sale_to() ? $product->get_date_on_sale_to()->date( 'Y-m-d H:i:s' ) : '';

        // Calculate remaining days if sale end date exists.
        $placeholders['{product_sale_remaining_days}'] = '';
        if ( $product->get_date_on_sale_to() ) {
            $now      = new \DateTime();
            $end_date = new \DateTime( $product->get_date_on_sale_to()->date( 'Y-m-d H:i:s' ) );
            $interval = $now->diff( $end_date );

            if ( 0 === $interval->invert ) { // Only if sale hasn't ended.
                $placeholders['{product_sale_remaining_days}'] = $interval->days;
            }
        }
        $placeholders['{product_sku}']        = $product->get_sku();
        $placeholders['{product_stock}']      = $product->get_stock_status();
        $placeholders['{product_categories}'] = wp_strip_all_tags( wc_get_product_category_list( $product->get_id() ) );
        $placeholders['{product_tags}']       = wp_strip_all_tags( wc_get_product_tag_list( $product->get_id() ) );
        $placeholders['{product_image_url}']  = wp_get_attachment_url( $product->get_image_id() );

        return $placeholders;
    }

    /**
     * Add WooCommerce placeholders description.
     *
     * @param array $placeholders Placeholders.
     * @return array Placeholders.
     */
    public function add_fab_placeholders_description( $placeholders ) {
        $placeholders['{product_id}']                  = __( 'Product ID', 'floating-awesome-button' );
        $placeholders['{product_title}']               = __( 'Product title', 'floating-awesome-button' );
        $placeholders['{product_slug}']                = __( 'Product slug', 'floating-awesome-button' );
        $placeholders['{product_status}']              = __( 'Product status', 'floating-awesome-button' );
        $placeholders['{product_date}']                = __( 'Product creation date', 'floating-awesome-button' );
        $placeholders['{product_type}']                = __( 'Product type', 'floating-awesome-button' );
        $placeholders['{product_url}']                 = __( 'Product URL', 'floating-awesome-button' );
        $placeholders['{product_excerpt}']             = __( 'Product short description', 'floating-awesome-button' );
        $placeholders['{product_content}']             = __( 'Product full description', 'floating-awesome-button' );
        $placeholders['{product_price}']               = __( 'Product price', 'floating-awesome-button' );
        $placeholders['{product_quantity}']            = __( 'Product quantity in stock', 'floating-awesome-button' );
        $placeholders['{product_sale_start_date}']     = __( 'Product sale start date', 'floating-awesome-button' );
        $placeholders['{product_sale_end_date}']       = __( 'Product sale end date', 'floating-awesome-button' );
        $placeholders['{product_sale_remaining_days}'] = __( 'Number of days remaining in the product sale', 'floating-awesome-button' );
        $placeholders['{product_sku}']                 = __( 'Product SKU', 'floating-awesome-button' );
        $placeholders['{product_stock}']               = __( 'Product stock status', 'floating-awesome-button' );
        $placeholders['{product_categories}']          = __( 'Product categories', 'floating-awesome-button' );
        $placeholders['{product_tags}']                = __( 'Product tags', 'floating-awesome-button' );
        $placeholders['{product_image_url}']           = __( 'Product image URL', 'floating-awesome-button' );

        return $placeholders;
    }

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

    /**
     * JIT autoload integration method.
     *
     * @return void
     */
    public static function jit_autoload_integration() {
        $instance = new self();
        $instance->autoload_integration();
    }

    /**
     * JIT add integration method.
     *
     * @param array $integrations The integrations.
     * @return array The integrations.
     */
    public static function jit_add_integration( $integrations ) {
        $instance = new self();
        return $instance->add_integration( $integrations );
    }

    /**
     * JIT add placeholders method.
     *
     * @param array    $placeholders The placeholders.
     * @param int|null $product_id   The product ID.
     * @return array The placeholders.
     */
    public static function jit_add_fab_placeholders( $placeholders, $product_id = null ) {
        $instance = new self();
        return $instance->add_fab_placeholders( $placeholders, $product_id );
    }

    /**
     * JIT add placeholders description method.
     *
     * @param array $placeholders The placeholders.
     * @return array The placeholders.
     */
    public static function jit_add_fab_placeholders_description( $placeholders ) {
        $instance = new self();
        return $instance->add_fab_placeholders_description( $placeholders );
    }

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add integration to FAB.
        add_filter( 'fab_plugin_integrations', array( __CLASS__, 'jit_add_integration' ) );

        // Prevent error if plugin is not active.
        if ( ! FAB_WOOCOMMERCE_PLUGIN_ACTIVE ) {
            return;
        }

        // Autoload integration.
        add_action( 'admin_init', array( __CLASS__, 'jit_autoload_integration' ) );

        // Add placeholder filters.
        add_filter( 'fab_placeholders', array( __CLASS__, 'jit_add_fab_placeholders' ), 10, 2 );

        // Explain placeholders.
        add_filter( 'fab_placeholders_description', array( __CLASS__, 'jit_add_fab_placeholders_description' ), 10, 1 );
    }
}
