<?php

namespace HulkPlugins\Woo\ProductConditionManager\Controllers;

use HulkPlugins\Woo\ProductConditionManager\Models\ProductModel;
use HulkPlugins\Woo\ProductConditionManager\Models\SettingsModel;
use HulkPlugins\Woo\ProductConditionManager\Traits\SingletonTrait;
use HulkPlugins\Woo\ProductConditionManager\Utils\Helper;
use WC_Product;
use WP_Post;
use function HulkPlugins\Woo\ProductConditionManager\hulk_woo_pcm_fs;
use const HulkPlugins\Woo\ProductConditionManager\DOCUMENTATION;
use const HulkPlugins\Woo\ProductConditionManager\PLUGIN_DIR_URL;
defined( 'ABSPATH' ) || exit;
class ProductController {
    use SingletonTrait;
    private array $cache = [];

    private ProductModel $model;

    public function __construct() {
        $this->model = ProductModel::getInstance();
    }

    public function init_hooks() : void {
        add_filter( 'woocommerce_product_data_tabs', [$this, 'product_data_tabs'] );
        add_action( 'woocommerce_product_data_panels', [$this, 'product_data_panels'] );
        add_action( 'woocommerce_process_product_meta', [$this, 'save_product_meta'] );
        add_action( 'woocommerce_product_quick_edit_end', [$this, 'product_quick_bulk_edit_end'] );
        add_action( 'woocommerce_product_bulk_edit_end', [$this, 'product_quick_bulk_edit_end'] );
        add_action( 'woocommerce_product_quick_edit_save', [$this, 'product_quick_bulk_edit_save'] );
        add_action( 'woocommerce_product_bulk_edit_save', [$this, 'product_quick_bulk_edit_save'] );
        add_action( 'add_inline_data', [$this, 'add_inline_data'] );
    }

    /**
     * Add a "Product Condition" tab to the single product WooCommerce options.
     * @param $tabs
     *
     * @return mixed
     */
    public function product_data_tabs( $tabs ) {
        $tabs['hulk_woo_pcm_tab'] = [
            'label'  => esc_attr__( 'Product Condition', 'product-condition-manager-for-woocommerce' ),
            'target' => 'hulk_woo_pcm_target',
            'class'  => 'hulk_woo_pcm_tab',
        ];
        return $tabs;
    }

    /**
     * Add Product Condition options to the "Product Condition" product tab on the single pages.
     * @return void
     */
    public function product_data_panels() : void {
        global $post;
        ?>
		<div id="hulk_woo_pcm_target" class="panel woocommerce_options_panel">
			<!-- React app -->
			<div id="hulk_woo_pcm_settings"></div>
		</div>
		<?php 
        wp_enqueue_style( 'hulk_woo_pcm_settings' );
        wp_enqueue_script( 'hulk_woo_pcm_settings' );
        wp_localize_script( 'hulk_woo_pcm_settings', 'hulk_woo_pcm_settings', [
            'DOCUMENTATION'  => DOCUMENTATION,
            'PLUGIN_DIR_URL' => PLUGIN_DIR_URL,
            'appName'        => 'ProductDataMetaSettings',
            'conditions'     => Helper::getDefaultConditions(),
            'settings'       => $this->model->getAllMeta( $post->ID ),
            'globalSettings' => SettingsModel::getInstance()->getSettings(),
            'freemius'       => [
                'is_premium'           => hulk_woo_pcm_fs()->is_premium(),
                'is_not_paying'        => hulk_woo_pcm_fs()->is_not_paying(),
                'checkout_url'         => hulk_woo_pcm_fs()->checkout_url(),
                'get_upgrade_url'      => hulk_woo_pcm_fs()->get_upgrade_url(),
                'can_use_premium_code' => hulk_woo_pcm_fs()->can_use_premium_code(),
            ],
        ] );
    }

    /**
     * Saves Product Condition options when a product is saved.
     * @param $post_id
     *
     * @return void
     */
    public function save_product_meta( $post_id ) : void {
        check_admin_referer( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
    }

    /**
     * Add Product Condition options to quick and bulk edits
     * @return void
     */
    public function product_quick_bulk_edit_end() : void {
        echo '<div id="hulk_woo_pcm_settings"></div>';
        if ( isset( $this->cache['quickEditRunOnce'] ) ) {
            return;
        }
        $this->cache['quickEditRunOnce'] = true;
        wp_enqueue_style( 'hulk_woo_pcm_settings' );
        wp_enqueue_script( 'hulk_woo_pcm_settings' );
        wp_localize_script( 'hulk_woo_pcm_settings', 'hulk_woo_pcm_settings', [
            'appName'        => 'ProductQuickEditMetaSettings',
            'conditions'     => Helper::getDefaultConditions(),
            'globalSettings' => SettingsModel::getInstance()->getSettings(),
            'PLUGIN_DIR_URL' => PLUGIN_DIR_URL,
            'freemius'       => [
                'is_premium'           => hulk_woo_pcm_fs()->is_premium(),
                'is_not_paying'        => hulk_woo_pcm_fs()->is_not_paying(),
                'checkout_url'         => hulk_woo_pcm_fs()->checkout_url(),
                'get_upgrade_url'      => hulk_woo_pcm_fs()->get_upgrade_url(),
                'can_use_premium_code' => hulk_woo_pcm_fs()->can_use_premium_code(),
            ],
        ] );
    }

    /**
     * Fires after outputting the fields for the inline editor for posts and pages.
     *
     * @param $post
     *
     * @return void
     */
    public function add_inline_data( $post ) : void {
        if ( $post->post_type !== 'product' ) {
            return;
        }
        $settings = $this->model->getAllMeta( $post->ID );
        echo '<div class="hulk_woo_pcm_inline_data"';
        foreach ( $settings as $key => $value ) {
            echo 'data-' . esc_attr( $key ) . '="' . esc_html( $value ) . '"';
        }
        echo '></div>';
    }

    /**
     * Quick and multiple product edits save
     *
     * @param $product
     *
     * @return void
     */
    public function product_quick_bulk_edit_save( $product ) : void {
        // Nonce verification
        if ( !isset( $_REQUEST['woocommerce_quick_edit_nonce'] ) ) {
            return;
        }
        $nonce = sanitize_text_field( wp_unslash( $_REQUEST['woocommerce_quick_edit_nonce'] ) );
        if ( !wp_verify_nonce( $nonce, 'woocommerce_quick_edit_nonce' ) ) {
            return;
        }
    }

}
