<?php
if(!function_exists('get_diya_settings')) {
  function get_diya_settings($name = '', $tab = '') {
    if(empty($tab) && empty($name)) return '';
    if(empty($tab)) return get_option($name);
    if(empty($name)) return get_option("dc_{$tab}_settings_name");
    $settings = get_option("dc_{$tab}_settings_name");
    if(!isset($settings[$name])) return '';
    return $settings[$name];
  }
}

if(!function_exists('getFontAwesomeIconArray')) {
	function getFontAwesomeIconArray(){
		global $wp_filesystem;
		
		$pattern = '/\.(fa-(?:\w+(?:-)?)+):before\s+{\s*content:\s*"(.+)";\s+}/';
	
		//if( ini_get('allow_url_fopen') ) {
			//$subject = $wp_filesystem->get_contents( get_template_directory_uri() .'/lib/fonts/font-awesome/css/font-awesome.css');
		//} else {
			$subject = file_get_contents_curl( get_template_directory_uri() .'/lib/fonts/font-awesome/css/font-awesome.css' );
		//}
		preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);

		$icons = array();
		$icons[''] = '';
		foreach($matches as $match){
			$icons[$match[1]] = $match[1];
		}
		array_shift($icons);
		ksort($icons);
		$new_array = '--SELECT--';
		array_unshift($icons, $new_array);
		return $icons;
		
	}
}

/* Font Awesome Array */
if (!function_exists('file_get_contents_curl')){
  function file_get_contents_curl($url) {
      /*$ch = curl_init();

      curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

      $data = curl_exec($ch);
      curl_close($ch);*/
      
      $response = wp_remote_get( $url );
      if( is_array($response) ) {
				//$header = $response['headers']; // array of http header lines
				$data = $response['body']; // use the content
			}

      return $data;
  }
}

function sanitize_primary_color($color) {
	$primaryColor = $color;
	$primaryHoverColor = get_theme_mod( 'primary-hover-color-setting' );
	$secondaryColor = get_theme_mod( 'secondary-color-setting' );
	$secondaryHoverColor = get_theme_mod( 'secondary-hover-color-setting' );
	$secondaryBackColor = get_theme_mod( 'secondary-color-setting' );
	
	switch_diya_theme_color($primaryColor, $primaryHoverColor, $secondaryColor, $secondaryHoverColor, $secondaryBackColor);
	
	return $color;
}

function sanitize_primary_hover_color($color) {
	$primaryColor = get_theme_mod( 'primary-color-setting' );
	$primaryHoverColor = $color;
	$secondaryColor = get_theme_mod( 'secondary-color-setting' );
	$secondaryHoverColor = get_theme_mod( 'secondary-hover-color-setting' );
	$secondaryBackColor = get_theme_mod( 'secondary-color-setting' );
	
	switch_diya_theme_color($primaryColor, $primaryHoverColor, $secondaryColor, $secondaryHoverColor, $secondaryBackColor);
	
	return $color;
}

function sanitize_secondary_color($color) {
	$primaryColor = get_theme_mod( 'primary-color-setting' );
	$primaryHoverColor = get_theme_mod( 'primary-hover-color-setting' );
	$secondaryColor = $color;
	$secondaryHoverColor = get_theme_mod( 'secondary-hover-color-setting' );
	$secondaryBackColor = $color;
	
	switch_diya_theme_color($primaryColor, $primaryHoverColor, $secondaryColor, $secondaryHoverColor, $secondaryBackColor);
	
	return $color;
}

function sanitize_secondary_hover_color($color) {
	$primaryColor = get_theme_mod( 'primary-color-setting' );
	$primaryHoverColor = get_theme_mod( 'primary-hover-color-setting' );
	$secondaryColor = get_theme_mod( 'secondary-color-setting' );
	$secondaryHoverColor = $color;
	$secondaryBackColor = get_theme_mod( 'secondary-color-setting' );
	
	switch_diya_theme_color($primaryColor, $primaryHoverColor, $secondaryColor, $secondaryHoverColor, $secondaryBackColor);
	
	return $color;
}

