<?php get_header(); ?>
<?php the_post(); ?>
<?php
/**
 * user edit ajax system
 * =====================
 * passwords strenght meter taken from:
 * https://code.tutsplus.com/articles/using-the-included-password-strength-meter-script-in-wordpress--wp-34736
 * https://github.com/WordPress/WordPress/blob/master/wp-admin/js/user-profile.js#L223
 * ajax idea:
 * https://wordpress.stackexchange.com/questions/274778/updating-user-profile-with-ajax-not-working
 * form field validation:
 * https://itnext.io/https-medium-com-joshstudley-form-field-validation-with-html-and-a-little-javascript-1bda6a4a4c8c
 * page template idea:
 * https://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-the-front-end
 */
  $customPostsMeta = get_post_custom();
  $subheadline = $customPostsMeta['_subheadline'][0];
  $show_profile = is_user_logged_in();
  $content_class = 'content clearfix';
  if ( $show_profile ) {
    $content_class .= ' content-profile';


    /**
     * @user collect user data to show
     */
    $current_user = wp_get_current_user();
    $current_user_favorites = get_user_meta($current_user->ID, '_favorite_posts', true);
    $favorites = false;
    if ( !empty($current_user_favorites) ) {
      $favorites = new WP_Query(array(
        'post_type' => 'any',
        'post__in' => $current_user_favorites,
      ));
    }
    $current_users_comments = get_comments(array(
      'user_id' => $current_user->ID,
    ));
    $current_users_comments_number = count($current_users_comments);
    $has_comments = $current_users_comments_number > 0;
    $comment_item_args = array(
	    'avatar_size' => 150,
    );
    $comment_item_depth = 0;
    $edit_data_attributes = ' data-edit="' . __('Bearbeiten', 'sandbox') . '" data-save="' . __('Speichern', 'sandbox') . '" data-saving="' . __('Speichert', 'sandbox') . '" data-saved="' . __('Gespeichert', 'sandbox') . '" data-generate="' . __('Passwort generieren', 'sandbox') . '"';
    $edit_box = '<div class="user_edit"' . $edit_data_attributes . '><a href="#%s">' . __('Bearbeiten', 'sandbox') . '</a><div class="field">%s</div></div>';
    $input_pattern = ' pattern="[A-Za-z0-9 ]{3,32}"';
    $input_title = ' title="' . __('Nur Buchstaben oder Zahlen erlaubt') . '"';
    $input_pattern_nickname = ' pattern="[a-zA-Z0-9-_ ]{3,32}"';
    $input_title_nickname = ' title="' . __('Nur Buchstaben, Zahlen, Binde- und Unterstriche erlaubt') . '"';
    $input_pattern_password = ' pattern="^[^\\\\]*$"';
    $input_title_password = ' title="' . __('Alle Zeichen außer "\" sind erlaubt.') . '"';


    /**
     * Taken from WordPress
     * https://github.com/WordPress/WordPress/blob/master/wp-admin/user-edit.php#L462
     */
    $display_names = array();
    $display_names['display_nickname'] = $current_user->nickname;
    $display_names['display_username'] = $current_user->user_login;

    if ( !empty($current_user->first_name) ) {
      $display_names['display_firstname'] = $current_user->first_name;
    }
    if ( !empty($current_user->last_name) ) {
      $display_names['display_lastname'] = $current_user->last_name;
    }
    if ( !empty($current_user->first_name) && !empty($current_user->last_name) ) {
      $display_names['display_firstlast'] = $current_user->first_name . ' ' . $current_user->last_name;
      $display_names['display_lastfirst'] = $current_user->last_name . ' ' . $current_user->first_name;
    }
    if ( !in_array( $current_user->display_name, $display_names) ) { // Only add this if it isn't duplicated elsewhere.
      $display_names = array('display_displayname' => $current_user->display_name) + $display_names;
    }
    $display_names = array_map('trim', $display_names);
    $display_names = array_unique($display_names);
  }
  // echo var_dump($current_user->first_name);
