<?php
add_action('ct_before_builder', function(){
?>
    <script>
        setTimeout(() => {
            if(document.querySelectorAll('[ng-app="CTFrontendBuilderUI"]')[0]){
                    document.querySelectorAll('[ng-app="CTFrontendBuilderUI"]')[0].setAttribute("dir", "ltr");
                var T = document.getElementsByTagName("title")[0];
                T.innerText =  T.innerText.replace("Oxygen", "" );
            }
        }, 1000);
    </script>   
<?php
});

// function rm_oxykits_get_carbon_rep($field_name){
// 	if(!function_exists('carbon_get_theme_option')) {
// 	   global $wpdb;
//         $results = $GLOBALS['wpdb']->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_name LIKE '_$field_name%'", ARRAY_A );
        
//         $load_fonts = [];
//         foreach($results as $kkk=>$reee){
//             $item = explode("|",$reee['option_name']);
//             $index = intval($item[2]);
//             $keyX = $item[1];
//             if(empty($index)  && $index != 0) {
//                 continue;
//             }
//             $load_fonts[$index][$keyX] = $reee['option_value'];
           
//         }
// 	}
// 	else {
// 	    $load_fonts = carbon_get_theme_option($field_name);
// 	}
// 	return $load_fonts;
// }
function rm_oxykits_get_meta_array_as_string($key, $pid=0) {
    $pid        = ($pid) ? : get_the_ID( );
    $get_meta   = get_post_meta( $pid , $key, true);
    $final_data = (is_array($get_meta)) ? implode(",",$get_meta) : "";
    return $final_data;
}
function rm_oxykits_youtube_return_id_from_link($link){
    preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $link, $matches);

    $id = (count($matches)> 0) ? $matches[0] : '';
    
    $pattern = '/\?[a-zA-Z0-9-_=]*|\=[a-zA-Z0-9-_=]*/i';
    $id = ($id) ? preg_replace($pattern, '', $id) : '';
    
	return $id;
}
function rm_oxykits_youtube_return_image_url($link="", $check_custom=true, $image_size='medium', $video_info=[]){
    $image_url = "";
    if($check_custom){
        $custom_image   = (isset($video_info['custom_image'])) ? $video_info['custom_image'] : '';

        if(is_array($custom_image)) {
            $image_url      = $custom_image['sizes'][$image_size];
        }
        elseif(!empty($custom_image)) {
            $image_url      = $custom_image;
        }
    }
    if(!$image_url){
        $size = ( $image_size == 'medium' ) ? 'hqdefault' : 'maxresdefault';
        $image_url  = ($link) ? 'https://img.youtube.com/vi/'. rm_oxykits_youtube_return_id_from_link($link) .'/'. $size .'.jpg' : "";
    }
	return $image_url;
}



// preparing fonts from settings
function rm_oxykits_preparing_font() {
	$ds1Up = wp_upload_dir()['baseurl'];
	$current_lang = (get_locale() == 'ar') ? 'ar' : 'default';
	
	$load_fonts = get_field('fonts', 'option');
	if($load_fonts) {
		foreach($load_fonts as $font) {
			if($font['lang'] != $current_lang || !$font['font_url']) {
				continue;
			}
			
			if($font['ftype'] == 'sheet') {
				echo '<link href="'. $font['font_url'] .'" rel="stylesheet">';
			}
			else {
				$fonts = explode(',', $font['font_url']);
				$woff2 = ($font['ftype'] == 'url') ? $fonts[0] : $ds1Up . $fonts[0];
				$woff = ($font['ftype'] == 'url') ? $fonts[1] : $ds1Up . $fonts[1];
				
				?>
				<style>
				@font-face {
					font-family: '<?php echo $font['font_name']; ?>';
					font-display: swap;
					src: 
						local('<?php echo $font['font_name']; ?>'),
						url('<?php echo $woff2;?>') format('woff2'),
						url('<?php echo $woff;?>') format('woff');
					font-style: normal;
					font-weight: <?php echo $font['weight']; ?>;
				}
				</style>
			<?php
			}
		}
	}
	else {
		$font_url = ($current_lang == 'ar') ? 'https://fonts.googleapis.com/css2?family=Cairo&display=swap' : 'https://fonts.googleapis.com/css2?family=Montserrat&display=swap'; 
		echo '<link href="'. $font_url .'" rel="stylesheet">';
	}
}




function rm_oxykits_get_logo_info($info_type = 'url') {
	$final = '';
	
	if($info_type == 'code') {
		$lang = (get_locale() == 'ar') ? 'ar' : 'en';
		$logo = get_option('options_logo_code_' . $lang, '');
		$final = ($logo) ? : get_option('options_logo_code', '');
	}
	
	if($info_type == 'url' || ( $info_type == 'code' && !$final)) {
		$lang = (get_locale() == 'ar') ? 'ar' : 'en';
		$logo = get_option('options_logo_' . $lang, '');
		$final = ($logo) ? : get_option('options_logo', '');		
	}
	
	return $final;
}
