/**
 * Register all shortcodes for the plugin
 *
 * @package    Wp_Connect_Strava
 * @subpackage Wp_Connect_Strava/includes
 * @author     Jan Henckens <*****@*****.**>
 */
function connect_strava_register_shortcode_embed($atts)
{
    require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-wp-connect-strava-api.php';
    $activityId = $atts['activity'];
    $transient = 'strava_connect_' . $activityId;
    if (get_option('connect_strava_user_data')) {
        if (get_transient($transient)) {
            $activityEmbedToken = get_transient($transient);
        } else {
            $strava = new Wp_Connect_Strava_Api();
            $activityEmbedToken = $strava->getActivity($activityId);
            set_transient($transient, $activityEmbedToken, 1 * HOUR_IN_SECONDS);
        }
        $activityEmbedUrl = 'https://www.strava.com/activities/' . $activityId . '/embed/' . $activityEmbedToken;
        if (get_option('connect_strava_user_data')) {
        }
        ob_start();
        ?>
        <iframe
            height='405'
            width='590'
            frameborder='0'
            allowtransparency='true'
            scrolling='no'
            src='<?php 
        echo $activityEmbedUrl;
        ?>
'>
        </iframe>
        <?php 
        return ob_get_clean();
    } else {
        ob_start();
        ?>
        <a href="https://www.strava.com/activities/<?php 
        echo $activityId;
        ?>
">View my activity on Strava</a>
        <?php 
        return ob_get_clean();
    }
}
    /**
     * Renders the widget on the frontend of the website
     * @param $args
     * @param $instance
     */
    public function widget($args, $instance)
    {
        $title = apply_filters('widget_title', $instance['title']);
        $before_title = "<div class='widget-title'>";
        $after_title = "</div>";
        if (get_option('connect_strava_user_data')) {
            require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-wp-connect-strava-api.php';
            $strava = new Wp_Connect_Strava_Api();
            $stats = $strava->getStats();
            $athlete = json_decode(get_option('connect_strava_user_data'));
            extract($args);
            if ($athlete->measurement_preference === "meters") {
                $unit = "km";
            } else {
                $unit = "mi";
            }
            ?>
            <aside id="wp-connect-strava" class="widget wp-connect-strava">
                <?php 
            if ($title) {
                echo $before_title . $title . $after_title;
            }
            ?>
                <?php 
            if ($instance['show_avatar'] === "on") {
                ?>
                    <img src="<?php 
                echo $athlete->profile;
                ?>
">
                <?php 
            }
            ?>
                <?php 
            if ($instance['show_ride_stats'] === "on") {
                ?>
                    <p>Rides this year: <?php 
                echo $stats->ytd_ride_totals->count;
                ?>
</p>
                    <p>All time
                        distance: <?php 
                echo number_format($stats->all_ride_totals->distance, 0, '.', ',') . $unit;
                ?>
</p>php
                <?php 
            }
            ?>
                <?php 
            if ($instance['show_run_stats'] === "on") {
                ?>
                    <p>Runs this year: <?php 
                echo $stats->ytd_run_totals->count;
                ?>
                    </p>
                <?php 
            }
            ?>

            </aside>
            <?php 
        } else {
            if (current_user_can('manage_options')) {
                ?>
                <aside id="wp-connect-strava" class="widget wp-connect-strava">
                    <?php 
                if ($title) {
                    echo $before_title . $title . $after_title;
                }
                ?>
                    <?php 
                $url = admin_url() . 'options-general.php?page=strava_connect';
                $link = sprintf(wp_kses(__('Connect Strava is not configured, click  <a href="%s">here</a> to connect your Strava account. (only you will see this message)', 'wp-connect-strava'), array('a' => array('href' => array()))), esc_url($url));
                echo $link;
                ?>
                </aside>
            <?php 
            }
        }
    }