<?php
    /**
     * Error Page 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 BBE\Customizer\Section\Pagination as Pagination;

    use function esc_html__;
    use function get_search_query;
    use function get_search_form;
    use function esc_html;
    use function is_search;
    use function get_categories;
    use function get_tags;
    use function esc_html_e;
    use function esc_attr;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->add_section( 'error_page_settings_section' );
                $this->add_control( 'error_page_section_tab' );
                $this->add_control( 'error_page_image' );
                $this->add_control( 'error_icon_style' );
                $this->tab = 'design';
                $this->add_control( 'error_title_typography' );
                $this->add_control( 'error_title_button_settings' );
                $this->add_control( 'error_button_typography' );
                $this->add_control( 'error_button_color' );
                $this->add_control( 'error_button_background' );
                $this->add_control( 'error_button_border_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'error_page_section_tab' => [
                        'transport' => 'postMessage',
                        'default'   => Theme_Defaults::get_defaults( $id ),
                    ],
                    'error_page_image' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_icon_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_title_button_settings' =>  [
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_button_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_button_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_button_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'error_button_border_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];

                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'error_page_settings_section' => [
                        'title'    => esc_html__( '404 Page', 'blog-bee' ),
                        'panel'    => 'blog_panel',
                        'priority' => 45,
                    ],
                    'error_page_section_tab' => [
                        'type'   => 'section-tab',
                        'fields' => Helpers::get_customizer_section_tabs(),
                    ],
                    'error_page_image' =>  [
                        'label' =>  esc_html__( '404 Image', 'blog-bee' ),
                        'description' =>  esc_html__( 'Upload image that shows you are on 404 error page.', 'blog-bee' ),
                        'type'  =>  'media',
                        'mime_type' =>  'image',
                        'priority'  =>  10,
                    ],
                    'error_icon_style' =>  [
                        'label' =>  esc_html__( 'Button Style', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'  => Helpers::get_customizer_icon_style()
                    ],
                    'error_title_typography' =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'error_button_typography' =>  [
                        'label' =>  esc_html__( 'Button Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'error_title_button_settings' =>  [
                        'label' =>  esc_html__( 'Button Settings', 'blog-bee' ),
                        'type'  =>  'separator',
                    ],
                    'error_button_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'error_button_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'error_button_border_color' =>  [
                        'label' =>  esc_html__( 'Border Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                ];

                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'error_page_section_tab'=> 'general',
                    'error_page_title'      => "Oops! That page can't be found.",
                    'error_page_image'      =>  0,
                    'error_page_content' =>  'Sorry, the page you are looking for does not exist.',
                    'error_button_label' =>  'Back to Home',
                    'error_icon_style' =>  '',
                    'error_button_icon_picker' =>  $this->get_icon_picker( [ 'type' => 'icon', 'value' => 'fa-solid fa-arrow-right' ] ),
                    'error_title_typography'    =>  $this->get_typography( [
                        'font_weight'   =>  '500',
                        'font_size' =>  $this->get_responsive( '34px', '26px', '14px' ),
                        'line_height'   =>  $this->get_responsive( 1.46, 1.46, 1.46 )
                    ] ),
                    'error_title_button_settings'    =>  '',
                    'error_button_typography'    =>  $this->get_typography( [
                        'font_weight'   =>  '500',
                        'font_size' =>  $this->get_responsive( '22px', '16px', '14px' ),
                        'line_height'   =>  $this->get_responsive( 1.46, 1.46, 1.46 )
                    ] ),
                    'error_button_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#000' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '#E78371' ] ),
                    ],
                    'error_button_background' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#efefef' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '' ] ),
                    ],
                    'error_button_border_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#000' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '#000' ] ),
                    ],
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                 $dynamic_args = [
                    'error_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'error_title_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'error_title_typography' ),
                        'selector'  =>  '.bbe-error-404 .page-header .page-title'
                    ],
                    'error_button_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'error_button_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'error_button_typography' ),
                        'selector'  =>  'body .bbe-error-404 .error-actions .error-button'
                    ],
                    'error_button_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'error_button_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'error_button_color' ),
                        'selector'  =>  '.site .bbe-error-404 .error-actions .error-button',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'error_button_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'error_button_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'error_button_background' ),
                        'selector'  =>  '.site .bbe-error-404 .error-actions .error-button.filled',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'error_button_border_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'error_button_border_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'error_button_border_color' ),
                        'selector'  =>  '.site .bbe-error-404 .error-actions .error-button.outline',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                ];
                return $dynamic_args;
            }
            
            /**
             * Add partial
             * 
             * @override
             * @since 1.0.0
             */
            protected function register_partials() {
                $this->manager->selective_refresh->add_partial( 'error_button_partial', [
                    'selector'  =>  '.bbe-error-404 .error-button',
                    'render_callback'   =>  [ $this, 'render_partial' ],
                    'settings'  =>  [ 'error_button_icon_picker', 'error_button_label' ]
                ] );
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = '.bbe-error-404 .error-actions';
                $error_button_class[] = 'error-button';
                $error_button_class[] = Theme_Defaults::get_theme_option( 'error_icon_style' );
                ?>
                    <main id="primary" class="site-main bbe-error-404 not-found" <?php Helpers::the_aos_data_attr(); ?>>

                        <header class="page-header">

                            <h1 class="page-title"><?php echo esc_html( Theme_Defaults::get_theme_option( 'error_page_title' ) ); ?></h1>

                            <div class="error-image">

                                <?php
                                    $error_page_image = Theme_Defaults::get_theme_option( 'error_page_image' );
                                    if ( $error_page_image ) echo wp_get_attachment_image( $error_page_image, 'large', false, [ 'class' => 'error-page-image' ] );
                                ?>

                            </div>

                        </header><!-- .page-header -->

                        <div class="page-content">

                            <p class="error-desc"><?php echo esc_html( Theme_Defaults::get_theme_option( 'error_page_content' ) ); ?></p>

                            <div class="error-actions">

                                <a href="<?php echo esc_url( home_url() ); ?>" class="<?php echo implode( ' ', $error_button_class ); ?>">

                                    <?php
                                        $this->render_partial();
                                    ?>

                                </a>
                            </div>

                        </div>

                    </main><!-- #main -->
                <?php
            }

            /**
             * Partial Html
             * 
             * @since 1.0.0
             */
            public function render_partial() {
                $button_icon = Theme_Defaults::get_theme_option( 'error_button_icon_picker' );
                $label = Theme_Defaults::get_theme_option( 'error_button_label' );

                echo '<span class="label">'. esc_html( $label ) .'</span>';

                if ( is_array( $button_icon ) ) {
                    $button_icon['tag'] = 'span';
                    echo Helpers::get_icon_html( $button_icon );
                }
            }
        }

        Error_Page::get_instance();
    }
