<?php

/**
 * @file
 * Functions to support theming in the Cudau theme.
 */

use Drupal\Core\Template\Attribute;
use Drupal\taxonomy\Entity\Term;
use Drupal\block\Entity\Block;
use Drupal\Core\Url;

$theme_path = \Drupal::service('extension.list.theme')->getPath('ziston');

include_once $theme_path . '/includes/functions.php';
include_once $theme_path . '/includes/menu.php';
include_once $theme_path . '/includes/form.php';
include_once $theme_path . '/includes/oembed.php';
include_once $theme_path . '/includes/override.php';
include_once $theme_path . '/customize/fonts.php';

function ziston_preprocess_page(&$variables) {
	global $base_url;

	$current_theme = \Drupal::config('system.theme')->get('default');
	$sub_theme = '';
	$theme_handler = \Drupal::service('theme_handler');
	if($theme_handler->themeExists($current_theme)){
		$theme_handler = $theme_handler->getTheme($current_theme);
		$theme_info = $theme_handler->info;
		if(isset($theme_info['base theme']) && !empty($theme_info['base theme']) && strpos($theme_info['base theme'], 'avias_')){
			$sub_theme = $current_theme;
		}
	}

	$theme_path = \Drupal::service('extension.list.theme')->getPath('ziston');
	$variables['sticky_menu'] = theme_get_setting('sticky_menu');
	$variables['preloader'] = theme_get_setting('preloader');
	$variables['theme_path'] = $theme_path;
	
	$variables['footer_first_size'] = theme_get_setting('footer_first_size');
	$variables['footer_second_size'] = theme_get_setting('footer_second_size');
	$variables['footer_third_size'] = theme_get_setting('footer_third_size');
	$variables['footer_four_size'] = theme_get_setting('footer_four_size');

   $variables['hide_header_button_link'] = theme_get_setting('hide_header_button_link');
   $variables['header_button_link'] = theme_get_setting('header_button_link');
   $variables['header_button_text'] = theme_get_setting('header_button_text');

   $variables['base_url'] = $base_url;
	//Header setting -----------
	$header = 'header';
	if(theme_get_setting('default_header')){
		$header = theme_get_setting('default_header');
	}

	if(isset($variables['gva_header']) && $variables['gva_header'] && $variables['gva_header']!='_none' ){
		$header = $variables['gva_header'];
	}
	
	$header_skin = '';
	if($sub_theme){
		$theme_path_sub = \Drupal::service('extension.list.theme')->getPath($sub_theme);
		if(file_exists($theme_path_sub . '/templates/page/' . trim($header) . '.html.twig')){
			$header_skin  = $theme_path_sub . '/templates/page/' . trim($header) . '.html.twig';
		}elseif(file_exists($theme_path . '/templates/page/' . trim($header) . '.html.twig')){
			$header_skin  = $theme_path . '/templates/page/' . trim($header) . '.html.twig';
		}
	}else{
		if(file_exists($theme_path . '/templates/page/' . trim($header) . '.html.twig')){
			$header_skin  = $theme_path . '/templates/page/' . trim($header) . '.html.twig';
		}
	}
	if(empty($header_skin)) $header_skin = $theme_path . '/templates/page/header.html.twig';

	$variables['theme_path'] = $theme_path;

	$variables['header_skin'] = $header_skin;
 
 	$custom_account = '';
  	$user = \Drupal::currentUser();
  	$user_picture = '';
  	if($uid = $user->id()){
    	$custom_account = "Howdy, <a class=\"link-user\" href=\"{$base_url}/user/\"><span>{$user->getAccountName()}</span></a>";
    	$user = \Drupal\user\Entity\User::load($uid);
    	if (!empty($user->user_picture) && $user->user_picture->isEmpty() === FALSE) {
      	$user_picture = $user->user_picture->entity->getFileUri();
    	}
  	}
  	$variables['user_picture'] = $user_picture;
  	$variables['custom_account'] = $custom_account;
}

function ziston_preprocess_image(&$variables) {
	if (isset($variables['attributes']['width']) && isset($variables['attributes']['height'])) {
		unset($variables['attributes']['width']);
		unset($variables['attributes']['height']);
	}
}

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 *
 * Adds body classes if certain regions have content.
 */
