<?php

/**
 * Neonic functions.
 *
 *
 * @package WordPress
 * @subpackage Neonic
 * @since Neonic 1.0
 */

/** Theme Page */
function neonic_add_theme_page() {
    add_theme_page(
        __( 'Neonic Theme Guide', 'neonic' ),       
        __( 'Neonic Theme Guide', 'neonic' ),       
        'edit_theme_options',            
        'neonic-theme-page',               
        'neonic_render_theme_page'         
    );
}
add_action( 'admin_menu', 'neonic_add_theme_page' );

function neonic_render_theme_page() {
    ?>
    <div class="wrap">
        <style>
            .theme-container {
                max-width: 900px;
                margin: 20px 0;
            }
            .theme-welcome-header {
                background: linear-gradient(
                    45deg,
                    #e66465,
                    #d8546c,
                    #c74672,
                    #b33b78,
                    #9c347c,
                    #b33b78,
                    #c74672,
                    #d8546c,
                    #e66465
                );
                background-size: 400% 400%;
                animation: gradient-flow 8s ease infinite;
                padding: 20px 30px;
                border-radius: 6px;
                box-shadow: 0 1px 3px rgba(0,0,0,0.08);
                color: white;
                margin-bottom: 25px;
            }
            @keyframes gradient-flow {
                0% { background-position: 0% 50%; }
                50% { background-position: 100% 50%; }
                100% { background-position: 0% 50%; }
            }
            .theme-welcome-header h1 {
                margin-top: 0;
                font-size: 2em;
                color: white;
            }
            .theme-welcome-header p {
                font-size: 16px;
                margin-bottom: 5px;
                opacity: 0.9;
            }
            .theme-content-section {
                background: white;
                padding: 25px 30px;
                border-radius: 6px;
                margin-bottom: 20px;
                box-shadow: 0 1px 3px rgba(0,0,0,0.08);
            }
            .theme-content-section h2 {
                margin-top: 0;
                padding-bottom: 10px;
                border-bottom: 1px solid #f0f0f0;
                font-size: 1.5em;
            }
            .theme-section-title {
                display: flex;
                align-items: center;
                gap: 10px;
            }
            .theme-section-title span {
                font-size: 22px;
            }
            .theme-steps {
                margin-left: 15px;
            }
            .theme-steps li {
                margin-bottom: 12px;
                font-size: 15px;
            }
            .theme-pro-section {
                background: #f9f9f9;
                padding: 20px;
                border-radius: 6px;
                border-left: 4px solid #c74672;
                margin-top: 25px;
            }
            .theme-pro-section h3 {
                margin-top: 0;
                font-size: 1.2em;
            }
            .theme-buttons {
                margin-top: 25px;
                display: flex;
                gap: 10px;
                flex-wrap: wrap;
            }
            .theme-button {
                display: inline-flex;
                align-items: center;
                gap: 8px;
                padding: 10px 18px;
                font-size: 14px;
                border-radius: 4px;
                text-decoration: none;
                font-weight: 500;
                transition: all 0.2s ease;
            }
            .theme-button-primary {
                background: #c74672;
                color: white;
                border: none;
            }
            .theme-button-primary:hover {
                background: #b33b78;
                color: white;
            }
            .theme-button-secondary {
                background: white;
                color: #333;
                border: 1px solid #ddd;
            }
            .theme-button-secondary:hover {
                background: #f7f7f7;
                color: #333;
                border-color: #ccc;
            }
            .theme-features-grid {
                display: grid;
                grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
                gap: 15px;
                margin-top: 20px;
            }
            .theme-feature-item {
                background: #f9f9f9;
                padding: 15px;
                border-radius: 4px;
                font-size: 14px;
                text-align: center;
            }
            .theme-feature-item span {
                display: block;
                font-size: 24px;
                margin-bottom: 8px;
            }
        </style>
        
        <div class="theme-container">
            <div class="theme-welcome-header">
                <h1><?php esc_html_e( 'Welcome to Neonic Theme', 'neonic' ); ?></h1>
                <p><?php esc_html_e( 'Thank you for choosing Neonic! This guide will help you get started with our theme.', 'neonic' ); ?></p>
            </div>
            
            <div class="theme-content-section">
                <div class="theme-section-title">
                    <h2><span>📚</span><?php esc_html_e( 'How to Use the Templates', 'neonic' ); ?></h2>
                </div>
                <p><?php esc_html_e( 'To use the available page templates, follow these steps:', 'neonic' ); ?></p>
                
                <ol class="theme-steps">
                    <li><?php esc_html_e( 'Go to', 'neonic' ); ?> <strong><?php esc_html_e( 'Pages > Add New', 'neonic' ); ?></strong> <?php esc_html_e( 'and create a new page. For example, name it "About".', 'neonic' ); ?></li>
                    <li><?php esc_html_e( 'Click', 'neonic' ); ?> <strong><?php esc_html_e( 'Publish', 'neonic' ); ?></strong> <?php esc_html_e( 'to save the page.', 'neonic' ); ?></li>
                    <li><?php esc_html_e( 'Next, go to', 'neonic' ); ?> <strong><?php esc_html_e( 'Pages > All Pages', 'neonic' ); ?></strong><?php esc_html_e( ', find your new page, and click', 'neonic' ); ?> <strong><?php esc_html_e( 'Quick Edit', 'neonic' ); ?></strong>.</li>
                    <li><?php esc_html_e( 'In the', 'neonic' ); ?> <strong><?php esc_html_e( 'Template', 'neonic' ); ?></strong> <?php esc_html_e( 'dropdown, select the template that matches your page — for example,', 'neonic' ); ?> <strong><?php esc_html_e( 'About Template', 'neonic' ); ?></strong>.</li>
                    <li><?php esc_html_e( 'Click', 'neonic' ); ?> <strong><?php esc_html_e( 'Update', 'neonic' ); ?></strong> <?php esc_html_e( 'to apply the template.', 'neonic' ); ?></li>
                </ol>
                
                <div class="theme-pro-section">
                    <h3>🔓 <?php esc_html_e( 'Unlock All Templates', 'neonic' ); ?></h3>
                    <p><?php esc_html_e( 'To access all exclusive templates and advanced layout options, consider upgrading to the', 'neonic' ); ?> <strong><?php esc_html_e( 'PRO version', 'neonic' ); ?></strong> <?php esc_html_e( 'of the Neonic theme.', 'neonic' ); ?></p>
                    <p><?php esc_html_e( '✨ Want to explore more premium designs? Visit our full collection at Supremacy Themes.', 'neonic' ); ?></p>
                    
                    <div class="theme-buttons">
                        <a href="https://supremacythemes.com/demo/?url=https://supremacythemes.com/demos/neonic/" class="theme-button theme-button-secondary" target="_blank">
                            <span class="dashicons dashicons-visibility"></span>
                            <?php esc_html_e( 'Preview PRO Version', 'neonic' ); ?>
                        </a>
                        <a href="https://supremacythemes.com/design/neonic/" class="theme-button theme-button-primary" target="_blank">
                            <span class="dashicons dashicons-star-filled"></span>
                            <?php esc_html_e( 'Upgrade to PRO', 'neonic' ); ?>
                        </a>
                        <a href="https://supremacythemes.com/" class="theme-button theme-button-secondary" target="_blank">
                            <span class="dashicons dashicons-store"></span>
                            <?php esc_html_e( 'Browse Our Themes', 'neonic' ); ?>
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="theme-content-section">
                <div class="theme-section-title">
                    <h2><span>⚙️</span><?php esc_html_e( 'Theme Customization', 'neonic' ); ?></h2>
                </div>
                <p><?php esc_html_e( 'You can easily customize your theme by going to Appearance > Customize. Here you can adjust colors, layouts, and more.', 'neonic' ); ?></p>
                <a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" class="theme-button theme-button-secondary">
                    <span class="dashicons dashicons-admin-customizer"></span>
                    <?php esc_html_e( 'Start Customizing', 'neonic' ); ?>
                </a>
                <?php
                $locked = (bool) get_option( 'neonic_blocks_locked', true );
                $icon_class = $locked ? 'dashicons-lock' : 'dashicons-unlock';
                $button_text = $locked ? __( 'Unlock blocks', 'neonic' ) : __( 'Lock blocks', 'neonic' );
                $toggle_url = esc_url( add_query_arg( 'neonic_toggle_lock', $locked ? '0' : '1' ) );
                ?>
                <a href="<?php echo $toggle_url; ?>" class="theme-button theme-button-secondary">
                    <span class="dashicons <?php echo esc_attr( $icon_class ); ?>"></span>
                    <?php echo esc_html( $button_text ); ?>
                </a>
            </div>
        </div>
    </div>
    <?php
}

