<?php
/**
 * Golpo Theme Customizer.
 *
 * @package Golpo
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function golpo_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

    // -------------------------------------------------------------------------
    // PANELS
    // -------------------------------------------------------------------------
    $wp_customize->add_panel( 'golpo_design_panel', array(
        'priority'       => 10,
        'capability'     => 'edit_theme_options',
        'theme_supports' => '',
        'title'          => __( 'General Design', 'golpo' ),
        'description'    => __( 'Global design settings for the theme.', 'golpo' ),
    ) );

    // -------------------------------------------------------------------------
    // CORE SECTIONS MOVE TO PANEL
    // -------------------------------------------------------------------------
    // Move Core Colors to General Design -> Global Colors handled below, or just reassign panel
    // Note: Core `colors` section exists. We can reuse it or move it.
    // As per user request: "move 'Colors' option from theme customizer root to under 'General Design' > 'Global Colors'"
    // We already created 'golpo_colors_section' in previous step, let's use that as the primary one or move core controls there.
    // Core 'colors' section usually contains 'header_textcolor' and 'background_color'.
    
    $wp_customize->get_control( 'background_color' )->section = 'golpo_colors_section';
    $wp_customize->get_control( 'header_textcolor' )->section = 'golpo_colors_section';
    
    // Attempt to move Background Image and Header Image
    $wp_customize->get_section( 'background_image' )->panel = 'golpo_design_panel';
    $wp_customize->get_section( 'header_image' )->panel = 'golpo_design_panel';
    
    // Move Site Identity LOGO resizer
    $wp_customize->add_setting( 'golpo_logo_width', array(
        'default'           => 150,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_logo_width', array(
        'label'       => __( 'Logo Width (px)', 'golpo' ),
        'description' => __( 'Adjust the width of the logo.', 'golpo' ),
        'section'     => 'title_tagline', // Site Identity
        'type'        => 'range',
        'input_attrs' => array(
            'min'  => 50,
            'max'  => 600,
            'step' => 5,
        ),
    ) );


    // -------------------------------------------------------------------------
    // LAYOUT SECTION
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_layout_section', array(
        'title'    => __( 'Global Layout', 'golpo' ),
        'panel'    => 'golpo_design_panel',
        'priority' => 10,
    ) );

    $wp_customize->add_setting( 'golpo_global_layout', array(
        'default'   => 'full-width',
        'transport' => 'refresh', // Needs refresh to apply structural classes
        'sanitize_callback' => 'sanitize_key',
    ) );

    $wp_customize->add_control( 'golpo_global_layout', array(
        'label'    => __( 'Site Layout', 'golpo' ),
        'section'  => 'golpo_layout_section',
        'type'     => 'select',
        'choices'  => array(
            'full-width' => __( 'Full Width', 'golpo' ),
            'boxed'      => __( 'Boxed', 'golpo' ),
            'narrow'     => __( 'Narrow', 'golpo' ),
        ),
    ) );

    // Container Custom Width
    $wp_customize->add_setting( 'golpo_container_width', array(
        'default'           => 1140,
        'transport'         => 'postMessage',
        'sanitize_callback' => 'absint',
    ) );

    // 1. Number Control (Precise Input)
    $wp_customize->add_control( 'golpo_container_width_visual', array(
        'label'       => __( 'Container Width (Slider)', 'golpo' ),
        'section'     => 'golpo_layout_section',
        'settings'    => 'golpo_container_width', // Bind to same setting
        'type'        => 'range',
        'input_attrs' => array(
            'min'  => 800,
            'max'  => 1600,
            'step' => 10,
        ),
    ) );

    // 2. Number Control (Exact Value)
    $wp_customize->add_control( 'golpo_container_width_number', array(
        'label'       => __( 'Container Width (px)', 'golpo' ),
        'description' => __( 'Precise value. Drag the slider above or type here.', 'golpo' ),
        'section'     => 'golpo_layout_section',
        'settings'    => 'golpo_container_width', // Bind to same setting
        'type'        => 'number',
        'input_attrs' => array(
            'min'  => 800,
            'max'  => 1600,
            'step' => 10,
        ),
    ) );

    $wp_customize->add_setting( 'golpo_sidebar_layout', array(
        'default'   => 'right',
        'transport' => 'refresh',
        'sanitize_callback' => 'sanitize_key', 
    ) );

    $wp_customize->add_control( 'golpo_sidebar_layout', array(
        'label'    => __( 'Sidebar Layout', 'golpo' ),
        'section'  => 'golpo_layout_section',
        'type'     => 'select',
        'choices'  => array(
            'right'      => __( 'Right Sidebar', 'golpo' ),
            'left'       => __( 'Left Sidebar', 'golpo' ),
            'no-sidebar' => __( 'No Sidebar', 'golpo' ),
        ),
    ) );

    // Sidebar Width
    $wp_customize->add_setting( 'golpo_sidebar_width', array(
        'default'           => 30,
        'transport'         => 'postMessage', // We will hook this up to CSS
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_sidebar_width', array(
        'label'       => __( 'Sidebar Width (%)', 'golpo' ),
        'description' => __( 'Percentage of width for the sidebar (e.g., 30). Content will take the remaining space.', 'golpo' ),
        'section'     => 'golpo_layout_section',
        'type'        => 'number',
        'input_attrs' => array(
            'min'  => 20,
            'max'  => 50,
            'step' => 1,
        ),
    ) );

    // -------------------------------------------------------------------------
    // BLOG SETTINGS SECTION
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_blog_section', array(
        'title'    => __( 'Blog Settings', 'golpo' ),
        'panel'    => 'golpo_design_panel',
        'priority' => 15,
    ) );

    // Grid Columns
    $wp_customize->add_setting( 'golpo_grid_columns', array(
        'default'           => 1,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_grid_columns', array(
        'label'    => __( 'Blog Grid Columns', 'golpo' ),
        'section'  => 'golpo_blog_section',
        'type'     => 'select',
        'choices'  => array(
            1 => __( '1 Column', 'golpo' ),
            2 => __( '2 Columns', 'golpo' ),
            3 => __( '3 Columns', 'golpo' ),
            4 => __( '4 Columns', 'golpo' ),
        ),
    ) );

    // Posts Per Page
    $wp_customize->add_setting( 'golpo_posts_per_page', array(
        'default'           => get_option( 'posts_per_page' ),
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_posts_per_page', array(
        'label'    => __( 'Posts Per Page', 'golpo' ),
        'section'  => 'golpo_blog_section',
        'type'     => 'number',
        'input_attrs' => array( 'min' => 1, 'max' => 50, 'step' => 1 ),
    ) );

    // Post Meta Toggle
    $wp_customize->add_setting( 'golpo_enable_post_meta', array(
        'default'           => true,
        'transport'         => 'refresh',
        'sanitize_callback' => 'golpo_sanitize_checkbox',
    ) );

    $wp_customize->add_control( 'golpo_enable_post_meta', array(
        'label'    => __( 'Show Post Meta (Date, Author, etc.)', 'golpo' ),
        'section'  => 'golpo_blog_section',
        'type'     => 'checkbox',
    ) );

    // -------------------------------------------------------------------------
    // COLORS SECTION
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_colors_section', array(
        'title'    => __( 'Global Colors', 'golpo' ),
        'panel'    => 'golpo_design_panel',
        'priority' => 20,
    ) );

    // Predefined Color Palettes
    $wp_customize->add_setting( 'golpo_color_scheme', array(
        'default'           => 'default',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'sanitize_key',
    ) );

    $wp_customize->add_control( 'golpo_color_scheme', array(
        'label'    => __( 'Color Scheme', 'golpo' ),
        'section'  => 'golpo_colors_section',
        'type'     => 'select',

        'choices'  => array(
            'default'     => __( 'Default (Modern Corporate)', 'golpo' ),
            'ocean'       => __( 'Ocean Blue', 'golpo' ),
            'midnight'    => __( 'Midnight Blue (Dark)', 'golpo' ),
            'forest'      => __( 'Forest Green', 'golpo' ),
            'emerald'     => __( 'Emerald City', 'golpo' ),
            'sunset'      => __( 'Sunset Orange', 'golpo' ),
            'coral'       => __( 'Coral Reef', 'golpo' ),
            'berry'       => __( 'Berry Red', 'golpo' ),
            'rose'        => __( 'Dusty Rose', 'golpo' ),
            'charcoal'    => __( 'Charcoal & Gold (Dark)', 'golpo' ),
            'slate'       => __( 'Slate & Teal', 'golpo' ),
            'coffee'      => __( 'Coffee & Cream', 'golpo' ),
            'royal'       => __( 'Royal Gold (Dark)', 'golpo' ),
            'lavender'    => __( 'Soft Lavender', 'golpo' ),
            'mint'        => __( 'Fresh Mint', 'golpo' ),
            'custom'      => __( 'Custom Palette', 'golpo' ),
        ),
    ) );

    // Individual Colors (Only effective if 'Custom' is selected or overridden)
    $colors = array(
        'golpo_primary_color'   => array('default' => '#030120', 'label' => __( 'Primary Color', 'golpo' )),
        'golpo_secondary_color' => array('default' => '#a156b4', 'label' => __( 'Secondary Color', 'golpo' )),
        'golpo_text_color'      => array('default' => '#555555', 'label' => __( 'Text Color', 'golpo' )),
        'golpo_link_color'      => array('default' => '#030120', 'label' => __( 'Link Color', 'golpo' )),
        'golpo_link_hover_color'=> array('default' => '#595777', 'label' => __( 'Link Hover Color', 'golpo' )),
    );

    foreach( $colors as $id => $data ) {
        $wp_customize->add_setting( $id, array(
            'default'           => $data['default'],
            'transport'         => 'postMessage',
            'sanitize_callback' => 'sanitize_hex_color',
        ) );

        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $id . '_control', array(
            'label'    => $data['label'],
            'section'  => 'golpo_colors_section',
            'settings' => $id,
        ) ) );
    }

    // -------------------------------------------------------------------------
    // TYPOGRAPHY SECTION
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_typography_section', array(
        'title'    => __( 'Global Typography', 'golpo' ),
        'panel'    => 'golpo_design_panel',
        'priority' => 30,
    ) );

    $wp_customize->add_setting( 'golpo_body_font', array(
        'default'           => 'Raleway',
        'transport'         => 'refresh',
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( 'golpo_body_font', array(
        'label'       => __( 'Body Font Family (Google Font Name)', 'golpo' ),
        'description' => __( 'Enter a valid Google Font name (e.g., Roboto, Open Sans).', 'golpo' ),
        'section'     => 'golpo_typography_section',
        'type'        => 'text',
    ) );

    // Body Font Size
    $wp_customize->add_setting( 'golpo_body_font_size', array(
        'default'           => 16,
        'transport'         => 'postMessage',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_body_font_size', array(
        'label'    => __( 'Body Font Size (px)', 'golpo' ),
        'section'  => 'golpo_typography_section',
        'type'     => 'number',
        'input_attrs' => array( 'min' => 10, 'max' => 30, 'step' => 1 ),
    ) );

    // Site Title Font Size
    $wp_customize->add_setting( 'golpo_site_title_font_size', array(
        'default'           => 32,
        'transport'         => 'postMessage',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_site_title_font_size', array(
        'label'    => __( 'Site Title Font Size (px)', 'golpo' ),
        'section'  => 'golpo_typography_section',
        'type'     => 'number',
        'input_attrs' => array( 'min' => 18, 'max' => 60, 'step' => 1 ),
    ) );

    // Headings (H1 - H6)
    for ( $i = 1; $i <= 6; $i++ ) {
        $wp_customize->add_setting( 'golpo_h' . $i . '_font_size', array(
            'default'           => ( 40 - ( $i * 4 ) ), // Approximate defaults: H1=36, H2=32, etc.
            'transport'         => 'postMessage',
            'sanitize_callback' => 'absint',
        ) );

        $wp_customize->add_control( 'golpo_h' . $i . '_font_size', array(
            'label'    => sprintf( __( 'H%s Font Size (px)', 'golpo' ), $i ),
            'section'  => 'golpo_typography_section',
            'type'     => 'number',
            'input_attrs' => array( 'min' => 10, 'max' => 80, 'step' => 1 ),
        ) );
    }

    // Sidebar Heading Font Size
    $wp_customize->add_setting( 'golpo_sidebar_heading_font_size', array(
        'default'           => 20,
        'transport'         => 'postMessage',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_sidebar_heading_font_size', array(
        'label'    => __( 'Sidebar Heading Font Size (px)', 'golpo' ),
        'section'  => 'golpo_typography_section',
        'type'     => 'number',
        'input_attrs' => array( 'min' => 12, 'max' => 40, 'step' => 1 ),
    ) );

    // Sidebar Link Font Size
    $wp_customize->add_setting( 'golpo_sidebar_link_font_size', array(
        'default'           => 14,
        'transport'         => 'postMessage',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_sidebar_link_font_size', array(
        'label'    => __( 'Sidebar Link Font Size (px)', 'golpo' ),
        'section'  => 'golpo_typography_section',
        'type'     => 'number',
        'input_attrs' => array( 'min' => 10, 'max' => 30, 'step' => 1 ),
    ) );

    // -------------------------------------------------------------------------
    // BUTTONS SECTION
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_buttons_section', array(
        'title'    => __( 'Global Buttons', 'golpo' ),
        'panel'    => 'golpo_design_panel',
        'priority' => 40,
    ) );

    $wp_customize->add_setting( 'golpo_btn_bg_color', array(
        'default'           => '#030120',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'sanitize_hex_color',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'golpo_btn_bg_color', array(
        'label'    => __( 'Button Background', 'golpo' ),
        'section'  => 'golpo_buttons_section',
    ) ) );

    $wp_customize->add_setting( 'golpo_btn_text_color', array(
        'default'           => '#ffffff',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'sanitize_hex_color',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'golpo_btn_text_color', array(
        'label'    => __( 'Button Text Color', 'golpo' ),
        'section'  => 'golpo_buttons_section',
    ) ) );

    $wp_customize->add_setting( 'golpo_btn_radius', array(
        'default'           => '5',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( 'golpo_btn_radius', array(
        'label'    => __( 'Button Border Radius (px)', 'golpo' ),
        'section'  => 'golpo_buttons_section',
        'type'     => 'number',
    ) );

    // -------------------------------------------------------------------------
    // FEATURES: Dark Mode & Back To Top
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_features_section', array(
        'title'    => __( 'Extra Features', 'golpo' ),
        'panel'    => 'golpo_design_panel',
        'priority' => 50,
    ) );

    $wp_customize->add_setting( 'golpo_enable_dark_mode', array(
        'default'           => true,
        'transport'         => 'refresh',
        'sanitize_callback' => 'golpo_sanitize_checkbox',
    ) );

    $wp_customize->add_control( 'golpo_enable_dark_mode', array(
        'label'    => __( 'Enable Dark Mode Toggle', 'golpo' ),
        'section'  => 'golpo_features_section',
        'type'     => 'checkbox',
    ) );

    $wp_customize->add_setting( 'golpo_enable_back_to_top', array(
        'default'           => true,
        'transport'         => 'refresh',
        'sanitize_callback' => 'golpo_sanitize_checkbox',
    ) );

    $wp_customize->add_control( 'golpo_enable_back_to_top', array(
        'label'    => __( 'Enable Back to Top Button', 'golpo' ),
        'section'  => 'golpo_features_section',
        'type'     => 'checkbox',
    ) );

    // -------------------------------------------------------------------------
    // PERFORMANCE / CACHE (UI Only - Functionality requires plugin logic)
    // -------------------------------------------------------------------------
    $wp_customize->add_section( 'golpo_cache_section', array(
        'title'    => __( 'Cache & Optimization', 'golpo' ),
        'panel'    => '', // Root Level
        'priority' => 100,
    ) );

    $cache_options = array(
        'page_caching'           => __( 'Enable Page Caching', 'golpo' ),
        'cache_preloading'       => __( 'Enable Cache Preloading', 'golpo' ),
        'remove_unused_css'      => __( 'Remove Unused CSS', 'golpo' ),
        'preload_links'          => __( 'Preload Links', 'golpo' ),
        'delay_script_execution' => __( 'Delay Script Execution', 'golpo' ),
        'minify_css_js'          => __( 'Minify CSS & JS', 'golpo' ),
        'defer_scripts'          => __( 'Defer Scripts', 'golpo' ),
        'optimize_database'      => __( 'Optimize Database', 'golpo' ),
        'lazy_load'              => __( 'Lazy Load Images', 'golpo' ),
        'optimize_google_fonts'  => __( 'Optimize Google Fonts', 'golpo' ),
        'resize_images'          => __( 'Responsive Image Placeholders', 'golpo' ),
        'browser_caching'        => __( 'Enable Browser Caching', 'golpo' ),
        'gzip_compression'       => __( 'Enable GZIP Compression', 'golpo' ),
        'mobile_caching'         => __( 'Separate Mobile Cache', 'golpo' ),
    );

    foreach ( $cache_options as $key => $label ) {
        $setting_id = 'golpo_perf_' . $key;
        $wp_customize->add_setting( $setting_id, array(
            'default'           => false,
            'transport'         => 'postMessage', // No-op refresh
            'sanitize_callback' => 'golpo_sanitize_checkbox',
        ) );

        $wp_customize->add_control( $setting_id, array(
            'label'    => $label, // Already translated
            'section'  => 'golpo_cache_section',
            'type'     => 'checkbox',
        ) );
    }

	// -------------------------------------------------------------------------
    // LEGACY / EXISTING SETTINGS (SOCIAL & FOOTER)
    // -------------------------------------------------------------------------
	$wp_customize->add_section( 'social' , array(
		'title'      => esc_html__( 'Social', 'golpo' ),
		'priority'   => 30,
	) );

    $socials = array('facebook', 'twitter', 'instagram', 'github', 'youtube');
    foreach($socials as $social) {
        $wp_customize->add_setting( 'social_' . $social , array(
            'default'     => '',
            'transport'   => 'postMessage',
            'sanitize_callback'    => 'esc_url_raw',
        ) );
        $wp_customize->add_control( 'social_' . $social, array(
            'label'        => ucfirst($social),
            'section'    => 'social',
            'settings'   => 'social_' . $social,
            'priority'    => 9
        ) );
    }

	// Footer Section
	$wp_customize->add_section( 'footer' , array(
		'title'      => esc_html__( 'Footer', 'golpo' ),
		'priority'   => 30,
	) );

	$wp_customize->add_setting( 'footer_text' , array(
		'default'     => sprintf('<a href="%1$s">%2$s</a>', esc_url(__('https://wordpress.org/', 'golpo')), __('Proudly powered by WordPress', 'golpo')),
		'sanitize_callback'    => 'golpo_sanitize_html_text',
	) );

	$wp_customize->add_control( 'footer_text', array(
		'label'        => esc_html__( 'Footer Text', 'golpo' ),
		'section'    => 'footer',
		'settings'   => 'footer_text',
		'type'       => 'textarea',
		'priority'    => 9
	) );

	$wp_customize->add_setting( 'footer_love', array(
		'default'           => 1,
		'sanitize_callback' => 'absint',
	) );

	$wp_customize->add_control( 'footer_love', array(
		'label'    => esc_html__( 'Display some love by showing credit to me.', 'golpo' ),
		'section'  => 'footer',
		'settings' => 'footer_love',
		'type'     => 'checkbox',
	) );


}
add_action( 'customize_register', 'golpo_customize_register' );

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function golpo_customize_preview_js() {
	wp_enqueue_script( 'golpo_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20160512', true );
}
add_action( 'customize_preview_init', 'golpo_customize_preview_js' );

/**
 * Enqueue Checkbox Reset Script (Simplified for immediate reset of checkboxes via JS if needed, 
 * but here we just add a global reset btn script for the controls pane)
 */
function golpo_customize_controls_js() {
    wp_enqueue_script( 'golpo_customizer_reset', get_template_directory_uri() . '/assets/js/customizer-reset.js', array( 'jquery', 'customize-controls' ), '1.0', true );
    
    // Pass object for ajax
    wp_localize_script( 'golpo_customizer_reset', 'golpo_reset_obj', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'nonce'   => wp_create_nonce( 'golpo_reset_nonce' ),
        'confirm' => __( 'Are you sure you want to reset all theme settings to default?', 'golpo' )
    ));
}
add_action( 'customize_controls_enqueue_scripts', 'golpo_customize_controls_js' );


function golpo_sanitize_html_text( $value ) {
	$value = wp_kses_post( $value );
	return $value;
}

function golpo_sanitize_checkbox( $checked ) {
	return ( ( isset( $checked ) && true == $checked ) ? true : false );
}