<?php
/**
 * Theme Customizer
 *
 * @package Zentorone
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Zentorone_Customizer {

	public function __construct() {
		add_action( 'customize_register', array( $this, 'register_settings' ) );
	}

	/**
	 * Register Settings
	 *
	 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
	 */
	public function register_settings( $wp_customize ) {

		$wp_customize->add_panel( 'zentorone_theme_options', array(
			'priority'       => 20,  // Öncelik sırası, diğer varsayılan panellerle çakışmasın diye ayarlayın
			'title'          => __( 'Zentorone Theme Options', 'zentorone' ),
			'description'    => __( 'All theme-specific customizations.', 'zentorone' ),
		) );

		// 1. Section: Homepage Slider
		$wp_customize->add_section( 'zentorone_slider_section', array(
			'priority'       => 21,
			'title'          => __( 'Homepage Slider', 'zentorone' ),
			'description'    => __( 'Configure up to 5 slides for the homepage carousel.', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

		for ( $i = 1; $i <= 5; $i++ ) {
			// Slide Image
			$wp_customize->add_setting( 'zentorone_slider_image_' . $i, array(
				'default'           => '',
				'sanitize_callback' => 'esc_url_raw',
			) );
			$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'zentorone_slider_image_' . $i, array(
				'label'    => sprintf( __( 'Slide %d Image', 'zentorone' ), $i ),
				'section'  => 'zentorone_slider_section',
			) ) );

			// Slide Title
			$wp_customize->add_setting( 'zentorone_slider_title_' . $i, array(
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			) );
			$wp_customize->add_control( 'zentorone_slider_title_' . $i, array(
				'label'    => sprintf( __( 'Slide %d Title', 'zentorone' ), $i ),
				'section'  => 'zentorone_slider_section',
				'type'     => 'text',
			) );

			// Slide Description
			$wp_customize->add_setting( 'zentorone_slider_desc_' . $i, array(
				'default'           => '',
				'sanitize_callback' => 'sanitize_textarea_field',
			) );
			$wp_customize->add_control( 'zentorone_slider_desc_' . $i, array(
				'label'    => sprintf( __( 'Slide %d Description', 'zentorone' ), $i ),
				'section'  => 'zentorone_slider_section',
				'type'     => 'textarea',
			) );

			// Slide Button Link
			$wp_customize->add_setting( 'zentorone_slider_link_' . $i, array(
				'default'           => '',
				'sanitize_callback' => 'esc_url_raw',
			) );
			$wp_customize->add_control( 'zentorone_slider_link_' . $i, array(
				'label'    => sprintf( __( 'Slide %d Button Link', 'zentorone' ), $i ),
				'section'  => 'zentorone_slider_section',
				'type'     => 'url',
			) );
		}


		// 2. Section: Layout Settings
		$wp_customize->add_section( 'zentorone_layout_section', array(
			'priority'       => 22,
			'title'          => __( 'Layout Settings', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

		// Setting: Global Layout
		$wp_customize->add_setting( 'zentorone_global_layout', array(
			'default'           => 'right-sidebar',
			'sanitize_callback' => 'sanitize_key',
		) );

		$wp_customize->add_control( 'zentorone_global_layout', array(
			'type'    => 'select',
			'section' => 'zentorone_layout_section',
			'label'   => __( 'Default Layout', 'zentorone' ),
			'choices' => array(
				'right-sidebar' => __( 'Right Sidebar', 'zentorone' ),
				'left-sidebar'  => __( 'Left Sidebar', 'zentorone' ),
				'no-sidebar'    => __( 'No Sidebar (Boxed)', 'zentorone' ),
				'full-width'    => __( 'Full Width', 'zentorone' ),
			),
		) );

		// 3. Section: Header Options
		$wp_customize->add_section( 'zentorone_header_section', array(
			'priority'       => 23,
			'title'          => __( 'Header Options', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

        // Show Logo
        $wp_customize->add_setting( 'zentorone_show_logo', array(
            'default'           => true,
            'sanitize_callback' => 'zentorone_sanitize_checkbox',
        ) );
        $wp_customize->add_control( 'zentorone_show_logo', array(
            'type'    => 'checkbox',
            'section' => 'zentorone_header_section',
            'label'   => __( 'Show Logo', 'zentorone' ),
        ) );

        // Show Site Title
        $wp_customize->add_setting( 'zentorone_show_site_title', array(
            'default'           => true,
            'sanitize_callback' => 'zentorone_sanitize_checkbox',
        ) );
        $wp_customize->add_control( 'zentorone_show_site_title', array(
            'type'    => 'checkbox',
            'section' => 'zentorone_header_section',
            'label'   => __( 'Show Site Title', 'zentorone' ),
        ) );

        // Show Site Tagline
        $wp_customize->add_setting( 'zentorone_show_site_tagline', array(
            'default'           => false,
            'sanitize_callback' => 'zentorone_sanitize_checkbox',
        ) );
        $wp_customize->add_control( 'zentorone_show_site_tagline', array(
            'type'    => 'checkbox',
            'section' => 'zentorone_header_section',
            'label'   => __( 'Show Site Tagline', 'zentorone' ),
        ) );

		// 4. Section: Typography
		$wp_customize->add_section( 'zentorone_typography_section', array(
			'priority'       => 24,
			'title'          => __( 'Typography', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

		$wp_customize->add_setting( 'zentorone_body_font', array(
			'default'           => 'Open Sans',
			'sanitize_callback' => 'sanitize_text_field',
		) );

		$wp_customize->add_control( 'zentorone_body_font', array(
			'type'    => 'select',
			'section' => 'zentorone_typography_section',
			'label'   => __( 'Body Font', 'zentorone' ),
			'choices' => array(
				'Open Sans'  => 'Open Sans',
				'Lora'       => 'Lora',
				'Montserrat' => 'Montserrat',
				'Inter'      => 'Inter',
			),
		) );

		$wp_customize->add_setting( 'zentorone_heading_font', array(
			'default'           => 'Montserrat',
			'sanitize_callback' => 'sanitize_text_field',
		) );

		$wp_customize->add_control( 'zentorone_heading_font', array(
			'type'    => 'select',
			'section' => 'zentorone_typography_section',
			'label'   => __( 'Heading Font', 'zentorone' ),
			'choices' => array(
				'Open Sans'  => 'Open Sans',
				'Lora'       => 'Lora',
				'Montserrat' => 'Montserrat',
				'Inter'      => 'Inter',
			),
		) );

		// 5. Section: Navigation & Breadcrumbs
		$wp_customize->add_section( 'zentorone_nav_section', array(
			'priority'       => 25,
			'title'          => __( 'Navigation & Breadcrumbs', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

		$wp_customize->add_setting( 'zentorone_breadcrumbs_enabled', array(
			'default'           => true,
			'sanitize_callback' => 'zentorone_sanitize_checkbox',
		) );

		$wp_customize->add_control( 'zentorone_breadcrumbs_enabled', array(
			'type'    => 'checkbox',
			'section' => 'zentorone_nav_section',
			'label'   => __( 'Enable Breadcrumbs', 'zentorone' ),
		) );

		// 6. Section: Social Media
		$wp_customize->add_section( 'zentorone_social_section', array(
			'priority'       => 26,
			'title'          => __( 'Social Media Links', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

		$socials = array( 'facebook', 'twitter', 'instagram', 'youtube', 'linkedin' );
		foreach ( $socials as $social ) {
			$wp_customize->add_setting( 'zentorone_social_' . $social, array(
				'default'           => '',
				'sanitize_callback' => 'esc_url_raw',
			) );

			$wp_customize->add_control( 'zentorone_social_' . $social, array(
				'type'    => 'url',
				'section' => 'zentorone_social_section',
				'label'   => ucfirst( $social ) . ' ' . __( 'URL', 'zentorone' ),
			) );
		}

		// 7. Section: Footer Options
		$wp_customize->add_section( 'zentorone_footer_section', array(
			'priority'       => 27,
			'title'          => __( 'Footer Options', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
		) );

		$wp_customize->add_setting( 'zentorone_footer_copyright', array(
			'sanitize_callback' => 'zentorone_sanitize_html',
			'default' => sprintf(
				__( '&copy; %d %s. All Rights Reserved.', 'zentorone' ),
				date_i18n( 'Y' ),
				get_bloginfo( 'name' )
			),
			'transport' => 'refresh',
		) );

		$wp_customize->add_control( 'zentorone_footer_copyright', array(
			'type'    => 'textarea',
			'section' => 'zentorone_footer_section',
			'label'   => __( 'Copyright Text', 'zentorone' ),
		) );
		
		// 8. Section: Performance & Assets
		$wp_customize->add_section( 'zentorone_performance_general', array(
			'priority'       => 28,
			'title'          => __( 'Performance & Assets', 'zentorone' ),
			'panel'          => 'zentorone_theme_options',
			'description'    => __( 'Manage how assets are loaded.', 'zentorone' ),
		) );

		// Setting: Font Source
		$wp_customize->add_setting( 'zentorone_font_source', array(
			'default'           => 'google',
			'sanitize_callback' => 'sanitize_key',
			'transport'         => 'refresh',
		) );

		$wp_customize->add_control( 'zentorone_font_source', array(
			'type'    => 'radio',
			'section' => 'zentorone_performance_general',
			'label'   => __( 'Font Source', 'zentorone' ),
			'choices' => array(
				'local'  => __( 'Local (Self-Hosted)', 'zentorone' ),
				'google' => __( 'Google Fonts', 'zentorone' ),
			),
		) );






	}
}

/**
 * Sanitize JS
 */
function zentorone_sanitize_js( $js ) {
	return $js; // In production, consider more robust sanitization if needed, but JS is inherently risky.
}

/**
 * Sanitize checkbox
 */
function zentorone_sanitize_checkbox( $checked ) {
	return ( ( isset( $checked ) && true === $checked ) ? true : false );
}

/**
 * Sanitize HTML (allows basic tags for copyright text)
 */
function zentorone_sanitize_html( $input ) {
    $allowed_html = array(
        'a'      => array( 'href' => array(), 'title' => array() ),
        'br'     => array(),
        'em'     => array(),
        'strong' => array(),
        'span'   => array(),
    );
    return wp_kses( $input, $allowed_html );
}

/**
 * Customizer Heading Control
 */
if ( class_exists( 'WP_Customize_Control' ) ) {
	class Zentorone_Customizer_Heading_Control extends WP_Customize_Control {
		public $type = 'heading';

		public function render_content() {
			if ( ! empty( $this->label ) ) {
				echo '<h3 style="margin: 20px 0 10px; padding-bottom: 5px; border-bottom: 1px solid #ddd; color: #333;">' . esc_html( $this->label ) . '</h3>';
			}
		}
	}
}