function neonic_set_activation_transient() {
    set_transient( 'neonic_show_welcome_notice', true, 60 );
}
add_action( 'after_switch_theme', 'neonic_set_activation_transient' );

function neonic_admin_welcome_notice() {
    if ( get_transient( 'neonic_show_welcome_notice' ) ) {
        delete_transient( 'neonic_show_welcome_notice' );

        $theme_page_url = admin_url( 'themes.php?page=neonic-theme-page' );
        ?>
        <div class="notice notice-success is-dismissible">
            <p><strong><?php esc_html_e( 'Thank you for installing Neonic Theme! 🎉', 'neonic' ); ?></strong></p>
            <p><?php esc_html_e( 'Get started with our easy-to-follow guide for setting up your site.', 'neonic' ); ?></p>
            <p>
                <a href="<?php echo esc_url( $theme_page_url ); ?>" class="button button-primary"><?php esc_html_e( 'View Theme Guide', 'neonic' ); ?></a>
                <a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" class="button"><?php esc_html_e( 'Customize Theme', 'neonic' ); ?></a>
            </p>
        </div>
        <?php
    }
}
add_action( 'admin_notices', 'neonic_admin_welcome_notice' );

/** Enqueue Styles */
function neonic_enqueue_styles() {
	wp_enqueue_style(
		'neonic-style',
		get_template_directory_uri() . '/public/css/neonic.css',
		array(), 
		'1.0.0', 
		'all'    
	);
}
add_action( 'wp_enqueue_scripts', 'neonic_enqueue_styles' );


