<?php

defined('ABSPATH') || die('No direct access.');

add_action('admin_enqueue_scripts', function() {
    $referrer = null;
    if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
        $unslashed_referrer = wp_unslash( $_SERVER['HTTP_REFERER'] );
        $referrer = esc_url_raw( $unslashed_referrer );
    }
    $parsed   = $referrer ? wp_parse_url($referrer) : null;
    $queryParams = [];
    if ($parsed && isset($parsed['query'])) {
        parse_str($parsed['query'], $queryParams);
    }
    $backPage = $queryParams['page'] ?? null;

    $current_page = isset($_GET['page']) ? sanitize_key( wp_unslash($_GET['page']) ) : '';
    if ( strpos($current_page, 'swpanel_ai') === 0 ) {
        wp_enqueue_style(
            'google-fonts',
            'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400&display=swap',
            array(),
            '1.0',
        );
        wp_enqueue_style(
            'swpanel_ai_style',
            plugin_dir_url(__FILE__) . '../admin/assets/css/index.css',
            array(),
            '1.0'
        );
        wp_enqueue_style(
            'swpanel_ai_style_awesome',
            'https://static.swpanel.com/fonts/font-awesome-2021/css/all.min.css',
            array(),
            '1.0'
        );
        wp_enqueue_script(
            'swpanel_ai_creating_charts_core',
            'https://static.swpanel.com/charts/FusionCharts-2021/Powercharts/js/fusioncharts.js',
            array(),
            '1.0',
            true
        );
        wp_enqueue_script(
            'swpanel_ai_creating_charts',
            'https://static.swpanel.com/charts/FusionCharts-2021/Powercharts/js/fusioncharts.charts.js',
            array('swpanel_ai_creating_charts_core'),
            '1.0',
            true
        );
    }
    if ( $current_page === 'swpanel_ai_style' ) {
        wp_enqueue_style('wp-block-library');
        wp_enqueue_style('global-styles');
        wp_enqueue_script(
            'swpanel_ai_style',
            plugin_dir_url(__FILE__) . '../admin/assets/js/style-page.js',
            ['wp-i18n','react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'swpanel_ai_creating_charts'],
            '1.0',
            true
        );
        wp_set_script_translations('swpanel_ai_style', 'swpanel-ai', plugin_dir_path(__FILE__) . '../languages');
        wp_localize_script( 'swpanel_ai_style', 'swpanel_ai_data', array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'run_headers_task' => get_option('swpanel_ai_run_headers_task', 0),
            'pluginUrl' => plugin_dir_url(__FILE__),
            'back_page' => $backPage,
            'retry_text' => __('Retry', 'swpanel-ai')
        ));
    }
    if ( $current_page === 'swpanel_ai_structure' ) {
        wp_enqueue_script(
            'swpanel_ai_structure',
            plugin_dir_url( __FILE__ ) . '../admin/assets/js/structure-page.js',
            array('wp-i18n','jquery', 'swpanel_ai_creating_charts'),
            '1.0',
            true
        );
        wp_set_script_translations('swpanel_ai_structure', 'swpanel-ai', plugin_dir_path(__FILE__) . '../languages');
        wp_localize_script( 'swpanel_ai_structure', 'swpanel_ai_data', array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'pluginUrl' => plugin_dir_url(__FILE__),
            'run_description_keywords_task' => get_option('swpanel_ai_run_description_keywords_task', 0),
            'back_page' => $backPage,
            'retry_text' => __('Retry', 'swpanel-ai')
        ));
    }
    if ( $current_page === 'swpanel_ai_template' ) {
        wp_enqueue_script(
            'swpanel_ai_template',
            plugin_dir_url( __FILE__ ) . '../admin/assets/js/template-page.js',
            array('wp-i18n','jquery', 'swpanel_ai_creating_charts'),
            '1.0',
            true
        );
        wp_set_script_translations('swpanel_ai_template', 'swpanel-ai', plugin_dir_path(__FILE__) . '../languages');
        wp_localize_script( 'swpanel_ai_template', 'swpanel_ai_data', array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'pluginUrl' => plugin_dir_url(__FILE__),
            'retry_text' => __('Retry', 'swpanel-ai')
        ));
    }
    if ( $current_page === 'swpanel_ai_creating' ) {
        wp_enqueue_script(
            'swpanel_ai_creating',
            plugin_dir_url( __FILE__ ) . '../admin/assets/js/creating-page.js',
            array('wp-i18n','jquery', 'wp-api-fetch', 'swpanel_ai_creating_charts'),
            '1.0',
            true
        );
        wp_set_script_translations('swpanel_ai_creating', 'swpanel-ai', plugin_dir_path(__FILE__) . '../languages');
        wp_localize_script( 'swpanel_ai_creating', 'swpanel_ai_data', array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'admin_redirect_url' => home_url(),
            'run_creating_task' => get_option('swpanel_ai_run_creating_task', 0),
            'background_task' => get_transient('swpanel_ai_background_task'),
            'pluginUrl' => plugin_dir_url(__FILE__),
            'back_page' => $backPage,
            'retry_text' => __('Retry', 'swpanel-ai')
        ));
    }
    if ( $current_page === 'swpanel_ai_concept' ) {
        wp_enqueue_script(
            'swpanel_ai_concept',
            plugin_dir_url( __FILE__ ) . '../admin/assets/js/concept-page.js',
            array(),
            '1.0',
            true
        );
        wp_localize_script( 'swpanel_ai_concept', 'swpanel_ai_data', array(
            'pluginUrl' => plugin_dir_url(__FILE__)
        ));
    }
    if ( $current_page === 'swpanel_ai' ) {
        wp_enqueue_script(
            'swpanel_ai_admin-menu',
            plugin_dir_url( __FILE__ ) . '../admin/assets/js/admin-menu.js',
            [],
            '1.0',
            true
        );
        wp_localize_script( 'swpanel_ai_admin-menu', 'swpanel_ai_data', array(
            'pluginUrl' => plugin_dir_url(__FILE__)
        ));
    }
    if ( $current_page === 'swpanel_ai_token' ) {
        wp_enqueue_script(
            'swpanel_ai_token',
            plugin_dir_url( __FILE__ ) . '../admin/assets/js/token-form.js',
            [],
            '1.0',
            true
        );
        wp_localize_script( 'swpanel_ai_token', 'swpanel_ai_data', array(
            'pluginUrl' => plugin_dir_url(__FILE__)
        ));
    }
});

add_action('enqueue_block_editor_assets', function() {
    if (\SWPanel\Auth::validate()){
        wp_enqueue_script(
            'swpanel_ai_store',
            plugin_dir_url(__FILE__) . '../admin/assets/js/swpanel-store.js',
            ['react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives'],
            '1.0',
            true
        );
        wp_enqueue_script(
            'swpanel_ai_editor',
            plugin_dir_url(__FILE__) . '../admin/assets/js/gutenberg-editor.js',
            ['react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives'],
            '1.0',
            true
        );
        wp_enqueue_style(
            'swpanel_ai_editor-styles',
            plugin_dir_url(__FILE__) . '../admin/assets/css/gutenberg-editor.css',
            [],
            '1.0'
        );
        wp_set_script_translations('swpanel_ai_editor', 'swpanel-ai', plugin_dir_path(__FILE__) . '../languages');
        wp_localize_script('swpanel_ai_editor', 'swpanelData', array(
            'bearerToken' => get_option('swpanel_ai_license_token'),
            'apiUrl' => get_option('swpanel_ai_url'),
            'language' => get_locale(),
            'pluginUrl' => plugin_dir_url(__FILE__),
        ));
    }
});
