<?php

  namespace SocialStreams;

  class Shortcode
  {

    public function __construct()
    {
      add_action('init', array( $this, 'init__register_shortcodes'));

    }/* __construct() */


    public function init__register_shortcodes()
    {

      add_shortcode('social-streams', array( $this, 'create_socialstreams_shortcode') );
    }

    public function create_socialstreams_shortcode()
    {
      ob_start();
      $this->display_socialstreams();
      $output_string=ob_get_contents();;
      ob_end_clean();

      return $output_string;
    }

    public function display_socialstreams()
    {
      ?>

      <?php

      $socialstreams = $this->get_socialstreams();

      foreach ( $socialstreams as $ssPost ){
        // \BW\Debug::pre( $sspost );
        // \SocialStreams\Utils::render('socialstream-post-widget', array( 'sspost' => $sspost ) );
        ?>
        <div class="grid-item <?php echo $ssPost->classes() ?>" data-id="<?php echo $ssPost->order_id() ?>" <?php echo ($ssPost->has_media())?"style='background-image:url(".$ssPost->image().")'":""; ?>>
						<?php if ( !$ssPost->has_media() ): ?>
            <div class="social-media-post-content">
              <div class="ss-post-content-inner">
                <?php echo $ssPost->formatted_caption() ?>
              </div>
              <a href="<?php echo $ssPost->permalink() ?>" class="ss-post-time" target="_blank">
                <span data-livestamp="<?php echo $ssPost->timestamp() ?>"></span>
              </a>
            </div><!-- .ss-post-content -->
						<?php endif; ?>

            <div class="social-media-post-footer">
              <!--<a href="<?php echo $ssPost->user_link() ?>" class="ss-avatar" target="_blank"> <img src="<?php echo $ssPost->avatar() ?>"/> </a>-->
              <?php if ( $ssPost->has_media() ): ?>
              <div class="text-with-image">
              	<?php echo $ssPost->formatted_caption() ?>
              </div>
              <?php endif; ?>
              <a href="<?php echo $ssPost->user_link() ?>" class="social-media-nick" target="_blank">@<?php echo $ssPost->screen_name() ?></a>
              <span class="ss-social-icon"><i class="ss-icon-<?php echo $ssPost->service_name() ?>"></i></span>
            </div><!-- .ss-post-footer -->

        </div><!-- .grid-item -->

      <?php  }  // end foreach ?>



      <?php

    } // create_socialstreams_widget

    public function get_socialstreams()
    {
      global $post;
      /**
       * Call JSONAPI from PHP  - load top 24 posts
       */
      $args = array(
        'status' => 'publish',
        'perPage' => 24,
        'page' => 1,
        'request_id' => 'socialstreams_top'
      );

      //if archive page only show 6 posts( no infinitescroll )
      if ( !is_single($post) && !is_page($post) )
        $args['perPage'] = 6;

      \SocialStreams\JsonAPI\Utils::call( 'get', '1/site/options/socialstreams_app_settings');
      $response = \SocialStreams\JsonAPI\Utils::call( 'get', '1/site/socialstreams', $args );

      if ( isset( $response['data'] ) && !empty( $response['data'] ) )
      {
        foreach ( $response['data'] as $postData )
        {
          $posts[] = new SSPost( $postData );
        }
      }

      return $posts;

    }/* get_socialstreams() */

  }/* class Cron() */
