<?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 function HulkPlugins\Woo\ProductConditionManager\hulk_woo_pcm_fs;
use const HulkPlugins\Woo\ProductConditionManager\DOCUMENTATION;
use const HulkPlugins\Woo\ProductConditionManager\PLUGIN_DIR_PATH;
use const HulkPlugins\Woo\ProductConditionManager\PLUGIN_DIR_URL;
defined( 'ABSPATH' ) || exit;
class DokanController {
    use SingletonTrait;
    private ProductModel $model;

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

    public function init_hooks() : void {
        add_action(
            'dokan_product_edit_after_main',
            [$this, 'product_edit_after_main'],
            10,
            2
        );
        add_action( 'dokan_process_product_meta', [$this, 'save_product_meta'] );
    }

    public function render_template( $post, $post_id ) : void {
        ?>
		<div class="dokan-product-condition-options dokan-edit-row dokan-clearfix">
			<div class="dokan-section-heading" data-togglehandler="dokan_product_condition_options">
				<h2><i class="fas fa-cog" aria-hidden="true"></i> <?php 
        esc_html_e( 'Product Condition', 'product-condition-manager-for-woocommerce' );
        ?></h2>
				<a href="#" class="dokan-section-toggle">
					<i class="fas fa-sort-down fa-flip-vertical" aria-hidden="true"></i>
				</a>
				<div class="dokan-clearfix"></div>
			</div>
			<div class="dokan-section-content">
				<div id="hulk_woo_pcm_settings"></div>
				<div class="dokan-clearfix"></div>
			</div>
		</div>
		<?php 
        $settings_assets = (require PLUGIN_DIR_PATH . 'build/admin/settings/index.asset.php');
        wp_enqueue_style(
            'hulk_woo_pcm_settings',
            PLUGIN_DIR_URL . 'build/admin/settings/index.css',
            ['wp-components'],
            $settings_assets['version']
        );
        wp_enqueue_script(
            'hulk_woo_pcm_settings',
            PLUGIN_DIR_URL . 'build/admin/settings/index.js',
            $settings_assets['dependencies'],
            $settings_assets['version'],
            true
        );
        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(),
            ],
        ] );
    }

    public function product_edit_after_main( $post, $post_id ) : void {
        $this->render_template( $post, $post_id );
    }

    public function save_product_meta( $post_id ) : void {
        check_admin_referer( 'dokan_edit_product_nonce', '_dokan_edit_product_nonce' );
    }

}