if(!function_exists('switch_diya_theme_color')) {
	function switch_diya_theme_color($primaryColor = '#000000', $primaryHoverColor = '#ffffff', $secondaryColor = '#ffc907', $secondaryHoverColor = '#000000', $secondaryBackColor = '#ffc907') {
		global $DC_Diya;
		$primary_color_blocks = array(
			'.main_menu_pixiefy nav ul li a',
			'.we_are_diya h2',
			'.about_diya h2',
			'.hovicon.effect-3.sub-b, .hovicon.effect-3.sub-b i',
			'.about_promo h2',
			'.tab_title_his > li.active > a, .tab_title_his > li.active > a:hover, .tab_title_his > li.active > a:focus',
			'.our_service h2',
			'.service_content h2',
			'.hovicon2.effect-3_2.sub-b_2, .hovicon2.effect-3_2.sub-b_2 i',
			'.service_icon_pixiefy p',
			'.testimonial_area_pt h2',
			'.profile_info_testimonial h6',
			'.team_area h2',
			'.team_area_inner_first h2',
			'.team_area_massage h2',
			'.skill_we_got h2',
			'.our_works_title h2',
			'.next_project h2',
			'.location_titles h2',
			'.contact_form_list h2',
			'.stay_c_title h2',
			'.location_inner p',
			'.copy_right p',
		
			);
		
		$primary_hover_color_blocks = array(
			
			
			'.about_promo:hover i',
			'.hovicon2.effect-3_2.sub-b_2:hover, .hovicon2.effect-3_2.sub-b_2:hover i',
			
			);
		
		$seconday_color_blocks = array(
			'.we_are_diya span',
			'.about_diya span',
			'ul.tab_title_his li a',	
			'.our_service span',
			'.fa.fa-star',
			'.intesresting_fact_inner h2',
			'.team_area span',	
			'.our_works_title span',
			'.location_titles span',
			'.main_heding_title h2',
			'.post_meta_blog h2 span',
			'.single_search button i',
			'.right_sidebar h2',
			'.main_menu_pixiefy nav ul li a:hover, #current_active_item',
			'.carousel_pixiefy a.link_str:hover',
			'.team_inner_trr:hover i',
			);
		
		$secondary_hover_color_blocks = array(
			'.tab_inner a:hover',
			'.team_inner_trr:hover i',
			);
		
		$secondary_color_backgrounds = array(
			'.tab_inner a:hover',
			'.sp_carousel_inner',
			'.owl-theme .owl-controls .owl-page.active span, .owl-theme .owl-controls.clickable .owl-page:hover span',
			'.sp_carousel_inner',
			'.next_project_inner a',
			'.wpcf7 input[type="submit"]',
			'.stay_c_icon a i',
			'.skill_right, .skill_right2, .skill_right3',
			'.tab_inner a',
			'.hovicon3.effect-3_3:after',
			'.hovicon2.effect-3_2:after',
			'.hovicon.effect-3:after',
			'.about_promo:hover i',
			'.hovicon4.effect-3_4:after',
			'.filter_nav ul li a:hover, .filter_nav ul li a.selected',
			'#easin_carousel4',
			'.tp-bullets.simplebullets .bullet:hover, .tp-bullets.simplebullets .bullet.selected',
			'.blog_post_pixiefy a:hover',
			'.carousel_title h2',
			'#menu_toggle span'
			);
		
		$primaryColorDefault = '#000000';
		$primaryHoverColorDefault = '#ffffff';
		$secondaryColorDefault = '#ffc907';
		$secondaryHoverColorDefault = '#000000';
		$secondaryBackColorDefault = '#ffc907';
		
		$switchCssContent = '';
		
		// Primary Color
		if($primaryColor && $primaryColorDefault != $primaryColor) {
			foreach($primary_color_blocks as $primary_color_block) {
				$switchCssContent .= $primary_color_block . " {". "\n" . "  color: " . $primaryColor . ";" . "\n" . "}" . "\n\n";
			}
		}
		
		// Primary Hover Color
		if($primaryHoverColor && $primaryHoverColorDefault != $primaryHoverColor) {
			foreach($primary_hover_color_blocks as $primary_hover_color_block) {
				$switchCssContent .= $primary_hover_color_block . " {". "\n" . "  color: " . $primaryHoverColor . ";" . "\n" . "}" . "\n\n";
			}
		}
		
		// Secondary Color
		if($secondaryColor && $secondaryColorDefault != $secondaryColor) {
			foreach($seconday_color_blocks as $seconday_color_block) {
				$switchCssContent .= $seconday_color_block . " {". "\n" . "  color: " . $secondaryColor . ";" . "\n" . "}" . "\n\n";
			}
		}
		
		// Secondary Hover Color
		if($secondaryHoverColor && $secondaryHoverColorDefault != $secondaryHoverColor) {
			foreach($secondary_hover_color_blocks as $secondary_hover_color_block) {
				$switchCssContent .= $secondary_hover_color_block . " {". "\n" . "  color: " . $secondaryHoverColor . ";" . "\n" . "}" . "\n\n";
			}
		}
		
		// Secondary Back Color
		if($secondaryBackColor && $secondaryBackColorDefault != $secondaryBackColor) {
			foreach($secondary_color_backgrounds as $secondary_color_background) {
				$switchCssContent .= $secondary_color_background . " {". "\n" . "  background-color: " . $secondaryBackColor . ";" . "\n" . "}" . "\n\n";
			}
		}
		
		return $switchCssContent;
		
		$cssFolder = $DC_Diya->theme_path . 'assets/frontend/css/';
		
		/*$form_fields = array ('save');
		$method = 'ftp';
		$url = wp_nonce_url('themes.php?page=Diya','diya-options');
		if (false === ($creds = request_filesystem_credentials($url, $method, false, false, $form_fields) ) ) {
			if ( ! WP_Filesystem($creds) ) {
				request_filesystem_credentials($url, $method, true, false, $form_fields);
			
				global $wp_filesystem;
				$wp_filesystem->put_contents($cssFolder . 'switch-color.css', $switchCssContent);
			}
		}*/
		//$switchCssFile = fopen($cssFolder . 'switch-color.css', "wb");
		//fwrite($switchCssFile, $switchCssContent);
		//fclose($switchCssFile);
	}
}

function diya_theme_color_switcher() {
	$primaryColor = get_theme_mod( 'primary-color-setting' );
	$primaryHoverColor = get_theme_mod( 'primary-hover-color-setting' );
	$secondaryColor = get_theme_mod( 'secondary-color-setting' );
	$secondaryHoverColor = get_theme_mod( 'secondary-hover-color-setting' );
	$secondaryBackColor = get_theme_mod( 'secondary-color-setting' );
	
	$color = switch_diya_theme_color($primaryColor, $primaryHoverColor, $secondaryColor, $secondaryHoverColor, $secondaryBackColor);
	
	echo '<style>' . $color . '</style>';
}
add_action('wp_footer', 'diya_theme_color_switcher');
?>