<?php
/**
 * Theme Customizer Controls
 *
 * @package Technical Blogging
 */

if ( ! function_exists( 'technical_blogging_customizer_typography_setting_register' ) ) :
function technical_blogging_customizer_typography_setting_register( $wp_customize ) {

    // Add Typography Panel for Body and Heading
    $wp_customize->add_panel(
        'technical_blogging_typography_settings_panel',
        array(
            'priority'      => 30,
            'capability'    => 'edit_theme_options',
            'title'         => esc_html__( 'Typography Settings', 'technical-blogging' ),
        )
    );

    // Section Body Typography
    $wp_customize->add_section(
        'technical_blogging_body_typography_settings',
        array(
            'priority'      => 25,
            'capability'    => 'edit_theme_options',
            'title'         => esc_html__( 'Body', 'technical-blogging' ),
            'panel'         => 'technical_blogging_typography_settings_panel',
        )
    );

    // Body Font Family Setting
    $wp_customize->add_setting(
        'technical_blogging_body_font_family',
        array(
            'default'           => 'Poppins, serif', // Default font
            'sanitize_callback' => 'technical_blogging_sanitize_font_family', // Custom sanitize function
        )
    );
    $wp_customize->add_control( new Technical_Blogging_Font_Select_Control(
    $wp_customize,
    'technical_blogging_body_font_family',
    array(
        'label'   => esc_html__( 'Body Font Family', 'technical-blogging' ),
        'section' => 'technical_blogging_body_typography_settings',
        'choices' => technical_blogging_get_google_fonts(),
    )
    ));
    $wp_customize->add_control(
        'technical_blogging_body_font_family',
        array(
            'label'   => esc_html__( 'Body Font Family', 'technical-blogging' ),
            'section' => 'technical_blogging_body_typography_settings',
            'type'    => 'select',
            'choices' => technical_blogging_get_google_fonts(), // Fetch available Google Fonts
        )
    );

    // Section Heading Typography
    $wp_customize->add_section(
        'technical_blogging_heading_typography_settings',
        array(
            'priority'      => 25,
            'capability'    => 'edit_theme_options',
            'title'         => esc_html__( 'Heading', 'technical-blogging' ),
            'panel'         => 'technical_blogging_typography_settings_panel',
        )
    );

    // Heading Font Family Setting
    $wp_customize->add_setting(
        'technical_blogging_heading_font_family',
        array(
            'default'           => 'Poppins, serif', // Default font
            'sanitize_callback' => 'technical_blogging_sanitize_font_family', // Custom sanitize function
        )
    );
    $wp_customize->add_control( new Technical_Blogging_Font_Select_Control(
    $wp_customize,
    'technical_blogging_body_font_family',
    array(
        'label'   => esc_html__( 'Body Font Family', 'technical-blogging' ),
        'section' => 'technical_blogging_body_typography_settings',
        'choices' => technical_blogging_get_google_fonts(),
    )
    ));
    $wp_customize->add_control(
        'technical_blogging_heading_font_family',
        array(
            'label'   => esc_html__( 'Heading Font Family', 'technical-blogging' ),
            'section' => 'technical_blogging_heading_typography_settings',
            'type'    => 'select',
            'choices' => technical_blogging_get_google_fonts(), // Fetch available Google Fonts
        )
    );
}
endif;

add_action( 'customize_register', 'technical_blogging_customizer_typography_setting_register' );

// Function to fetch Google Fonts
function technical_blogging_get_google_fonts() {
    // Add Google Fonts to be available for selection
    return array(
        'Poppins, serif' => 'Poppins',
        'Arial, sans-serif'   => 'Arial',
        'Georgia, serif'      => 'Georgia',
        'Verdana, sans-serif' => 'Verdana',
        'Times New Roman, serif' => 'Times New Roman',
        'Roboto, sans-serif'  => 'Roboto',
        'Open Sans, sans-serif' => 'Open Sans',
        'Lora, serif'         => 'Lora',
        'Merriweather, serif' => 'Merriweather',
        'Montserrat, sans-serif' => 'Montserrat',
        
        // Add more Google fonts as needed
    );
}

// Sanitize Google Fonts input
function technical_blogging_sanitize_font_family( $value ) {
    $allowed_fonts = array('Poppins, serif','Arial, sans-serif', 'Georgia, serif', 'Verdana, sans-serif','Times New Roman, serif', 'Roboto, sans-serif', 'Open Sans, sans-serif',
        'Lora, serif', 'Merriweather, serif', 'Montserrat, sans-serif',
        // Add more allowed fonts to this array
    );

    if ( in_array( $value, $allowed_fonts ) ) {
        return $value;
    } else {
        return 'Poppins, serif'; // Default fallback font
    }
}

function technical_blogging_sanitize_title( $value ) {
    return sanitize_text_field( $value );
}