function ziston_preprocess_html(&$variables) {
	global $theme, $base_url;
	global $parent_root;

	$theme_path = \Drupal::service('extension.list.theme')->getPath('ziston');

	$tmp_logo = theme_get_setting('logo');

	$variables['site_logo'] = $tmp_logo['url'];

	$variables['theme_path'] = $theme_path;

	if(theme_get_setting('customize_css') ){
		$custom_style  = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', theme_get_setting('customize_css') );
		$custom_style = str_replace( array( "\r\n", "\r", "\n", "\t", '  ', '   ', '    ' ), '', $custom_style );
		$variables['customize_css'] =  $custom_style;
	}

	//--- Customize gaviasthemer ---
	$customize_styles = '';
	$json = '';

	ob_start();
	$json = \Drupal::config('gaviasthemer.settings')->get('gavias_customize');
	
	$variables['links_google_fonts'] = ziston_links_typography_font($json);
	require_once($theme_path . '/customize/dynamic_style.php');
	$customize_styles = ob_get_clean();
	$customize_styles  = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $customize_styles );
	$customize_styles = str_replace( array( "\r\n", "\r", "\n", "\t", '  ', '   ', '    ' ), '', $customize_styles );

	$variables['customize_styles'] = $customize_styles;

	//Form customize 
	$user = \Drupal::currentUser();

	if(theme_get_setting('enable_customize') == 1 &&  \Drupal::moduleHandler()->moduleExists('gaviasthemer') && ($user->hasPermission('administer gavias_customize') || $user->hasPermission('administer gavias_customize preview')) ){
		$url_customize_save = Url::fromRoute('gaviasthemer.admin.customize_save', array(), array('absolute' => TRUE))->toString();
		$variables['#attached']['drupalSettings']['gavias_customize']['save'] = $url_customize_save;  
		$url_customize_preview = Url::fromRoute('gaviasthemer.admin.customize_preview', array(), array('absolute' => TRUE))->toString();
		$variables['#attached']['drupalSettings']['gavias_customize']['preview'] = $url_customize_preview;  
		$variables['#attached']['library'][] = 'gaviasthemer/customize';
 
		$variables['#attached']['drupalSettings']['gavias_customize']['json'] = $json;

		$variables['addon_template'] = '';
		$variables['save_customize_permission'] = 'hidden';
		$variables['fonts'] = ziston_render_option_font();
		$variables['patterns'] = ziston_options_patterns();
		if(file_exists($theme_path . '/templates/addon/skins.html.twig')){
			$variables['addon_template'] = $theme_path . '/templates/addon/skins.html.twig';
		}
		if($user->hasPermission('administer gavias_customize')){
			$variables['save_customize_permission'] = 'show';
		}
	}

	$variables['attributes']['class'][] = theme_get_setting('site_layout');
	$variables['attributes']['class'][] = theme_get_setting('footer_skin');

	$variables['preloader'] = theme_get_setting('preloader') == '1' ? 1 : 0;

	$variables['#attached']['drupalSettings']['listing_setting']['map_source'] = theme_get_setting('map_source') ? theme_get_setting('map_source') : 'google';
	$variables['#attached']['drupalSettings']['listing_setting']['mapbox_access_token'] = theme_get_setting('mapbox_access_token') ? theme_get_setting('mapbox_access_token') : 'pk.eyJ1IjoibmFtY3QiLCJhIjoiY2tnOHFwdHBsMGdodTJ5bHNwbnF5Mm5mciJ9.VNYZ-1YeH6oUe-2RvRGbyw';
	$variables['#attached']['drupalSettings']['listing_setting']['mapbox_id_style'] = theme_get_setting('mapbox_id_style') ? theme_get_setting('mapbox_id_style') : 'streets-v11';
	$variables['#attached']['drupalSettings']['listing_setting']['map_center_latitude'] = theme_get_setting('map_center_latitude');
	$variables['#attached']['drupalSettings']['listing_setting']['map_center_longitude'] = theme_get_setting('map_center_longitude');
	$variables['#attached']['drupalSettings']['listing_setting']['map_zoom'] = theme_get_setting('map_zoom') ? theme_get_setting('map_zoom') : '13';
	$variables['#attached']['drupalSettings']['listing_setting']['google_map_style'] = theme_get_setting('google_map_style');
}

function ziston_library_info_build() {
	$api_key = theme_get_setting('google_key');
	if(empty($api_key)) $api_key = 'AIzaSyBWk2I6T6psDLCXg8CDVFSEXvigfNU1V-E';
	$libraries = [];
	$theme_path = \Drupal::service('extension.list.theme')->getPath('ziston');
	$libraries['ziston.gmap_api'] = [
		'version' => '1.x',
		'js' => [
				'https://maps.googleapis.com/maps/api/js?key='.$api_key.'&libraries=places' => [
						'type' => 'external',
				],
				'assets/vendor/gmap3.js' => [],
				'assets/vendor/jquery.ui.map.min.js' => [],
		],
	];
	return $libraries;
}