?>
<?php if ( $show_profile ) { ?>
  <article id="content" <?php post_class(array('wrapper', 'container')) ?>>
    <header class="content-header">
      <figure class="avatar"><?php echo get_avatar($current_user->ID); ?></figure>
      <div class="inner">
        <h1 class="title"><?php echo $current_user->data->display_name; ?></h1>
      <?php if ( !empty($subheadline) ) { ?>
        <div class="subheadline">
          <?php echo $subheadline; ?>
        </div>
      <?php } ?>
      </div>
      <?php
        /**
         * @Sidebar Content
         */
        get_sidebar('content-header'); ?>
    </header>
    <div class="content content-profile-name clearfix">
      <?php
        /**
         * @Sidebar Meta
         */
        get_sidebar('meta'); ?>
      <h3><?php echo __('Ihre Daten', 'sandbox'); ?></h3>
      <dl class="profile-data user_edit-disabled">
        <dt class="user_login"><?php echo __('Benutzername', 'sandbox'); ?></dt>
        <dd id="profile-user_login">
          <div class="current"><?php echo $current_user->user_login; ?></div>
        </dd>
        <dt class="first_name"><?php echo __('Vorname', 'sandbox'); ?></dt>
        <dd id="profile-first_name" class="user_edit"<?php echo $edit_data_attributes; ?>>
          <div class="field">
            <input type="text" name="first_name" id="first_name"<?php echo $input_pattern; ?><?php echo $input_title; ?> placeholder="<?php echo $current_user->first_name; ?>" disabled />
            <button class="abort" title="<?php echo __('Abbrechen', 'sandbox'); ?>"></button>
          </div>
          <div class="current"><?php echo $current_user->first_name; ?></div>
          <label class="edit_link" for="first_name" data-mode="edit"><?php echo __('Bearbeiten', 'sandbox'); ?></label>
        </dd>
        <dt class="last_name"><?php echo __('Nachname', 'sandbox'); ?></dt>
        <dd id="profile-last_name" class="user_edit"<?php echo $edit_data_attributes; ?>>
          <div class="field">
            <input type="text" name="last_name" id="last_name"<?php echo $input_pattern; ?><?php echo $input_title; ?> placeholder="<?php echo $current_user->last_name; ?>" disabled />
            <button class="abort" title="<?php echo __('Abbrechen', 'sandbox'); ?>"></button>
          </div>
          <div class="current"><?php echo $current_user->last_name; ?></div>
          <label class="edit_link" for="last_name" data-mode="edit"><?php echo __('Bearbeiten', 'sandbox'); ?></label>
        </dd>
        <dt class="nickname"><?php echo __('Spitzname', 'sandbox'); ?></dt>
        <dd id="profile-nickname" class="user_edit"<?php echo $edit_data_attributes; ?>>
          <div class="field">
            <input type="text" name="nickname" id="nickname"<?php echo $input_pattern_nickname; ?><?php echo $input_title_nickname; ?> placeholder="<?php echo $current_user->nickname; ?>" disabled />
            <button class="abort" title="<?php echo __('Abbrechen', 'sandbox'); ?>"></button>
          </div>
          <div class="current"><?php echo $current_user->nickname; ?></div>
          <label class="edit_link" for="nickname" data-mode="edit"><?php echo __('Bearbeiten', 'sandbox'); ?></label>
        </dd>
        <dt class="display_name"><?php echo __('Anzeigename', 'sandbox'); ?></dt>
        <dd id="profile-display_name" class="user_edit"<?php echo $edit_data_attributes; ?>>
          <div class="field">
            <select name="display_name" id="display_name" disabled>
            <?php foreach ( $display_names as $key => $value ) { ?>
              <option <?php selected($current_user->display_name, $value); ?>><?php echo $value; ?></option>
            <?php } ?>
            </select>
            <button class="abort" title="<?php echo __('Abbrechen', 'sandbox'); ?>"></button>
          </div>
          <div class="current"><?php echo $current_user->display_name; ?></div>
          <label class="edit_link" for="display_name" data-mode="edit"><?php echo __('Bearbeiten', 'sandbox'); ?></label>
        </dd>
        <dt class="pass1"><?php echo __('Passwort', 'sandbox'); ?></dt>
        <dd id="profile-pass1" class="user_edit"<?php echo $edit_data_attributes; ?>>
          <div class="field">
            <input type="password" name="pass1" id="pass1" placeholder="<?php echo __('Neues Passwort eingeben'); ?>" value="" autocomplete="off" data-pw="<?php echo esc_attr(wp_generate_password(24)); ?>"<?php echo $input_pattern_password; ?><?php echo $input_title_password; ?> />
            <button class="hide" title="<?php echo __('Verbergen', 'sandbox'); ?>"></button>
            <button class="abort" title="<?php echo __('Abbrechen', 'sandbox'); ?>"></button>
          </div>
          <div class="current">*****</div>
          <label class="edit_link" for="pass1" data-mode="generate"><?php echo __('Passwort generieren', 'sandbox'); ?></label>
          <div class="strength-meter"><?php echo __('Passwortstärke', 'sandbox'); ?>: <span></span></div>
        </dd>
      </dl>
    </div>

  <?php if ( $favorites && $favorites->have_posts() ) { ?>
    <div class="content content-profile-favorites">
      <h3><?php echo __('Sie haben', 'sandbox'); ?> <?php printf(_n('<span>ein</span> Lernmaterial', '<span>%s</span> Lernmaterialien', $favorites->post_count, 'sandbox'), number_format_i18n($favorites->post_count));?> <?php echo __('favorisiert', 'sandbox'); ?></h3>
      <ol class="favorites-list profile-list">
    <?php while ( $favorites->have_posts() ) {
      $favorites->the_post();
      $favoriteMeta = get_post_custom();
      $favorite_id = get_the_ID();
      $file_url = $favoriteMeta['_alpha_file_url'][0];
      $file_size = $favoriteMeta['_alpha_file_size'][0];
      $file_count = !empty($favoriteMeta['_alpha_file_count'][0]) ? intval($favoriteMeta['_alpha_file_count'][0]) : 0;
      $file_options = unserialize($favoriteMeta['_alpha_file_options'][0]);
      $description = $favoriteMeta['_download-description'][0];
      $exercise = $favoriteMeta['_download-challenge'][0];
      $author = $favoriteMeta['_download-author'][0];
      $license = $favoriteMeta['_download-license'][0];
      $favorite_post_type = get_post_type();
    ?>
        <li id="favorite-<?php echo $favorite_id; ?>" <?php post_class('comment'); ?>>
          <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        </li>
    <?php } ?>
    <?php wp_reset_postdata(); ?>
      </ol>
    </div>
  <?php } else {?>
    <div class="content content-profile-favorites">
      <h3><?php echo __('Sie haben noch keine Lernmaterialien favorisiert', 'sandbox'); ?></h3>
      <a href="<?php echo get_post_type_archive_link('alpha_download') ?>" class="more"><?php echo __('Zu den Lernmaterialien', 'sandbox'); ?></a>
    </div>
  <?php } ?>
  <?php if ( $has_comments ) { ?>
    <div class="content content-profile-comments">
      <h3><?php echo __('Sie haben', 'sandbox'); ?> <?php printf(_n('<span>eine</span> Bewertung', '<span>%s</span> Bewertungen', $current_users_comments_number, 'sandbox'), number_format_i18n($current_users_comments_number));?> <?php echo __('verfasst', 'sandbox'); ?></h3>
      <ol class="comment-list profile-list">
      <?php foreach ( $current_users_comments as $comment ) {
        if ( !isset($GLOBALS['comment']) ) {
          $GLOBALS['comment'] = $comment;
        }
        $comment_post = get_post($comment->comment_post_ID);
      ?>
        <li id="comment-<?php comment_ID(); ?>" <?php comment_class( $comment->has_children ? 'parent' : '', $comment ); ?>>
          <h4><?php echo __('Verfasst zu'); ?> <a href="<?php the_permalink($comment->comment_post_ID); ?>"><?php echo get_the_title($comment->comment_post_ID); ?></a> <?php echo __('am'); ?> <?php printf( __('%1$s at %2$s'), get_comment_date('', $comment ), get_comment_time() ); ?></h4>
          <?php if ('0' == $comment->comment_approved ) : ?>
            <p class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.'); ?></p>
          <?php endif; ?>

          <?php echo apply_filters('comment_text', $comment->comment_content, $comment, $args); ?>
        </li>
      <?php } ?>
      </ol>
    </div>
  <?php } ?>
  </article><!-- .post -->
<?php } else { ?>
  <?php the_content() ?>
  <?php wp_link_pages('before=<div class="page-link">' . __('Pages:', 'sandbox') . '&after=</div>') ?>
<?php } ?>
<?php get_footer() ?>
