<?php
/**
 * Shinebuild functions.
 *
 *
 * @package WordPress
 * @subpackage Shinebuild
 * @since Shinebuild 1.0
 */

/** Theme Page */
function shinebuild_add_theme_page() {
    add_theme_page(
        __( 'Shinebuild Theme Guide', 'shinebuild' ),       
        __( 'Shinebuild Theme Guide', 'shinebuild' ),       
        'edit_theme_options',            
        'shinebuild-theme-page',               
        'shinebuild_render_theme_page'         
    );
}
add_action( 'admin_menu', 'shinebuild_add_theme_page' );

function shinebuild_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 Shinebuild Theme', 'shinebuild' ); ?></h1>
                <p><?php esc_html_e( 'Thank you for choosing Shinebuild! This guide will help you get started with our theme.', 'shinebuild' ); ?></p>
            </div>
            
            <div class="theme-content-section">
                <div class="theme-section-title">
                    <h2><span>📚</span><?php esc_html_e( 'How to Use the Templates', 'shinebuild' ); ?></h2>
                </div>
                <p><?php esc_html_e( 'To use the available page templates, follow these steps:', 'shinebuild' ); ?></p>
                
                <ol class="theme-steps">
                    <li><?php esc_html_e( 'Go to', 'shinebuild' ); ?> <strong><?php esc_html_e( 'Pages > Add New', 'shinebuild' ); ?></strong> <?php esc_html_e( 'and create a new page. For example, name it "About".', 'shinebuild' ); ?></li>
                    <li><?php esc_html_e( 'Click', 'shinebuild' ); ?> <strong><?php esc_html_e( 'Publish', 'shinebuild' ); ?></strong> <?php esc_html_e( 'to save the page.', 'shinebuild' ); ?></li>
                    <li><?php esc_html_e( 'Next, go to', 'shinebuild' ); ?> <strong><?php esc_html_e( 'Pages > All Pages', 'shinebuild' ); ?></strong><?php esc_html_e( ', find your new page, and click', 'shinebuild' ); ?> <strong><?php esc_html_e( 'Quick Edit', 'shinebuild' ); ?></strong>.</li>
                    <li><?php esc_html_e( 'In the', 'shinebuild' ); ?> <strong><?php esc_html_e( 'Template', 'shinebuild' ); ?></strong> <?php esc_html_e( 'dropdown, select the template that matches your page — for example,', 'shinebuild' ); ?> <strong><?php esc_html_e( 'About Template', 'shinebuild' ); ?></strong>.</li>
                    <li><?php esc_html_e( 'Click', 'shinebuild' ); ?> <strong><?php esc_html_e( 'Update', 'shinebuild' ); ?></strong> <?php esc_html_e( 'to apply the template.', 'shinebuild' ); ?></li>
                </ol>
                
                <div class="theme-pro-section">
                    <h3>🔓 <?php esc_html_e( 'Unlock All Templates', 'shinebuild' ); ?></h3>
                    <p><?php esc_html_e( 'To access all exclusive templates and advanced layout options, consider upgrading to the', 'shinebuild' ); ?> <strong><?php esc_html_e( 'PRO version', 'shinebuild' ); ?></strong> <?php esc_html_e( 'of the Shinebuild theme.', 'shinebuild' ); ?></p>
                    <p><?php esc_html_e( '✨ Want to explore more premium designs? Visit our full collection at Supremacy Themes.', 'shinebuild' ); ?></p>
                    
                    <div class="theme-buttons">
                        <a href="https://supremacythemes.com/demo/?url=https://supremacythemes.com/demos/shinebuild/" class="theme-button theme-button-secondary" target="_blank">
                            <span class="dashicons dashicons-visibility"></span>
                            <?php esc_html_e( 'Preview PRO Version', 'shinebuild' ); ?>
                        </a>
                        <a href="https://supremacythemes.com/design/shinebuild/" class="theme-button theme-button-primary" target="_blank">
                            <span class="dashicons dashicons-star-filled"></span>
                            <?php esc_html_e( 'Upgrade to PRO', 'shinebuild' ); ?>
                        </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', 'shinebuild' ); ?>
                        </a>
                    </div>
                </div>
            </div>
            
           <div class="theme-content-section">
                <div class="theme-section-title">
                    <h2><span>⚙️</span><?php esc_html_e( 'Theme Customization', 'shinebuild' ); ?></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.', 'shinebuild' ); ?></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', 'shinebuild' ); ?>
                </a>
                <?php
                $locked = (bool) get_option( 'shinebuild_blocks_locked', true );
                $icon_class = $locked ? 'dashicons-lock' : 'dashicons-unlock';
                $button_text = $locked ? __( 'Unlock blocks', 'shinebuild' ) : __( 'Lock blocks', 'shinebuild' );
                $toggle_url = esc_url( add_query_arg( 'shinebuild_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 shinebuild_set_activation_transient() {
    set_transient( 'shinebuild_show_welcome_notice', true, 60 );
}
add_action( 'after_switch_theme', 'shinebuild_set_activation_transient' );

function shinebuild_admin_welcome_notice() {
    if ( get_transient( 'shinebuild_show_welcome_notice' ) ) {
        delete_transient( 'shinebuild_show_welcome_notice' );

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




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

    $wp_admin_bar->add_node([
        'id'    => 'shinebuild_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 Shinebuild
        </span>',
        'href'  => admin_url('themes.php?page=shinebuild-theme-page'),
        'meta'  => [
            'class'  => 'shinebuild-admin-bar-button',
            'title'  => 'Unlock blocks',
        ]
    ]);
}, 100);


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

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

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