/**
 * Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
 */
function ziston_preprocess_maintenance_page(&$variables) {
	if (!$variables['db_is_active']) {
		$variables['site_name'] = '';
	}
}

/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function ziston_preprocess_block(&$variables) {
	// Add a clearfix class to system branding blocks.

	if ($variables['plugin_id'] == 'system_branding_block') {
		$variables['attributes']['class'][] = 'clearfix';
		$tmp_logo = theme_get_setting('logo');
		$variables['setting_logo'] = $tmp_logo['use_default'];
	}
} 

function ziston_preprocess_block__system_breadcrumb_block(&$variables){
	$styles = array();
	$bg_image = base_path() . \Drupal::service('extension.list.theme')->getPath('ziston') . '/assets/images/breadcrumb.jpg';
	$bg_color = '#161C26';
	$bg_position = 'center center';
	$bg_repeat = 'no-repeat';
	$text_style = 'text-light';
	
	if (!empty($variables['elements']['#id'])) {
		$block = Block::load($variables['elements']['#id']);
		if($variables['plugin_id'] == 'system_breadcrumb_block'){
			$_id = $variables['elements']['#id'];
			$breadcrumb_background_color = $block->getThirdPartySetting('gaviasthemer', 'breadcrumb_background_color');
			$breadcrumb_background_position = $block->getThirdPartySetting('gaviasthemer', 'breadcrumb_background_position');
			$breadcrumb_background_repeat = $block->getThirdPartySetting('gaviasthemer', 'breadcrumb_background_repeat');
			$breadcrumb_color_style = $block->getThirdPartySetting('gaviasthemer', 'breadcrumb_color_style');
			$breadcrumb_background_image_path = \Drupal::config('gaviasthemer.settings')->get('breadcrumb_background_image_path_' . $_id);
			if($breadcrumb_color_style){
				$text_style = $breadcrumb_color_style;
			}
			if($breadcrumb_background_color){
				$bg_color = $breadcrumb_background_color;
			}
			if($breadcrumb_background_position){
				$bg_position = $breadcrumb_background_position;
			}
			if($breadcrumb_background_repeat){
				$bg_repeat = $breadcrumb_background_repeat;
			}
			if($breadcrumb_background_image_path){
				$bg_image = file_create_url($breadcrumb_background_image_path);
			}
		}
	}

	$variables['attributes']['class'][] = $text_style;
	$styles[] = "background-color: {$bg_color};";
	if($bg_image){
		$styles[] = "background-image: url('{$bg_image}');";
	}
	$styles[] = "background-position: {$bg_position};";
	$styles[] = "background-repeat: {$bg_repeat};";
	$variables['custom_style'] = implode('', $styles);

	//Breadcrumb title
	$title = '';
	$request = \Drupal::request();
	$title = '';
	if ($route = $request->attributes->get(\Drupal\Core\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
		$title = \Drupal::service('title_resolver')->getTitle($request, $route);
	} 
	 $variables['breadcrumb_title'] = $title;
}

/**
 * Implements hook_preprocess_HOOK() for page templates.
 */
function ziston_preprocess_page_title(&$variables) {
	// Since the title and the shortcut link are both block level elements,
	// positioning them next to each other is much simpler with a wrapper div.
	if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
		// Add a wrapper div using the title_prefix and title_suffix render
		// elements.
		$variables['title_prefix']['shortcut_wrapper'] = array(
			'#markup' => '<div class="shortcut-wrapper clearfix">',
			'#weight' => 100,
		);
		$variables['title_suffix']['shortcut_wrapper'] = array(
			'#markup' => '</div>',
			'#weight' => -99,
		);
		// Make sure the shortcut link is the first item in title_suffix.
		$variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
	}
}

/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 */
function ziston_theme_suggestions_form_alter(array &$suggestions, array $variables) {
	if ($variables['element']['#form_id'] == 'search_block_form') {
		$suggestions[] = 'form__search_block_form';
	}
}


