<?php
if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly

class Rivax_Admin_Setup {

    function __construct() {

        add_filter( 'single_template', array( $this, 'single_rivax_template' ) );
        add_action( 'admin_menu', array( $this, 'register_pages' ), 9 );
        add_action( 'admin_init', array( $this, 'redirect_to_welcome' ) );
        add_action('admin_enqueue_scripts', array( $this, 'admin_scripts' ), 100 );
        add_action('enqueue_block_editor_assets', array( $this, 'editor_scripts' ), 90 );
        add_filter( 'ocdi/import_files', array( $this, 'import_demo_config' ) );
        add_filter( 'ocdi/plugin_page_setup', array( $this, 'ocdi_plugin_page_setup' ) );

    }



    // Single post preview for rivax-template post type
    function single_rivax_template ($single_template) {
        global $post;

        if ( in_array($post->post_type, ['rivax-template', 'elementor_library']) ) {
            $single_template = RIVAX_THEME_DIR . '/admin/single-rivax-template.php';
        }

        return $single_template;
    }


    // plugin pages
    function register_pages() {
        add_menu_page( esc_html__('Theme Dashboard', 'newsfy'), esc_html__('Newsfy', 'newsfy'), 'manage_options', 'rivax-dashboard', '', 'dashicons-admin-generic', 50 );
        add_submenu_page( 'rivax-dashboard', esc_html__('Theme Dashboard', 'newsfy'), esc_html__('Theme Dashboard', 'newsfy'), 'manage_options', 'rivax-dashboard',  array($this, 'dashboard_welcome') );

    }


    // Dashboard welcome page
    function dashboard_welcome() {
        include_once RIVAX_THEME_DIR . '/admin/dashboard-welcome.php';
    }

    // Redirect to dashboard page after theme activation.
    function redirect_to_welcome() {
        global $pagenow;
        if ( is_admin() && isset( $_GET['activated'] ) && 'themes.php' === $pagenow ) {
            wp_safe_redirect( admin_url( "admin.php?page=rivax-dashboard" ) );
            exit;
        }
    }


    // Load admin assets in pages we need
    function admin_scripts($hook) {

        if( $hook == 'toplevel_page_rivax-dashboard' ) {
            wp_enqueue_style('rivax_admin_style', RIVAX_THEME_URI . '/admin/assets/css/admin.css', array(), null);
        }
        elseif( in_array($hook, ['post.php', 'post-new.php']) ) { // Redux Meta Box
            wp_enqueue_style('rivax_settings_style', RIVAX_THEME_URI . '/admin/assets/css/settings.css', array(), null);
        }
    }


	// Load Gutenberg assets
	function editor_scripts( $hook ) {

		wp_enqueue_style( 'rivax-editor-style', RIVAX_THEME_URI . '/admin/assets/css/editor.css', array(), null);
		wp_add_inline_style( 'rivax-editor-style', $this->get_gutenberg_custom_style() );
	}

	// Admin custom style
	function get_gutenberg_custom_style () {
		$custom_css = '';
		include_once RIVAX_THEME_DIR . '/inc/custom-style-editor.php';
		return $custom_css;
	}


    // Demo importer config. One Click Demo Import plugin
    function import_demo_config() {
        return [
            [
		        'import_file_name'             => esc_html__('DigiMag Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/digimag/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/digimag/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/digimag/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('TechMag Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/techmag/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/techmag/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/techmag/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('NewsPress Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/newspress/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/newspress/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/newspress/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('NewsTime Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/newstime/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/newstime/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/newstime/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('Crypto Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/crypto/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/crypto/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/crypto/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('DailyMag Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/dailymag/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/dailymag/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/dailymag/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('DailyPlus Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/dailyplus/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/dailyplus/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/dailyplus/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('FormalNews Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/formalnews/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/formalnews/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/formalnews/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
			[
		        'import_file_name'             => esc_html__('NewsRoom Demo', 'newsfy'),
		        'local_import_file'            => RIVAX_THEME_DIR . '/admin/demo/newsroom/content.xml',
		        'local_import_redux'           => [
			        [
				        'file_path'   => RIVAX_THEME_DIR . '/admin/demo/newsroom/redux.json',
				        'option_name' => 'rivax_newsfy_options',
			        ],
		        ],
		        'import_preview_image_url'     => RIVAX_THEME_URI . '/admin/demo/newsroom/screenshot.webp',
		        'preview_url'                  => 'https://landing.rivaxstudio.com/newsfy',
	        ],
        ];
    }


    // change One Click Demo Import settings
    function ocdi_plugin_page_setup( $default_settings ) {
        $default_settings['parent_slug'] = 'rivax-dashboard';
        $default_settings['menu_slug'] = 'rivax-demo-importer';
        return $default_settings;
    }

}

// Call Rivax_Admin_Setup
new Rivax_Admin_Setup();


// Plugins installer
include_once RIVAX_THEME_DIR . '/admin/plugins-installer.php';


// Redux Framework Config
include_once RIVAX_THEME_DIR . '/admin/redux-config.php';