<?php
if (!defined('ABSPATH')) exit;

/**
 * Enqueue Vendor Frontend Assets
 */
add_action('wp_enqueue_scripts', function () {

    if (is_admin()) return;

    global $post;

    if (!isset($post->post_content)) return;

    $is_dashboard_page = has_shortcode($post->post_content, 'stakmv-vendor-dashboard');
    $is_create_page    = has_shortcode($post->post_content, 'stakmv-vendor-create');

    if (!$is_dashboard_page && !$is_create_page) {
        return;
    }

    wp_enqueue_style(
        'stakmv-vendor-ui',
        STAKMV_PLUGIN_URL . 'assets/css/vendor-dashboard.css',
        [],
        file_exists(STAKMV_PLUGIN_PATH . 'assets/css/vendor-dashboard.css')
            ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/css/vendor-dashboard.css')
            : '1.0.2'
    );

    if ($is_dashboard_page) {
        wp_enqueue_media();

        wp_enqueue_script(
            'stakmv-vendor-ui',
            STAKMV_PLUGIN_URL . 'assets/js/vendor-dashboard.js',
            [],
            file_exists(STAKMV_PLUGIN_PATH . 'assets/js/vendor-dashboard.js')
                ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/js/vendor-dashboard.js')
                : '1.0.2',
            false
        );

        $chart_data = array(
            'earnings' => array(),
            'bids'     => array(),
            'watchers' => array(1, 2, 3, 4, 6, 8, 12),
        );
        if (is_user_logged_in() && function_exists('stakmv_get_chart_data')) {
            $chart_data = stakmv_get_chart_data(get_current_user_id());
        }

        wp_localize_script('stakmv-vendor-ui', 'STAKMV_CHART_DATA', $chart_data);
    }

    if ($is_create_page) {
        wp_enqueue_script(
            'stakmv-create-listing',
            STAKMV_PLUGIN_URL . 'assets/js/vendor-create-listing.js',
            ['jquery'],
            file_exists(STAKMV_PLUGIN_PATH . 'assets/js/vendor-create-listing.js')
                ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/js/vendor-create-listing.js')
                : '1.0.0',
            false
        );
        wp_localize_script('stakmv-create-listing', 'STAKMV_CREATE', [
            'ajax'  => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('stakmv_cl_nonce'),
        ]);
    }
});
add_action('wp_enqueue_scripts', function () {

    if (!is_page(stakmv_get_option('create_listing_page'))) {
        return;
    }

    wp_enqueue_script(
        'sortablejs',
        STAKMV_PLUGIN_URL . 'assets/js/Sortable.min.js',
        [],
        file_exists(STAKMV_PLUGIN_PATH . 'assets/js/Sortable.min.js')
            ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/js/Sortable.min.js')
            : '1.15.7',
        false
    );

    wp_enqueue_script(
        'cropperjs',
        STAKMV_PLUGIN_URL . 'assets/vendor/cropperjs/cropper.min.js',
        [],
        file_exists(STAKMV_PLUGIN_PATH . 'assets/vendor/cropperjs/cropper.min.js')
            ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/vendor/cropperjs/cropper.min.js')
            : '2.1.1',
        false
    );
});
