<?php
    /**
     * Base class for meta
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Admin;
    use WP_Term;
    use WP_Post;

    if( ! class_exists( __NAMESPACE__ . '\\Base_Meta' ) ) {
        /**
         * Base_Meta class
         */
        class Base_Meta {
            /**
             * Render html
             * 
             * @param array $layouts    An array of layouts
             * @param string $layout    Currently selected layout
             * @param string $field_key    Field key
             * @since 1.0.0
             */
            protected function get_html( $layouts, $layout, $field_key, $update ) {
                $wrapper_tag = $update ? 'td' : 'div';
                echo '<'. esc_attr( $wrapper_tag ) .' class="layouts">';
                    if( ! empty( $layouts ) && is_array( $layouts ) ) :

                        foreach( $layouts as $layout_id => $layout_args ) :
                            $is_checked = checked( $layout, $layout_id, false );
                            $class = 'layout';
                            if( $is_checked ) $class .= ' active';
                            ?>
                                <div class="<?php echo esc_attr( $class ); ?>">

                                    <input 
                                        type = "radio"
                                        name = "<?php echo esc_attr( $field_key ); ?>"
                                        id = "<?php echo esc_attr( $layout_id ); ?>"
                                        value = "<?php echo esc_attr( $layout_id ); ?>"
                                        <?php echo esc_attr( $is_checked ); ?>
                                    >

                                    <label for="<?php echo esc_attr( $layout_id ); ?>">

                                        <img src="<?php echo esc_url( $layout_args[ 'url' ] ); ?>" alt="<?php echo esc_attr( $layout_args[ 'label' ] ); ?>" loading="lazy" />

                                        <span class="label"><?php echo esc_html( $layout_args[ 'label' ] ); ?></span>

                                    </label>

                                </div>
                            <?php
                        endforeach;

                    endif;
                echo '</' . esc_attr( $wrapper_tag ) . '>';
            }

            /**
             * Render sidebar layout html
             * 
             * @param WP_Post $post The current post
             * @since 1.0.0
             */
            protected function get_sidebar_layouts_html( $queried_object, $meta_key, $update = false ) {
                $layout = 'customizer-sidebar';
                $is_taxonomy = in_array( $queried_object, [ 'category', 'post_tag' ] );
                
                if( $queried_object instanceof WP_Term ) {
                    $id = $queried_object->term_id;
                    $layout = get_term_meta( $id, $meta_key, true ) ?: 'customizer-sidebar';
                }

                if( $queried_object instanceof WP_Post ) {
                    $id = $queried_object->ID;
                    $layout = get_post_meta( $id, $meta_key, true ) ?: 'customizer-sidebar';
                }

                $layouts = [
                    'customizer-sidebar'    =>  [
                        'label' =>  __( 'Customizer', 'blog-bee' ),
                        'url' =>    get_template_directory_uri() . '/inc/customizer/assets/images/customizer.webp',
                    ],
                    'none'  =>  [
                        'label' =>  esc_html__( 'No Sidebars', 'blog-bee' ),
                        'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/no-sidebar.webp'
                    ],
                    'left'  =>  [
                        'label' =>  esc_html__( 'Left Sidebar Only', 'blog-bee' ),
                        'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/left-sidebar.webp'
                    ],
                    'right'  =>  [
                        'label' =>  esc_html__( 'Right Sidebar Only', 'blog-bee' ),
                        'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/right-sidebar.webp'
                    ],
                    'both'  =>  [
                        'label' =>  esc_html__( 'Both Sidebars', 'blog-bee' ),
                        'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/both-sidebars.webp'
                    ],
                    'both-left'  =>  [
                        'label' =>  esc_html__( 'Both on Left', 'blog-bee' ),
                        'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/both-left-sidebar.webp'
                    ],
                    'both-right'  =>  [
                        'label' =>  esc_html__( 'Both on Right', 'blog-bee' ),
                        'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/both-right-sidebar.webp'
                    ],
                ];
                if( $update ) :
                    ?>
                        <tr class="bbe-meta-field sidebar-layout form-field">

                            <th class="heading"><?php esc_html_e( 'Sidebar Layouts', 'blog-bee' ); ?></th>

                            <?php $this->get_html( $layouts, $layout, $meta_key, $update ); ?>

                        </tr>
                    <?php
                else :
                    ?>
                        <div class="bbe-meta-field sidebar-layout">

                            <h2 class="heading"><?php esc_html_e( 'Sidebar Layouts', 'blog-bee' ); ?></h2>

                            <?php $this->get_html( $layouts, $layout, $meta_key, $update ); ?>

                        </div>
                        
                    <?php
                endif;
            }
        }
    }