<?php
    /**
     * Background 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\Helpers as Helpers;

    use function esc_html__;
    use function esc_attr;

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

            /**
             * Constructor
             *
             * @since 1.0.0
             */
            public function __construct() {
                parent::__construct();
                add_action( 'wp_body_open', [ $this, 'render_html' ] );
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->section = 'background_image';
                $this->manager->get_section( 'background_image' )->title = esc_html__( 'Background', 'blog-bee' );
                $this->manager->get_control( 'background_color' )->section = 'background_image';
                $this->manager->get_control( 'background_color' )->priority = 20;
                $this->manager->remove_control( 'background_color' );
                $this->add_control( 'site_background_color' );
                $this->add_control( 'background_animation' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'site_background_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'background_animation' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'site_background_color'    =>  [
                        'label' =>  esc_html__( 'Background Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient']
                    ],
                    'background_animation' =>  [
                        'label' =>  esc_html__( 'Background Animation', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  [
                            'none' => esc_html__( 'None', 'blog-bee' ),
                            'one'  => esc_html__( 'Fading squares', 'blog-bee' ),
                            'three'  => esc_html__( 'Falling dots', 'blog-bee' ),  
                        ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'site_background_color'    =>  $this->get_color( [ 'value' => '#fff' ] ),
                    'background_animation'    =>  'none',
                    'background_animation_color'    =>  $this->get_color( [ 'value' => '#E78371' ] ),
                ] );
            }

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

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $animation = Theme_Defaults::get_theme_option( 'background_animation' );
                if( $animation === 'none' ) return;
                $block_class[] = self::$id;

                echo '<div class="' . esc_attr( implode( ' ', $block_class ) ) . '">';

                    for ( $i = 0; $i < 20; $i++ ) :

                        echo '<div class="item"></div>';

                    endfor;

                echo '</div>';
            }
        }
        Background::get_instance();
    }