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

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Customizer\Section\Footer_Top_Row as Footer_Top_Row;
    use BBE\Customizer\Section\Footer_Middle_Row as Footer_Middle_Row;
    use BBE\Customizer\Section\Footer_Bottom_Row as Footer_Bottom_Row;
    use BBE\Helpers as Helpers;

    use function esc_html__;
    use function esc_attr;
    use function get_template_directory_uri;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'footer_builder_section' );
                $this->add_control( 'footer_builder_section_tab' );
                $this->add_control( 'footer_builder_layouts' );
                $this->add_control( 'footer_builder' );
                $this->tab = 'design';
                $this->add_control( 'footer_builder_box_shadow' );
                $this->add_control( 'footer_builder_margin' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'footer_builder_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_builder_layouts' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_image' ],
                        'default' =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_builder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_builder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_builder_box_shadow' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_box_shadow' ],
                        'transport' =>  'postMessage',
                        'default' =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_builder_margin' =>  [
                        '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 = [
                    'footer_builder_section' =>  [
                        'title' =>  esc_html__( 'Footer', 'blog-bee' ),
                        'priority' =>  55,
                    ],
                    'footer_builder_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'footer_builder_layouts' =>  [
                        'label' =>  esc_html__( 'Layouts', 'blog-bee' ),
                        'type'  =>  'builder-layout',
                        'builder_id'    =>  'footer_builder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Custom', 'blog-bee' ),
                                'url' =>  get_template_directory_uri() . '/inc/customizer/assets/images/header-builder-custom.webp',
                                'value' =>  'custom'
                            ],
                            [
                                'label' =>  esc_html__( 'Layout 1', 'blog-bee' ),
                                'url' =>  get_template_directory_uri() . '/inc/customizer/assets/images/footer-builder-one.webp',
                                'value' =>  'one'
                            ],
                            [
                                'label' =>  esc_html__( 'Layout 2', 'blog-bee' ),
                                'url' =>  get_template_directory_uri() . '/inc/customizer/assets/images/footer-builder-two.webp',
                                'value' =>  'two'
                            ]
                        ],
                        'field_values'  =>  [
                            'one'   =>  [
                                'top'   =>  [
                                    'first' =>  [],
                                    'second' =>  [],
                                    'third' =>  []
                                ],
                                'middle'    =>  [
                                    'first' =>  [],
                                    'second' =>  [],
                                    'third' =>  []
                                ],
                                'bottom'   =>  [
                                    'first' =>  [ 'footer-menu' ],
                                    'second' =>  [ 'copyright' ],
                                    'third' =>  [ 'footer-social-icons', 'scroll-to-top' ],
                                ],
                            ],
                            'two'   =>  [
                                'top'   =>  [
                                    'first' =>  [],
                                    'second' =>  [ 'footer-logo' ],
                                    'third' =>  []
                                ],
                                'middle'    =>  [
                                    'first' =>  [],
                                    'second' =>  [ 'footer-menu', 'footer-social-icons' ],
                                    'third' =>  []
                                ],
                                'bottom'    =>  [
                                    'first' =>  [],
                                    'second' =>  [ 'copyright' ],
                                    'third' =>  [ 'scroll-to-top' ],
                                ],
                            ],
                        ]
                    ],
                    'footer_builder' =>  [
                        'type'  =>  'bbe-builder',
                        'layout' =>  'footer_builder_layouts',
                        'row_section_ids'   =>  [
                            'top' =>  'footer_top_row_section',
                            'middle' =>  'footer_middle_row_section',
                            'bottom' =>  'footer_bottom_row_section',
                        ],
                        'widgets'   =>  [
                            'copyright'  =>  [
                                'label' =>  esc_html__( 'Copyright', 'blog-bee' ),
                                'section_id' =>  'copyright_section',
                                'icon'  =>  'text'
                            ],
                            'footer-logo'  =>  [
                                'label' =>  esc_html__( 'Logo', 'blog-bee' ),
                                'section_id' =>  'footer_logo_section',
                                'icon'  =>  'admin-site'
                            ],
                            'scroll-to-top'  =>  [
                                'label' =>  esc_html__( 'Scroll to Top', 'blog-bee' ),
                                'section_id' =>  'scroll_to_top_section',
                                'icon'  =>  'arrow-up-alt2'
                            ],
                            'footer-social-icons'  =>  [
                                'label' =>  esc_html__( 'Social Icons', 'blog-bee' ),
                                'section_id' =>  'footer_social_icons_section',
                                'icon'  =>  'share'
                            ],
                            'footer-menu'  =>  [
                                'label' =>  esc_html__( 'Footer Menu', 'blog-bee' ),
                                'section_id' =>  'footer_menu_section',
                                'icon'  =>  'menu'
                            ],
                            'footer-widget-area-one'  =>  [
                                'label' =>  esc_html__( 'Widget 1', 'blog-bee' ),
                                'section_id' =>  'footer_widget_area_one_section',
                                'icon'  =>  'screenoptions'
                            ],
                            'footer-widget-area-two'  =>  [
                                'label' =>  esc_html__( 'Widget 2', 'blog-bee' ),
                                'section_id' =>  'footer_widget_area_two_section',
                                'icon'  =>  'screenoptions'
                            ],
                            'footer-widget-area-three'  =>  [
                                'label' =>  esc_html__( 'Widget 3', 'blog-bee' ),
                                'section_id' =>  'footer_widget_area_three_section',
                                'icon'  =>  'screenoptions'
                            ],
                            'footer-html-1'  =>  [
                                'label' =>  esc_html__( 'HTML 1', 'blog-bee' ),
                                'section_id' =>  'footer_html_one_section',
                                'icon'  =>  'editor-code'
                            ],
                            'footer-html-2'  =>  [
                                'label' =>  esc_html__( 'HTML 2', 'blog-bee' ),
                                'section_id' =>  'footer_html_two_section',
                                'icon'  =>  'editor-code',
                                'disable'   =>  true
                            ],
                            'footer-shortcode-1'  =>  [
                                'label' =>  esc_html__( 'Shortcode 1', 'blog-bee' ),
                                'section_id' =>  'footer_shortcode_one_section',
                                'icon'  =>  'shortcode'
                            ],
                            'footer-shortcode-2'  =>  [
                                'label' =>  esc_html__( 'Shortcode 2', 'blog-bee' ),
                                'section_id' =>  'footer_shortcode_two_section',
                                'icon'  =>  'shortcode',
                                'disable'   =>  true
                            ],
                            'footer-divider-1'  =>  [
                                'label' =>  esc_html__( 'Divider 1', 'blog-bee' ),
                                'section_id' =>  'footer_divider_one_section',
                                'icon'  =>  'minus'
                            ],
                            'footer-divider-2'  =>  [
                                'label' =>  esc_html__( 'Divider 2', 'blog-bee' ),
                                'section_id' =>  'footer_divider_two_section',
                                'icon'  =>  'minus',
                                'disable'   =>  true
                            ],
                            'footer-divider-3'  =>  [
                                'label' =>  esc_html__( 'Divider 3', 'blog-bee' ),
                                'section_id' =>  'footer_divider_three_section',
                                'icon'  =>  'minus',
                                'disable'   =>  true
                            ],
                            'footer-image'  =>  [
                                'label' =>  esc_html__( 'Image', 'blog-bee' ),
                                'section_id' =>  'footer_image_section',
                                'icon'  =>  'format-image',
                                'disable'   =>  true
                            ],
                        ]
                    ],
                    'footer_builder_box_shadow' =>  [
                        'label' =>  esc_html__( 'Box Shadow', 'blog-bee' ),
                        'type'  =>  'box-shadow',
                        'tab'   =>  'design'
                    ],
                    'footer_builder_margin' =>  [
                        'label' =>  esc_html__( 'Margin', 'blog-bee' ),
                        'type'  =>  'dimension',
                        'tab'   =>  'design'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'footer_builder_section_tab' =>  'general',
                    'footer_builder_layouts' =>  'one',
                    'footer_builder' =>  [
                        'top'   =>  [
                            'first' =>  [],
                            'second' =>  [],
                            'third' =>  [],
                        ],
                        'middle'   =>  [
                            'first' =>  [],
                            'second' =>  [],
                            'third' =>  [],
                        ],
                        'bottom'   =>  [
                            'first' =>  [ 'footer-menu' ],
                            'second' =>  [ 'copyright' ],
                            'third' =>  [ 'footer-social-icons', 'scroll-to-top' ],
                        ],
                    ],
                    'footer_builder_background' =>  $this->get_color( [ 'value' => '#ffe4e0' ] ),
                    'footer_builder_border' =>  $this->get_border(),
                    'footer_builder_box_shadow' =>  $this->get_box_shadow(),
                    'footer_builder_margin' =>  $this->get_dimension( [
                        'top'    =>  40,
                        'link'  =>  false
                    ] ),
                ] );
            }

            /**
             * Add partials
             * 
             * @override
             * @since 1.0.0
             */
            protected function register_partials() {
                $this->manager->selective_refresh->add_partial( 'footer_builder_section_tab', [
                    'selector'        => 'footer.site-footer'
                ]);
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'footer_builder_box_shadow' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_builder_box_shadow' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'footer_builder_box_shadow' ),
                        'selector'  =>  'body.bbe-light .bbe-footer'
                    ],
                    'footer_builder_margin' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_builder_margin' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'footer_builder_margin' ),
                        'selector'  =>  'body .bbe-footer',
                        'property'  =>  'margin'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'site-footer';
                $block_class[] = 'bbe-builder';
                ?>
                    <footer id="colophon" class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>" <?php Helpers::the_aos_data_attr(); ?>>

                        <?php 

                            Footer_Top_Row::get_instance()->render_html();

                            Footer_Middle_Row::get_instance()->render_html();

                            Footer_Bottom_Row::get_instance()->render_html();

                        ?>

                    </footer><!-- #colophon -->
                <?php
            }

            /**
             * Render row html
             * 
             * @return html
             * @since 1.0.0
             * @param   string  $row_id The row id
             * @param   string  $row    The row
             */
            public static function render_row_html( $row_id, $row ) {
                $block_class[] = $row_id;
                $block_class[] = 'bbe-footer-row';
                $block_class[] = 'bbe-row';
                $full_width = Theme_Defaults::get_theme_option( 'footer_'. $row .'_row_full_width' );
                $direction = Theme_Defaults::get_theme_option( 'footer_'. $row .'_row_direction' );
                $alignment = Theme_Defaults::get_theme_option( 'footer_'. $row .'_row_horizontal_alignment' );
                $visibility = Theme_Defaults::get_theme_option( 'footer_'. $row .'_row_visibility' );
                $block_class[] = 'full-width--' . $full_width;
                $block_class[] = 'direction--' . $direction;
                $block_class[] = 'align--' . $alignment;
                
                $footer_builder = Theme_Defaults::get_theme_option( 'footer_builder' );
                $row = $footer_builder[ $row ];
                if( ! array_filter( $row ) ) return;

                if( empty( $row ) || ! is_array( $row ) ) {
                    // return if $row is empty or not an array, no need to render row
                    return;
                } else {
                    // add class if row has widgets
                    $widgets = $row && isset( $row[ 'second' ] ) ? $row[ 'second' ] : [];
                    if( count( $widgets ) > 0 ) {
                        $block_class[] = 'has-middle-column';
                    }
                }

                $all_registered_sections = self::get_all_registered_sections();
                $visibility_arr = explode( ' ', $visibility );

                $data_attrs = [
                    'data-desktop=' . esc_attr( ( in_array( 'desktop', $visibility_arr ) ? 'show' : 'hide' ) ) . '',
                    'data-tablet=' . esc_attr( ( in_array( 'tablet', $visibility_arr ) ? 'show' : 'hide' ) ) . '',
                    'data-mobile=' . esc_attr( ( in_array( 'mobile', $visibility_arr ) ? 'show' : 'hide' ) ) . '',
                ];
                ?>  
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>" <?php echo esc_attr( implode( ' ', $data_attrs ) ); ?>>
                        <div class="bbe-container">
                            <div class="row">
                                <?php
                                    foreach( $row as $column => $widgets ) :
                                        $column_class = 'column ' . $column;
                                        if( ( $column === 'second' ) && empty( $widgets ) ) continue;  // skip if no widgets in column, apply this logic for second columns only
                                        echo '<div class="'. esc_attr( $column_class ) .'">';
                                            foreach( $widgets as $widget ) :
                                                $all_registered_sections[ 'bbe-' . $widget ]::get_instance()->render_html();
                                            endforeach;
                                        echo '</div>';
                                    endforeach;
                                ?>
                            </div>
                        </div>
                    </div>
                <?php
            }
        }
        Footer_Builder::get_instance();
    }