<?php
/**
 * Adds the extended Global and Categories sections and their fields to the Colors Section.
 * 
 * @package Blogbuster
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'customize_register', 'blogbuster_register_theme_colors_options' );

if ( ! function_exists( 'blogbuster_register_theme_colors_options' ) ) :

    /**
     * Register theme options for Colors section.
     * 
     * @param WP_Customize_Manager $wp_customize Object that holds the customizer data.
     * @since 1.0.0
     */
    function blogbuster_register_theme_colors_options( $wp_customize ) {

        /*
         * Failsafe is safe
         */
        if ( ! isset( $wp_customize ) ) {
            return;
        }

        /**
         * Add Color Section
         * 
         * General Settings > Colors
         * 
         * @uses $wp_customize->add_section() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_section/
         * @since 1.0.0
         */
        $wp_customize->add_section( new Blogbuster_Customize_Section (
            $wp_customize, 'blogbuster_section_theme_colors',
                array(
                    'priority'  => 60,
                    'panel'     => 'blogbuster_panel_general',
                    'title'     => __( 'Colors', 'blogbuster' ),
                )
            )
        );

    /*------------------------------- Colors > Base Colors -------------------------------------------*/

        /**
         * Add Base Colors Section
         * 
         * General Settings > Colors
         * 
         * @uses $wp_customize->add_section() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_section/
         * @since 1.0.0
         */
        $wp_customize->add_section( new Blogbuster_Customize_Section (
            $wp_customize, 'blogbuster_section_colors_base',
                array(
                    'priority'  => 10,
                    'panel'     => 'blogbuster_panel_general',
                    'section'   => 'blogbuster_section_theme_colors',
                    'title'     => __( 'Base Colors', 'blogbuster' ),
                )
            )
        );

        /**
         * Color Picker field for Primary Color
         * 
         * General Settings > Colors > Base Colors
         * 
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_primary_theme_color',
            array(
                'default'           => blogbuster_get_customizer_default ( 'blogbuster_primary_theme_color' ),
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize, 'blogbuster_primary_theme_color',
                array(
                    'priority'   => 5,
                    'label'      => __( 'Primary Color', 'blogbuster' ),
                    'section'    => 'blogbuster_section_colors_base',
                    'settings'   => 'blogbuster_primary_theme_color'
                )
            )
        );

        /**
         * Color Picker field for Text Color
         * 
         * General Settings > Colors > Base Colors
         * 
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_text_color',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_text_color' ),
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize, 'blogbuster_text_color',
                array(
                    'label'      => __( 'Text Color', 'blogbuster' ),
                    'section'    => 'blogbuster_section_colors_base',
                    'settings'   => 'blogbuster_text_color',
                    'priority'   => 10
                )
            )
        );

        /**
         * Color Picker field for Link Color
         * 
         * General Settings > Colors > Base Colors
         * 
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_link_color',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_link_color' ),
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize, 'blogbuster_link_color',
                array(
                    'label'      => __( 'Link Color', 'blogbuster' ),
                    'section'    => 'blogbuster_section_colors_base',
                    'settings'   => 'blogbuster_link_color',
                    'priority'   => 15
                )
            )
        );

        /**
         * Color Picker field for Link Hover Color
         * 
         * General Settings > Colors > Base Colors
         * 
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_link_hover_color',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_link_hover_color' ),
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize, 'blogbuster_link_hover_color',
                array(
                    'label'      => __( 'Link Hover Color', 'blogbuster' ),
                    'section'    => 'blogbuster_section_colors_base',
                    'settings'   => 'blogbuster_link_hover_color',
                    'priority'   => 20
                )
            )
        );

    /*------------------------------- Colors > Categories Colors -------------------------------------*/

        /**
         * Add Categories Colors Section
         * 
         * General Settings > Colors
         * 
         * @uses $wp_customize->add_section() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_section/
         * @since 1.0.0
         */
        $wp_customize->add_section( new Blogbuster_Customize_Section (
            $wp_customize, 'blogbuster_section_colors_categories',
                array(
                    'priority'      => 20,
                    'panel'         => 'blogbuster_panel_general',
                    'section'       => 'blogbuster_section_theme_colors',
                    'title'         => __( 'Categories Colors', 'blogbuster' ),
                )
            )
        );

        /**
         * Color Picker field for Categories Color
         * 
         * General Settings > Colors > Categories Colors
         * 
         * @since 1.0.0
         */
        $priority = 5;
        $categories = get_categories( array( 'hide_empty' => 1 ) );

        foreach ( $categories as $category_list ) {
            $wp_customize->add_setting( 'category_color_'.esc_attr( $category_list->term_id ),
                array(
                    'default'           => '#ffeed6',
                    'sanitize_callback' => 'sanitize_hex_color',
                )
            );
            $wp_customize->add_control( new WP_Customize_Color_Control(
                $wp_customize, 'category_color_'.esc_attr( $category_list->term_id ),
                    array(
                        'label'      => sprintf( __( '%1$s Color', 'blogbuster' ), esc_html( $category_list->name ) ),
                        'section'    => 'blogbuster_section_colors_categories',
                        'settings'   => 'category_color_'.esc_attr( $category_list->term_id ),
                        'priority'   => absint( $priority )
                    )
                )
            );
            $priority += 5;
        }

    }

endif;