<?php
    /**
     * Breadcrumb Section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Helpers as Helpers;
    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;

    use function esc_html__;
    use function esc_url;
    use function esc_attr;
    use function esc_html;
    use function get_permalink;

    if( ! class_exists( __NAMESPACE__ . '\\Breadcrumb' ) ) {
        /**
         * Breadcrumb class
         */
        class Breadcrumb extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-breadcrumb';

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'breadcrumb_section' );
                $this->add_control( 'breadcrumb_section_tab' );
                $this->add_control( 'breadcrumb_option' );
                $this->add_control( 'breadcrumb_type' );
                $this->add_control( 'breadcrumb_alignment' );
                $this->tab = 'design';
                $this->add_control( 'breadcrumb_typography' );
                $this->add_control( 'breadcrumb_text_color' );
                $this->add_control( 'breadcrumb_link_color' );
                $this->add_control( 'breadcrumb_background' );
                $this->add_control( 'breadcrumb_padding' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'breadcrumb_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_type' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_text_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_link_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'breadcrumb_padding' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'breadcrumb_section' =>  [
                        'title' =>  esc_html__( 'Breadcrumb', 'blog-bee' ),
                        'panel' =>  'global_panel',
                        'priority'  =>  40
                    ],
                    'breadcrumb_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'breadcrumb_option' =>  [
                        'label' =>  esc_html__( 'Show Breadcrumb', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'breadcrumb_type' =>  [
                        'label' =>  esc_html__( 'Breadcrumb Type', 'blog-bee' ),
                        'description' =>  esc_html__( 'If you use other than "default" one you will need to install and activate respective plugins Breadcrumb NavXT, Yoast SEO and Rank Math SEO.', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  [
                            'default'   =>  esc_html__( 'Default', 'blog-bee' ),
                            'navxt'   =>  esc_html__( 'NavXT', 'blog-bee' ),
                            'yoast'   =>  esc_html__( 'Yoast SEO', 'blog-bee' ),
                            'rank-math'   =>  esc_html__( 'Rank Math', 'blog-bee' ),
                        ]
                    ],
                    'breadcrumb_alignment' =>  [
                        'label' =>  esc_html__( 'Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'breadcrumb_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'breadcrumb_text_color' =>  [
                        'label' =>  esc_html__( 'Text Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'breadcrumb_link_color' =>  [
                        'label' =>  esc_html__( 'Link Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'breadcrumb_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'breadcrumb_padding' =>  [
                        'label' =>  esc_html__( 'Padding', 'blog-bee' ),
                        'type'  =>  'dimension'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'breadcrumb_section_tab'   =>  'general',
                    'breadcrumb_option'   =>  false,
                    'breadcrumb_type'   =>  'default',
                    'breadcrumb_icon_picker'   =>  $this->get_icon_picker( [ 'value' => 'fa-solid fa-chevron-right' ] ),
                    'breadcrumb_alignment'   =>  'left',
                    'breadcrumb_typography'   =>  $this->get_typography( [
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                        'line_height'   =>  $this->get_responsive( 1.6, 1.6, 1.6 )
                    ] ),
                    'breadcrumb_text_color'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'breadcrumb_link_color'   =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#0073aa' ] ),
                    ],
                    'breadcrumb_background'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'breadcrumb_box_shadow'   =>  $this->get_box_shadow(),
                    'breadcrumb_padding'   =>  $this->get_dimension(),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'breadcrumb_alignment'   =>  [
                        'selector'  =>  '.bbe-breadcrumb',
                        'prefix'    =>  'align--'
                    ],
                    'breadcrumb_typography'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'breadcrumb_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'breadcrumb_typography' ),
                        'selector'  =>  '.bbe-breadcrumb .trail-item a, .bbe-breadcrumb .trail-end span',
                    ],
                    'breadcrumb_text_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'breadcrumb_text_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'breadcrumb_text_color' ),
                        'selector'  =>  '.bbe-breadcrumb .trail-end span',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'breadcrumb_link_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'breadcrumb_link_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'breadcrumb_link_color' ),
                        'selector'  =>  '.bbe-breadcrumb .trail-item a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'breadcrumb_background'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'breadcrumb_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'breadcrumb_background' ),
                        'selector'  =>  '.bbe-breadcrumb',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'breadcrumb_padding'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'breadcrumb_padding' ),
                        'default' =>  Theme_Defaults::get_defaults( 'breadcrumb_padding' ),
                        'selector'  =>  '.bbe-breadcrumb',
                        'property'  =>  'padding',
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                if( ! Theme_Defaults::get_theme_option( 'breadcrumb_option' ) ) return;
                $type = Theme_Defaults::get_theme_option( 'breadcrumb_type' );
                $icon_picker = Theme_Defaults::get_theme_option( 'breadcrumb_icon_picker' );
                $alignment = Theme_Defaults::get_theme_option( 'breadcrumb_alignment' );
                
                $block_class[] = self::$id;
                $block_class[] = 'type--' . $type;
                $block_class[] = 'align--' . $alignment;
                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ) ?>">

                        <?php
                            switch( $type ) :

                                case 'yoast':
                                        if( function_exists( 'yoast_breadcrumb' ) ) yoast_breadcrumb();
                                    break;

                                case 'rankmath':
                                        if( function_exists( 'rank_math_the_breadcrumbs' ) ) rank_math_the_breadcrumbs();
                                    break;

                                case 'bcn':
                                        if( function_exists( 'bcn_display' ) ) bcn_display();
                                    break;

                                default:
                                        $this->get_breadcrumb();
                                    break;

                            endswitch;
                        ?>

                    </div><!-- .bbe-breadcrumb -->
                <?php
            }

            /**
             * Get breadcrumb
             * 
             * @since 1.0.0
             */
            public function get_breadcrumb() {
                if ( ! function_exists( 'breadcrumb_trail' ) ) {
                    // load class file
                    require_once get_template_directory() . '/inc/breadcrumb-trail/breadcrumb-trail.php';
                }

                // arguments variable
                $breadcrumb_args = [
                    'container' => 'div',
                    'show_browse' => false
                ];
                breadcrumb_trail( $breadcrumb_args );
            }
        }
        Breadcrumb::get_instance();
    }