<?php

    namespace Pixebuilder\Element;
    
    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    class Comment_Number extends Item{

        static function get_type(){
            return 'post';
        }

        static function get_name(){
            return 'comment-number';
        }

        static function get_title(){
            return esc_html__('Comment Number', 'pixebuilder');
        }

        static function get_icon(){
            return 'comment';
        }
        
        static function render_content($settings){
            
            // get date and post id
            $post_id = get_the_ID();
            if( empty($post_id) && pixe_is_admin() ){
                global $pixe_request_type, $pixe_request_id;
                if( $pixe_request_type == 'post' && !empty($pixe_request_id) ){
                    $post_id = $pixe_request_id;
                }
            }

            // data attributes
            $atts = array();
            if( !empty($settings['typo']['preset']) ){
                $atts['typo-preset'] = $settings['typo']['preset'];
            }

            if( empty($settings['comment']['hide-text']) ){
                $comment_count = get_comments_number_text(false, false, false, $post_id);
            }else{
                $comment_count = get_comments_number($post_id);
            }

            // print
            $ret = '';

            // icon
            if( empty($settings['icon']['icon']) ){
                $icon = pixe_icon_preset('comment');
            }else{
                $icon = pixe_get_icon($settings['icon']['icon']);
            }

            $ret .= '<div class="pixe-detail" >';

            // left icon
            if( !empty($icon) && (empty($settings['icon']['position']) || $settings['icon']['position'] == 'left') ){
                $ret .= $icon;
            }
            
            // date
            $ret .= '<a class="pixe-inline" ';
            $ret .= ' href="' . esc_url(get_comments_link()) . '" ';
            $ret .= pixe_get_data_atts($atts);
            $ret .= '>' . $comment_count . '</a>';

            // right icon
            if( !empty($icon) && !empty($settings['icon']['position']) && $settings['icon']['position'] == 'right' ){
                $ret .= $icon;
            }

            $ret .= '</div>';

            return $ret;
        }

    }

    \Pixe_Builder::register_element('Pixebuilder\Element\Comment_Number');
    