<?php
if ( ! function_exists( 'vouge_template_part' ) ) {
	/**
	 * Function that echo module template part.
	 */
	function vouge_template_part( $module, $template, $slug = '', $params = array() ) {
		echo vouge_get_template_part( $module, $template, $slug, $params );
	}
}

if ( ! function_exists( 'vouge_get_template_part' ) ) {
	/**
	 * Function that load module template part.
	 */
	function vouge_get_template_part( $module, $template, $slug = '', $params = array() ) {

		$file_path = '';
		$html      =  '';

		$template_path = VOUGE_MODULE_DIR . '/' . $module;
		$temp_path = $template_path . '/' . $template;

		if ( ! empty( $temp_path ) ) {
			if ( ! empty( $slug ) ) {
				$file_path = "{$temp_path}-{$slug}.php";
				if ( ! file_exists( $file_path ) ) {
					$file_path = $temp_path . '.php';
				}
			} else {
				$file_path = $temp_path . '.php';
			}
		}

		$file_path = apply_filters( 'vouge_get_template_plugin_part', $file_path, $module, $template, $slug);

		if ( is_array( $params ) && count( $params ) ) {
			extract( $params );
		}

		if ( $file_path && file_exists( $file_path ) ) {
			ob_start();
			include( $file_path );
			$html = ob_get_clean();
		}

		return $html;
	}
}

if ( ! function_exists( 'vouge_get_page_id' ) ) {
	function vouge_get_page_id() {

		$page_id = get_queried_object_id();

		if( is_archive() || is_search() || is_404() || ( is_front_page() && is_home() ) ) {
			$page_id = -1;
		}

		return $page_id;
	}
}

/* Convert hexdec color string to rgb(a) string */
if ( ! function_exists( 'vouge_hex2rgba' ) ) {
	function vouge_hex2rgba($color, $opacity = false) {

		$default = 'rgb(0,0,0)';

		if(empty($color)) {
			return $default;
		}

		if ($color[0] == '#' ) {
			$color = substr( $color, 1 );
		}

		if (strlen($color) == 6) {
				$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
		} elseif ( strlen( $color ) == 3 ) {
				$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
		} else {
				return $default;
		}

		$rgb =  array_map('hexdec', $hex);

		if($opacity){
			if(abs($opacity) > 1) {
				$opacity = 1.0;
			}
			$output = implode(",",$rgb).','.$opacity;
		} else {
			$output = implode(",",$rgb);
		}

		return $output;

	}
}

if ( ! function_exists( 'vouge_html_output' ) ) {
	function vouge_html_output( $html ) {
		return apply_filters( 'vouge_html_output', $html );
	}
}


if ( ! function_exists( 'vouge_theme_defaults' ) ) {
	/**
	 * Function to load default values
	 */
	function vouge_theme_defaults() {

		$defaults = array (
			'primary_color' => '#222121',
			'primary_color_rgb' => vouge_hex2rgba('#222121', false),
			'secondary_color' => '#efeade',
			'secondary_color_rgb' => vouge_hex2rgba('#efeade', false),
			'tertiary_color' => '#eaf1e5',
			'tertiary_color_rgb' => vouge_hex2rgba('#eaf1e5', false),
			'body_bg_color' => '#f6f9f1',
			'body_bg_color_rgb' => vouge_hex2rgba('#f6f9f1', false),
			'body_text_color' => '#131313',
			'body_text_color_rgb' => vouge_hex2rgba('#131313', false),
			'headalt_color' => '#222121',
			'headalt_color_rgb' => vouge_hex2rgba('#222121', false),
			'link_color' => '#222121',
			'link_color_rgb' => vouge_hex2rgba('#222121', false),
			'link_hover_color' => '#3d3d3d',
			'link_hover_color_rgb' => vouge_hex2rgba('#3d3d3d', false),
			'border_color' => '#d9d9d9',
			'border_color_rgb' => vouge_hex2rgba('#d9d9d9', false),
			'accent_text_color' => '#ffffff',
			'accent_text_color_rgb' => vouge_hex2rgba('#ffffff', false),

			'body_typo' => array (
				'font-family' => "Outfit",
				'font-fallback' => '"Outfit", sans-serif',
				'font-weight' => 400,
				'fs-desktop' => 16,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.62,
				'lh-desktop-unit' => ''
			),
			'h1_typo' => array (
				'font-family' => "Instrument Serif",
				'font-fallback' => '"Instrument Serif", serif',
				'font-weight' => 400,
				'fs-desktop' => 60,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.25,
				'lh-desktop-unit' => ''
			),
			'h2_typo' => array (
				'font-family' => "Instrument Serif",
				'font-fallback' => '"Instrument Serif", serif',
				'font-weight' => 400,
				'fs-desktop' => 48,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.25,
				'lh-desktop-unit' => ''
			),
			'h3_typo' => array (
				'font-family' => "Instrument Serif",
				'font-fallback' => '"Instrument Serif", serif',
				'font-weight' => 400,
				'fs-desktop' => 32,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.25,
				'lh-desktop-unit' => ''
			),
			'h4_typo' => array (
				'font-family' => "Instrument Serif",
				'font-fallback' => '"Instrument Serif", serif',
				'font-weight' => 400,
				'fs-desktop' => 28,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.25,
				'lh-desktop-unit' => ''
			),
			'h5_typo' => array (
				'font-family' => "Instrument Serif",
				'font-fallback' => '"Instrument Serif", serif',
				'font-weight' => 400,
				'fs-desktop' => 24,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.25,
				'lh-desktop-unit' => ''
			),
			'h6_typo' => array (
				'font-family' => "Instrument Serif",
				'font-fallback' => '"Instrument Serif", serif',
				'font-weight' => 400,
				'fs-desktop' => 18,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.25,
				'lh-desktop-unit' => ''
			),
			'extra_typo' => array (
				'font-family' => "DM Sans",
				'font-fallback' => '"DM Sans", sans-serif',
				'font-weight' => 500,
				'fs-desktop' => 14,
				'fs-desktop-unit' => 'px',
				'lh-desktop' => 1.1,
				'lh-desktop-unit' => ''
			),

		);

		return $defaults;

	}
}