<?php
/**
 * Custom walker class to output markup for UIkit v3 comment component.
 */

if ( ! class_exists( 'Basho_Walker_Comment' ) ) {
    /**
	 * A custom walker for WordPress comments.
	 */
    class Basho_Walker_Comment extends Walker_Comment {

        public $tree_type = 'comment';

        public $db_fields = array(
            'parent' => 'comment_parent',
            'id' => 'comment_ID'
        );

    	// constructor: wrapper for the comments list
    	public function __construct() { ?>

    		<div class="comments-list">

    	<?php }

    	// start_lvl: wrapper for child comments list
    	public function start_lvl( &$output, $depth = 0, $args = array() ) {
    		$GLOBALS['comment_depth'] = $depth + 2; ?>

    		<div class="child-comments comments-list uk-margin-medium-left">

    	<?php }

    	// end_lvl: closing wrapper for child comments list
    	public function end_lvl( &$output, $depth = 0, $args = array() ) {
    		$GLOBALS['comment_depth'] = $depth + 2; ?>

    		</div>

    	<?php }

    	// start_el: HTML for comment template
    	public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
    		$depth++;
    		$GLOBALS['comment_depth'] = $depth;
    		$GLOBALS['comment'] = $comment;
    		$parent_class = ( empty( $args['has_children'] ) ? '' : 'parent' );

    		if ( 'article' == $args['style'] ) {
    			$tag = 'article';
    			$add_below = 'comment';
    		} else {
    			$tag = 'article';
    			$add_below = 'comment';
    		} ?>

    		<article <?php comment_class( empty( $args['has_children'] ) ? 'uk-comment uk-margin-medium-top uk-margin-medium-bottom' : 'uk-comment uk-margin-medium-top uk-margin-medium-bottom parent' ) ?> id="comment-<?php comment_ID() ?>" itemscope itemtype="http://schema.org/Comment">
                <div class="uk-card">
                    <header class="uk-comment-header uk-grid-medium uk-flex-middle" data-uk-grid>
                        <?php if ( get_option('show_avatars') === '1' ) : ?>
                            <div class="uk-width-auto">
                                <?php echo get_avatar( $comment, 38, get_option( 'avatar_default', 'mystery' ), __( 'Authors gravatar', 'basho' ), array( 'class' => 'uk-comment-avatar uk-border-circle' ) ); ?>
                            </div>
                        <?php endif; ?>
                        <div class="uk-width-expand">
                            <h4 class="uk-comment-title uk-margin-remove">
                                <a class="uk-link-reset" href="<?php comment_author_url(); ?>"><?php comment_author(); ?></a>
                            </h4>
                            <ul class="uk-comment-meta uk-subnav">
                                <li>
                                    <time class="comment-meta-item" datetime="<?php comment_date('Y-m-d') ?>T<?php comment_time('H:iP') ?>" itemprop="datePublished"><?php comment_date('j F Y') ?>, <a class="uk-link-reset" href="#comment-<?php comment_ID() ?>" itemprop="url"><?php comment_time() ?></a></time>
                                </li>
                                <?php if ( get_option('thread_comments') === '1' ) : ?>
                                    <?php if ( comments_open() ) : ?>
                                        <li>
                                            <?php comment_reply_link( array_merge( $args, array('class' => 'uk-link-muted', 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
                                        </li>
                                    <?php endif; ?>
                                <?php endif; ?>
                                <?php if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) : ?>
                                    <li>
                                        <?php edit_comment_link( __( 'Edit', 'basho' ) ); ?>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </div>
                    </header>
                    <div class="uk-comment-body" itemprop="text">
        				<?php comment_text(); ?>
                        <?php if ( $comment->comment_approved == '0' ) : ?>
                            <p class="comment-meta-item"><?php esc_html_e( 'Your comment is awaiting moderation.', 'basho' ); ?></p>
                        <?php endif; ?>
        			</div>
                </div>

    	<?php }

    	// end_el: closing HTML for comment template
    	public function end_el( &$output, $comment, $depth = 0, $args = array() ) { ?>

            </article>

    	<?php }

    	// destructor: closing wrapper for the comments list
    	public function __destruct() { ?>

            </div>

    	<?php }

    }
}
