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

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

class EgpsCreatePages {

	private $page_array;


	function __construct(){
		register_activation_hook( EGPS_BASE_FILE, [$this, 'egps_create_pages'] );

		$this->page_array = array(
			'photoalbum', 
			'camera', 
			'tag', 
			'checkout', 
			'userupdate', 
			'useraccount', 
			'paypalerror', 
			'thankyoupage', 
			'cart', 
			'forgot-password', 
			'signup', 
			'login'
		);

		add_filter( 'theme_page_templates', [$this, 'egps_create_frontend_templates'], 10, 4);
		add_filter( 'template_include', [$this, 'egps_add_frontend_templates'] );

		add_filter( 'wp_nav_menu_items', [$this, 'egps_add_cart_to_main_menu'], 10, 2 );

	}

	function egps_create_frontend_templates( $post_templates, $wp_theme, $post, $post_type ) {

		// Add custom template to select dropdown
		$pages = $this->page_array;
	
		foreach($pages as $p){
			//$post_templates['egps_' . $p . '.php'] = __('EGPS ' . ucfirst($p));
		}
	
		//$post_templates['egps_photoalbum.php'] = __('photoalbum');
		$post_templates['frontend_gallery.php'] = 'EGPS Gallery';
	
		return $post_templates;
	}

	function egps_create_pages(){

		global $wpdb;

		$pages = $this->page_array;

		foreach ( $pages as $page ) {

			//$shortcode = '[egps_' . $page . ']';
			//$shortcode = '<div id="egps_shcode_'.$page.'">Content</div>';
			$shortcode = '<div id="egps_shcode_'.$page.'"></div>';

			$title = '' . ucfirst( $page );

			// Check if this page already exists, with shortcode
			$existing_page = get_page_by_title( $title );

			if ( empty( $existing_page )){
				// If the page doesn't exist, create it
				if($page == 'photoalbum'){
					$new_page = array(
						'post_title'    => $title,
						'post_name'		=> 'photoalbum',
						'post_content'  => $shortcode,
						'post_status'   => 'publish',
						'post_type'     => 'page',
						'comment_status' => 'closed'
					);
				}else{
					$new_page = array(
						'post_title'    => $title,
						'post_name'		=> 'egps' . $page,
						'post_content'  => $shortcode,
						'post_status'   => 'publish',
						'post_type'     => 'page',
						'comment_status' => 'closed'
					);
				}

				$new_id = EgpsDatabase::egps_insert_to_db("posts", $new_page);

				if($new_id != null){
					// set template for frontend page
					$tmp_data = array(
						'post_id' 		=> $new_id,
						'meta_key' 		=> '_wp_page_template',
						//'meta_value' 	=> 'egps_' . $page . '.php'
						'meta_value' 	=> 'frontend_gallery.php'
					);

					EgpsDatabase::egps_insert_to_db("postmeta", $tmp_data);
				}

			}
		}
		
	}
	
	function egps_add_frontend_templates( $template ) {

		foreach($this->page_array as $page){
			$url_slug = strpos(get_page_template_slug(), $page);

			if($url_slug !== false){
				$template = dirname(dirname(__FILE__)) . '/public/pages/egps_' . $page . '.php';
			}
		}

		return $template;
	}

	function egps_add_cart_to_main_menu ( $items, $args ) {

		if (str_contains($args->theme_location, "primary")) {

			$items .= '<li id="egps_photoalbum"><a href="' .site_url(). '/photoalbum"> Photoalbum </a></li>';

			if(Egps::egps_chk_lic()){
				$items .= '<li id="egps_cart_menu_item"> 
			
					<a href="'. site_url() .'/egpscart">Cart &nbsp;
						<span id="egps_cart_badge" className="badge badge-pill badge-danger" color="secondary">0</span>		  
					</a>
				</li>';

				$items .= '<li id="egps_user_login"><a href="' .site_url(). '/egpslogin"> Account </a></li>';

			}
		}
		return $items;
	}
	

}

new EgpsCreatePages();