//ad
add_action('admin_bar_menu', function ($wp_admin_bar) {
    if ( ! current_user_can('edit_theme_options') ) {
        return;
    }

    $wp_admin_bar->add_node([
        'id'    => 'neonic_unlock_blocks',
        'title' => '<span style="
            padding:5px 10px;
            border-radius:6px;
            background: linear-gradient(
                    45deg,
                    #e66465,
                    #d8546c,
                    #c74672,
                    #b33b78,
                    #9c347c,
                    #b33b78,
                    #c74672
            );            
            color: white;
            border: none
        ">
            Unlock more features of Neonic
        </span>',
        'href'  => admin_url('themes.php?page=neonic-theme-page'),
        'meta'  => [
            'class'  => 'neonic-admin-bar-button',
            'title'  => 'Unlock blocks',
        ]
    ]);
}, 100);


function neonic_handle_toggle_lock() {
    if ( isset( $_GET['neonic_toggle_lock'] ) && current_user_can( 'edit_theme_options' ) ) {
        $new_value = $_GET['neonic_toggle_lock'] === '1' ? true : false;
        update_option( 'neonic_blocks_locked', $new_value );
        wp_safe_redirect( remove_query_arg( 'neonic_toggle_lock' ) );
        exit;
    }
}
add_action( 'admin_init', 'neonic_handle_toggle_lock' );

add_filter( 'block_editor_settings_all', function( $settings, $context ) {
    $locked = (bool) get_option( 'neonic_blocks_locked', true );
    if ( $locked ) {
        $settings['allowedBlockTypes'] = [];
    } else {
        $settings['allowedBlockTypes'] = true;
    }
    return $settings;
}, 20, 2 );

add_action( 'admin_init', function() {
    if ( isset( $_GET['neonic_toggle_block_lock'] ) && current_user_can( 'edit_theme_options' ) ) {
        $locked = $_GET['neonic_toggle_block_lock'] === '1' ? true : false;
        update_option( 'neonic_blocks_locked', $locked );
        wp_safe_redirect( remove_query_arg( 'neonic_toggle_block_lock' ) );
        exit;
    }
});