<?php

namespace WPSP_PRO;

class Assets
{
    public function __construct()
    {
        add_action('enqueue_block_assets', array($this, 'block_scripts'), 11);
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'), 30);
        add_filter('wpsp_settings_global', array($this, 'settings_objects_extend'));
    }
    /**
     * Gutten Support
     * @since 1.2.0
     */
    public function block_scripts()
    {
        if (!class_exists('\WPSP\Helper')) return;
        global $post_type;
        $allow_post_types = \WPSP_PRO\Helper::get_settings('allow_post_types');
        $allow_post_types = (!empty($allow_post_types) ? $allow_post_types : array('post'));
        if (!in_array($post_type, $allow_post_types) || !is_admin()) {
            return;
        }
        $manage_schedule = \WPSP\Helper::get_settings('manage_schedule');
        $activeScheduleStatus = (isset($manage_schedule->activeScheduleSystem) ? $manage_schedule->activeScheduleSystem : '');

        $is_enable_active_schedule_system = false;
        if( isset( $manage_schedule->activeScheduleSystem ) && $manage_schedule->activeScheduleSystem == 'manual_schedule' ) {
            $is_enable_active_schedule_system = $manage_schedule->manual_schedule->is_active_status;
        }else if( isset( $manage_schedule->activeScheduleSystem ) && $manage_schedule->activeScheduleSystem == 'auto_schedule' ) {
            $is_enable_active_schedule_system = $manage_schedule->auto_schedule->is_active_status;
        }
        $dep = include WPSP_PRO_ROOT_PATH . '/assets/js/admin.asset.php';
        wp_enqueue_style('wps-publish-date', WPSP_PRO_ASSETS_URL . 'css/wpsp-admin.min.css');
        wp_enqueue_script('wps-publish-date', WPSP_PRO_ASSETS_URL . 'js/admin.js', $dep['dependencies'], $dep['version'], true);
        wp_localize_script('wps-publish-date', 'WPSchedulePosts', apply_filters('WPSchedulePostsData', array(
            'AutoSchedulePanelTitle' => __('Auto Schedule', 'wp-scheduled-posts-pro'),
            'ManualSchedulePanelTitle' => __('Manual Schedule', 'wp-scheduled-posts-pro'),
            'schedule' => Helper::manual_schedule(),
            'allowedPostTypes' => $allow_post_types,
            'currentTime' => array(
                'date' => current_time('mysql'),
                'date_gmt' => current_time('mysql', 1),
            ),
            'activeScheduleSystem' => $activeScheduleStatus,
            'auto_date' => Helper::auto_schedule(),
            'wpsp_settings_name' => defined(WPSP_SETTINGS_NAME) ? WPSP_SETTINGS_NAME : 'wpsp_settings_v5',
            'is_active_classic_editor'  => Helper::is_enable_classic_editor(),
            'is_enable_active_schedule_system' => $is_enable_active_schedule_system,
            'image_path'     => WPSP_PRO_PLUGIN_URL . 'assets/images/',
        )));
    }

    public function enqueue_scripts($hook)
    {
        if (!class_exists('\WPSP\Helper')) return;
        $manage_schedule = \WPSP\Helper::get_settings('manage_schedule');
        $activeScheduleStatus = (isset($manage_schedule->activeScheduleSystem) ? $manage_schedule->activeScheduleSystem : '');
        $is_enable_active_schedule_system = false;
        if( isset( $manage_schedule->activeScheduleSystem ) && $manage_schedule->activeScheduleSystem == 'manual_schedule' ) {
            $is_enable_active_schedule_system = $manage_schedule->manual_schedule->is_active_status;
        }else if( isset( $manage_schedule->activeScheduleSystem ) && $manage_schedule->activeScheduleSystem == 'auto_schedule' ) {
            $is_enable_active_schedule_system = $manage_schedule->auto_schedule->is_active_status;
        }

        $current_screen = get_current_screen();
        if (is_admin() && \WPSP\Helper::plugin_page_hook_suffix($current_screen->post_type, $hook)) {
            wp_enqueue_script('media-upload');
            wp_enqueue_script('wpscp-pro-scripts', WPSP_PRO_ASSETS_URL . 'js/wpscp-pro-scripts.js', array('jquery'), WPSP_PRO_VERSION, true);
            wp_localize_script(
                'wpscp-pro-scripts',
                'wpscp_pro_ajax_object',
                array(
                    'ajax_url' => admin_url('admin-ajax.php'),
                    'PanelTitle' => __('Schedule at', 'wp-scheduled-posts-pro'),
                    'schedule' => Helper::manual_schedule(),
                    'currentTime' => array(
                        'date' => current_time('mysql'),
                        'date_gmt' => current_time('mysql', 1),
                    ),
                    'activeScheduleSystem' => $activeScheduleStatus,
                    'auto_date' => Helper::auto_schedule(),
                    'is_enable_active_schedule_system' => $is_enable_active_schedule_system,
                )
            );
            $dep = include WPSP_PRO_ROOT_PATH . '/assets/js/license.asset.php';
            wp_enqueue_style('wpsp-pro-scripts-license', WPSP_PRO_ASSETS_URL . 'css/license.css',array(), WPSP_PRO_VERSION);
	        wp_enqueue_script('wpsp-pro-scripts-license', WPSP_PRO_ASSETS_URL . 'js/license.js', $dep['dependencies'], $dep['version'], true);
            wp_localize_script('wpsp-pro-scripts-license', 'WPSchedulePostsLicense', apply_filters('WPSchedulePostsData', array(
                'image_path'     => WPSP_PRO_PLUGIN_URL . 'assets/images/',
            )));
        }
    }
    public function settings_objects_extend($args)
    {
        $args['license_nonce'] = wp_create_nonce(WPSP_PRO_SL_ITEM_SLUG . '_license_nonce');
        return $args;
    }
}
