<?php
    /**
     * Dark Mode 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 get_template_directory_uri;
    use function esc_attr;
    use function esc_html;

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

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'dark-mode' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'dark-mode' );
                $sticky_builder = Helpers::is_widget_in_builder( 'sticky_header_builder', 'dark-mode' );
                return ( $header_builder || $header_responsive_builder || $sticky_builder );
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'dark_mode_section' );
                $this->add_control( 'dark_mode_section_tab' );
                $this->add_control( 'dark_mode_icon_size' );
                $this->tab = 'design';
                $this->add_control( 'dark_mode_light_color' );
                $this->add_control( 'dark_mode_dark_color' );
                $this->add_control( 'dark_mode_light_background' );
                $this->add_control( 'dark_mode_dark_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'dark_mode_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'dark_mode_icon_size' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'dark_mode_light_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'dark_mode_dark_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'dark_mode_light_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'dark_mode_dark_background' =>  [
                        '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 = [
                    'dark_mode_section' =>  [
                        'title' =>  esc_html__( 'Dark Mode', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'dark_mode_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'dark_mode_icon_size'    =>  [
                        'label' =>  esc_html__( 'Icon Size', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'responsive'    =>  true,
                        'input_attrs'   =>  [
                           'px'    =>  [
                                'min'   =>  12,
                                'max'   =>  40,
                                'step'  =>  1
                            ],
                        ]
                    ],
                    'dark_mode_light_color' =>  [
                        'label' =>  esc_html__( 'Light Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                    ],
                    'dark_mode_dark_color' =>  [
                        'label' =>  esc_html__( 'Dark Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                    ],
                    'dark_mode_light_background' =>  [
                        'label' =>  esc_html__( 'Light Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'dark_mode_dark_background' =>  [
                        'label' =>  esc_html__( 'Dark Backround', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'dark_mode_section_tab' =>  'general',
                    'dark_mode_layouts' =>  'one',
                    'dark_mode_enable_labels' =>  false,
                    'dark_mode_dark_icon' =>  $this->get_icon_picker( [ 'value' => 'fa-solid fa-moon' ] ),
                    'dark_mode_light_icon' =>  $this->get_icon_picker( [ 'value' => 'fa-solid fa-sun' ] ),
                    'dark_mode_dark_text' =>  'Dark',
                    'dark_mode_light_text' =>  'Light',
                    'dark_mode_icon_size'    =>  $this->get_responsive( '15px', '15px', '15px' ),
                    'dark_mode_typography' =>  $this->get_typography([
                        'font_weight'   =>  '500',
                        'font_size' =>  $this->get_responsive( '14px', '14px', '14px' ),
                        'text_transform'    =>  'uppercase'
                    ]),
                    'dark_mode_light_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#30344b' ] ),
                        'hover' =>  $this->get_color()
                    ],
                    'dark_mode_dark_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#fff' ] ),
                        'hover' =>  $this->get_color()
                    ],
                    'dark_mode_light_background' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '' ] ),
                        'hover' =>  $this->get_color( [ 'value' => '' ] )
                    ],
                    'dark_mode_dark_background' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '' ] ),
                        'hover' =>  $this->get_color( [ 'value' => '' ] )
                    ],
                    'dark_mode_border_radius' =>  '0px',
                    'dark_mode_padding' =>  $this->get_dimension(),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_args = [
                    'dark_mode_icon_size'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'dark_mode_icon_size' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'dark_mode_icon_size' ),
                        'selector'  =>  'body .bbe-header .bbe-dark-mode .icon',
                        'property'  =>  'font-size'
                    ],
                    'dark_mode_light_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'dark_mode_light_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'dark_mode_light_color' ),
                        'selector'  =>  '.bbe-header .bbe-dark-mode .icon-wrapper.light-icon',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'dark_mode_dark_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'dark_mode_dark_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'dark_mode_dark_color' ),
                        'selector'  =>  '.bbe-header .bbe-dark-mode .icon-wrapper.dark-icon',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'dark_mode_light_background'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'dark_mode_light_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'dark_mode_light_background' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-dark-mode .icon-wrapper.light-icon, .bbe-light .bbe-header .bbe-dark-mode.layout--two .icon-wrapper.light-icon',
                        'hover_selector'  =>  '.bbe-light .bbe-header .bbe-dark-mode .icon-wrapper.light-icon, .bbe-light .bbe-header .bbe-dark-mode.layout--two',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'dark_mode_dark_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'dark_mode_dark_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'dark_mode_dark_background' ),
                        'selector'  =>  '.bbe-dark .bbe-header .bbe-dark-mode .icon-wrapper.dark-icon, .bbe-dark .bbe-header .bbe-dark-mode.layout--two .icon-wrapper.dark-icon',
                        'hover_selector'  =>  '.bbe-dark .bbe-header.site-header .bbe-dark-mode .icon-wrapper.dark-icon, .bbe-dark .bbe-header .bbe-dark-mode.layout--two',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $layout = Theme_Defaults::get_theme_option( 'dark_mode_layouts' );
                $block_class[] = self::$id;
                $block_class[] = 'header-widget';
                $block_class[] = 'layout--' . $layout;

                ?>

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

                        <?php

                            $this->get_light_icon();

                            $this->get_dark_icon();

                        ?>

                    </div><!-- .bbe-dark-mode -->

                <?php
            }

            /**
             * Dark Icon
             * 
             * @since 1.0.0
             */
            private function get_dark_icon() {
                $dark_icon = Theme_Defaults::get_theme_option( 'dark_mode_dark_icon' );
                $dark_icon[ 'tag' ] = 'span';
                $dark_icon[ 'tag' ] = 'button';
                $dark_icon[ 'aria_label' ] = esc_html__( 'Enable Dark Mode', 'blog-bee' );
                $dark_icon[ 'class' ] = 'dark-icon icon-wrapper';

                echo Helpers::get_icon_html( $dark_icon );
            }

            /**
             * Light Icon
             * 
             * @since 1.0.0
             */
            private function get_light_icon() {
                $light_icon = Theme_Defaults::get_theme_option( 'dark_mode_light_icon' );
                $light_icon[ 'tag' ] = 'span';
                $light_icon[ 'tag' ] = 'button';
                $light_icon[ 'class' ] = 'light-icon icon-wrapper';
                $dark_icon[ 'aria_label' ] = esc_html__( 'Enable Light Mode', 'blog-bee' );

                echo Helpers::get_icon_html( $light_icon );
            }
        }
        Dark_Mode::get_instance();
    }