<?php
/**
 * @package: EGPS - Easy Sell for Google Photo
 */

if(!defined('ABSPATH')){
    exit; // Exit if accessed directly
}


//wp_enqueue_style('egps_frontend_css', EGPS_BASE_PLUGIN . 'libs/css/egps_frontend.css', array(), null, true);
//wp_enqueue_script('egps_frontend_bootstrap_js', EGPS_BASE_PLUGIN . 'libs/bootstrap/bootstrap.min.js', array(), null, true);

get_header('full-width');

egps_load_frontend_style();

egps_load_js_script();

///////// put Firebase API data to localstorage
$firebase_options = get_option('egps_firebase_options');

if($firebase_options == false){
    $firebase_options = array();
}

$firebase_options['egps_site_url'] = site_url();

// Register the script
wp_register_script( 'some_handle', EGPS_BASE_PLUGIN . 'libs/js/egps_frontend.js', array(), EGPS_VERSION, false );

// Localize the script with new data
wp_localize_script( 'some_handle', 'object_name', $firebase_options );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

?>

    <?php
        // TO SHOW THE PAGE CONTENTS
        while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
            <div class="container-fluid"><div class="px-lg-5">
                <div class="entry-content-page">
                    <?php the_content(); ?> <!-- Page Content -->
                </div><!-- .entry-content-page -->
            </div></div><!-- .container-fluid -->
            <?php
        endwhile; //resetting the page loop
        wp_reset_query(); //resetting the page query

        wp_register_style('egps_bootstrap_icons', EGPS_BASE_PLUGIN . 'libs/bootstrap/bootstrap-icons.css', array(), EGPS_VERSION);
        wp_enqueue_style('egps_bootstrap_icons');

        global $wp;
        global $post;
        
        $post_slug = $post->post_name;

        $page_url_out = get_site_url();

        $options = get_option('egps_sitedesign');
        $sidebar_cameras = isset($options['egps_cameracloud_on']) ? true : false;
        $sidebar_tags = isset($options['egps_tagcloud_on']) ? true : false;
        $sidebar_egps = isset($options['egps_sidebar_on']) ? true : false;
        $sidebar_wp = isset($options['wp_sidebar_on']) ? true : false;
        $backtotop = isset($options['wp_backtotopbutton_on']) ? true : false;

        $egps_page_width = "";

        if( $sidebar_egps || $sidebar_wp || $sidebar_tags || $sidebar_cameras ){
            $egps_page_width = "col-xl-9 col-lg-9 col-md-8 mb-5 egps_float_to_left";
        }else{
            $egps_page_width = "col-xl-12 col-lg-12 col-md-4 mb-5";
        }
        
        echo '<div 
                class="' . esc_attr($egps_page_width) . '" 
                id="egps_plugin_root" 
                data-egps_photo_url="' . esc_attr($page_url_out) . '/" 
                data-egps_photo_suburl="'. esc_attr($post_slug) .'"
                ></div>'; //photoalbum


        if( $sidebar_egps || $sidebar_wp || $sidebar_tags || $sidebar_cameras ){
            echo '<div class="col-xl-3 col-lg-3 col-md-4 egps_float_to_left">';
        }


        //load albums list
        if( $sidebar_egps ){

            $albums_list_array = EgpsAlbums::egps_get_active_albums('egps_title', 'ASC', 'all_data');

            echo '<aside id="egps_albumlist_title" class="featured-sidebar widget-area">
                <section id="" class="egps_album_list_entries">
                    <div class="sidebar-headline-wrapper">
                        <div class="sidebarlines-wrapper">
                            <div class="widget-title-lines"></div>
                        </div>
                        <h2 class="widget-title">Albums</h2>
                    </div>
                <ul>';

            foreach ($albums_list_array as $item) {
                echo '<li class="list-unstyled">
                        <a 
                            href="' . esc_url($page_url_out . "/photoalbum/" . $item->egps_album_slug) . '" 
                            class="text-decoration-none" 
                            onClick="resetActualPageNumber()">' . esc_html($item->egps_title) . '</a>
                        </li>';
            }
            
            echo '</ul></section></aside>';
            
        }


        //load camera list
        if( $sidebar_cameras ){

            $camera_list_array = EgpsAlbums::egps_get_camera_cloud();

            echo '<aside id="egps_albumlist_title" class="featured-sidebar widget-area">
                <section id="" class="egps_album_list_entries">
                    <div class="sidebar-headline-wrapper">
                        <div class="sidebarlines-wrapper">
                            <div class="widget-title-lines"></div>
                        </div>
                        <h2 class="widget-title">Cameras</h2>
                    </div>
                <ul>';

            foreach ($camera_list_array as $item) {
                echo '<li class="list-unstyled">
                        <a 
                            href="' . esc_url($page_url_out . "/egpscamera/" . $item->egps_mediaCameraUrl) . '" 
                            class="text-decoration-none" 
                            onClick="resetActualPageNumber()">' . esc_html($item->egps_mediaCamera) . '</a>
                        </li>';
            }
            
            echo '</ul></section></aside>';
            
        }
        

        //load TAG cloud
        if( $sidebar_tags ){

            $tag_array = EgpsDatabase::egps_get_table_rows(
                "egps_tagcloud", 
                "egps_tag",
                "ASC"
            );

            echo '<aside id="egps_albumlist_title" class="featured-sidebar widget-area">
                <section id="" class="egps_album_list_entries">
                    <div class="sidebar-headline-wrapper">
                        <div class="sidebarlines-wrapper">
                            <div class="widget-title-lines"></div>
                        </div>
                        <h2 class="widget-title">TAG cloud</h2>
                    </div>
                <div class="">';

            foreach ($tag_array as $item) {
                $tagout = str_replace("#", "", $item->egps_tag);

                echo '<span>
                        <a 
                            href="' . esc_url($page_url_out . '/egpstag/' . $tagout) . '" 
                            class="text-decoration-none" 
                            onClick="resetActualPageNumber()">' . esc_html($tagout) . '</a>
                            <!--('. esc_attr($item->egps_tag_count) .')-->&nbsp;
                        </span>';
            }
            
            echo '</div></section></aside>';
            
        }
        

        //WP sidebar
        if( $sidebar_wp ){
            get_sidebar();
        }

        if( $sidebar_egps || $sidebar_wp || $sidebar_tags || $sidebar_cameras ){
            echo '</div>';
        }

        if( !$backtotop ){

            $css = '.egpsToTopButton{display: none !important;}';

            wp_register_style( 'egps_totop_btn_inline', false );
            wp_enqueue_style( 'egps_totop_btn_inline' );
            wp_add_inline_style( 'egps_totop_btn_inline', $css );

        }


get_footer();

