<?php
    /**
     * Preloader 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_attr;
    use function wp_kses_post;
    use function wp_get_attachment_image;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'preloader_section' );
                $this->add_control( 'preloader_section_tab' );
                $this->add_control( 'preloader_option' );
                $this->add_control( 'preloader_style' );
                $this->tab = 'design';
                $this->add_control( 'preloader_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'preloader_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'preloader_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'preloader_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'preloader_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 = '' ) {
                $enable_rule = [
                    'setting'   =>  'preloader_option',
                    'operator'  =>  '==',
                    'value' =>  true                
                ];
                $controls = [
                    'preloader_section' =>  [
                        'title' =>  esc_html__( 'Preloader', 'blog-bee' ),
                        'panel' =>  'global_panel',
                        'priority'  =>  20
                    ],
                    'preloader_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'preloader_option' =>  [
                        'label' =>  esc_html__( 'Enable Preloader', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'preloader_style' =>  [
                        'label' =>  esc_html__( 'Style', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  [
                            'spinner'   =>  esc_html__( 'Spinner', 'blog-bee' ),
                            'bars'   =>  esc_html__( 'Bars', 'blog-bee' ),
                        ],
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                $enable_rule,
                            ]
                        ]
                    ],
                    'preloader_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'preloader_section_tab' =>  'general',
                    'preloader_option' =>  false,
                    'preloader_style' =>  'spinner',
                    'preloader_color' =>  $this->get_color(),
                ] );
            }

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

            /*
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                if( ! Theme_Defaults::get_theme_option( 'preloader_option' ) ) return;
                $block_class[] = self::$id;
                $style = Theme_Defaults::get_theme_option( 'preloader_style' );
                $block_class[] = 'style--' . $style;
                $block_class[] = 'type--default';
                $content = '<div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div>';
                $delay = 3;
                $first_visit = false;
                $data_attrs = [
                    'data-delay=' . $delay,
                    'data-first-visit=' . $first_visit,
                ];

                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>" <?php echo esc_attr( implode( ' ', $data_attrs ) ); ?>>
                        <?php
                            echo wp_kses_post( $content );
                        ?>
                    </div><!-- .bbe-preloader -->
                <?php
            }
        }
        Preloader::get_instance();
    }