function ziston_theme_suggestions_page_alter(array &$suggestions, array $variables) {
	if ($node = \Drupal::request()->attributes->get('node')) {
		if(is_object($node)){
			if($node->hasField('gva_node_layout')){
				$layout = $node->get('gva_node_layout')->value;
				if($layout){
					array_splice($suggestions, 1, 0, 'page__layout__' . $layout);
				}else{
					array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
				} 
			}else{
				array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
			}
		}else{
			array_splice($suggestions, 1, 0, 'page__layout__container_no_sidebar');
		}
	} 

	$view_display_id = \Drupal::routeMatch()->getParameter('display_id');
	if($view_display_id){
		array_splice($suggestions, 1, 0, 'page__view__' . $view_display_id);
	}

	if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical' && $tid = \Drupal::routeMatch()->getRawParameter('taxonomy_term')) {
		$term = Term::load($tid);
		array_splice($suggestions, 1, 0, 'page__taxonomy__' . $term->bundle());
	}
	$node = \Drupal::routeMatch()->getParameter('node_preview'); 
	if (!empty($node)){
		array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());         
	}
}

function ziston_theme_suggestions_field_alter(&$suggestions, $variables) {
  $suggestions[] = 'field__' . 
  $variables['element']['#field_name'] . '__' . 
  $variables['element']['#view_mode'];
}

function ziston_preprocess_views_view(&$variables){
  	global $base_url;
  	$view = $variables['view'];
  	$variables['attributes']['class'][]="gva-view";
  	if(is_numeric(strpos($view->current_display, 'page'))){
    	$variables['attributes']['class'][] = "view-page";
  	} 
  	$variables['base_url'] = $base_url;
  
  	$current_uri = \Drupal::request()->getRequestUri();
  	$url = \Drupal::service('path.current')->getPath();
  	$arg = explode('/', $url);
  	$tid = 0;
  	$taxonomy_id = '';
  	if ((isset($arg[1]) && $arg[1] ==  "taxonomy") && (isset($arg[2]) && $arg[2] == "term") && isset($arg[3]) && is_numeric($arg[3]) ) {
    	$tid = $arg[3];
    	$term = Term::load($tid);
    	if($term->bundle()){
      	$taxonomy_id = $term->bundle();
    	}
  	}
  	
  	$variables['taxonomy_id'] = $taxonomy_id;
  	if(!count($variables['rows']) && ($taxonomy_id == 'listing_category' || $taxonomy_id == 'listing_locations' || $taxonomy_id == 'amenities')){
    	$listing_search_action = $base_url . 'listings';
    	$language =  \Drupal::languageManager()->getCurrentLanguage()->getID();
    	$languagesAll =  \Drupal::languageManager()->getLanguages();
    	if(count($languagesAll) > 1){
      	if(theme_get_setting('listing_search_action' . $language)){
        		$listing_search_action = $base_url . theme_get_setting('listing_search_action' . $language);
      	}
    	}else{
      	if(theme_get_setting('listing_search_action')){
        	$listing_search_action = $base_url . theme_get_setting('listing_search_action');
      	}
    	}
    	$empty = '<div class="listings-map-1">';
      	$empty .= '<div class="listings-map-1"> ';
        		$empty .= '<div class="half-map-layout-1 clearfix">';
          		$empty .= '<div class="main-listing-wrapper">';
            		$empty .= '<div class="map-action clearfix"><div class="control-search"><a href="' . $listing_search_action . '">' . t('Search Page') .'</a></div></div>';  
            		$empty .= '<div class="listing-items gva-view-grid-inner"><div class="alert alert-warning">No found results</div></div>';
          		$empty .= '</div>';  
          		$empty .= '<div class="main-map-wrapper">';
            		$empty .= '<div id="listing-main-map" class="listing-main-map"></div>';
         		$empty .= '</div>';
        		$empty .= '</div>  ';
      	$empty .= '</div>  ';
    	$empty .= '<script>map_init();</script>';

   	$variables['empty'] = $empty;
  	}
}

function ziston_page_attachments_alter(array &$attachments) {
	$style = '';
	if(theme_get_setting('logo_width')&& !empty(theme_get_setting('logo_width'))){
		$style .= 'header .site-branding-logo{max-width:'.theme_get_setting('logo_width').'!important;}';
		$style .= ' header.header-default .header-main-inner .branding{width:'.theme_get_setting('logo_width').'!important;}';
	}
	if(theme_get_setting('logo_padding_top') && !empty(theme_get_setting('logo_padding_top'))){
		$style .= 'header .site-branding-logo{padding-top:'.theme_get_setting('logo_padding_top').'!important; }';
	}
	if(theme_get_setting('logo_padding_bottom') && !empty(theme_get_setting('logo_padding_bottom'))){
		$style .= 'header .site-branding-logo{padding-bottom:'.theme_get_setting('logo_padding_bottom').'!important; }';
	}
	$attachments['#attached']['html_head'][] = [
		[
			'#type' => 'html_tag',
			'#tag' => 'style',
			'#value' => $style,
		],
		'style-logo',
	];
}
