<?php
/** this file contains functions added by suncode
 * */
//suncode start
//function sc_admin_style() {
//  wp_enqueue_style('admin-styles', get_template_directory_uri().'/suncode/css/admin.css');
//}
//add_action('admin_enqueue_scripts', 'sc_admin_style');
 /* add css to elementor page editor start*/
 add_action('elementor/editor/before_enqueue_scripts','add_elementor_css_by_suncode');
 function add_elementor_css_by_suncode(){
     $page_action = isset($_GET['action']) ? $_GET['action'] :'';
     if($page_action == 'elementor'){
         wp_enqueue_style( 'suncode_elementor_front_css',  get_template_directory_uri().'/suncode/css/suncode_elementor_front_css.css');
     }
     if(is_admin()){
         
     }
 } 
/** add css to elementor page editor end **/
function sc_admin_style() {
  wp_enqueue_style('admin-styles', get_template_directory_uri().'/suncode/css/admin.css');
}
add_action('admin_enqueue_scripts', 'sc_admin_style');
 function suncode_login_stylesheet() {
    wp_enqueue_style( 'custom-login', get_stylesheet_directory_uri() . '/suncode/css/style-login.css' );
}
add_action( 'login_enqueue_scripts', 'suncode_login_stylesheet' );
// customize admin bar css
function suncode_override_admin_bar_css() { 

   if ( is_admin_bar_showing() ) { 
       wp_enqueue_style( 'custom-adminbar', get_stylesheet_directory_uri() . '/suncode/css/style-adminbar.css' );


    }

}
// on frontend area
add_action( 'wp_head', 'suncode_override_admin_bar_css' );

// woo address
add_filter( 'woocommerce_default_address_fields' , 'sc_pixwell_override_address_fields', 999, 1 );
function sc_pixwell_override_address_fields( $fields ) {
	$persian_sort = array( 1 => 'country', 'state', 'city', 'address_1', 'address_2', 'postcode' );
	
	foreach( $fields as $key => $field ) {
		
		if( ! in_array( $key, $persian_sort ) ) {
			$fields[ $key ][ 'persian_sort' ] = 0;
		} else {
			$fields[ $key ][ 'persian_sort' ] = array_search( $key, $persian_sort );
		}
		
	}
		
	uasort( $fields, function( $val1, $val2 ) use( $persian_sort ) {
		
		if( $val1[ 'persian_sort' ] == 0 || $val2[ 'persian_sort' ] == 0 ) {
			return 0;
		}
		
		return $val1[ 'persian_sort' ] > $val2[ 'persian_sort' ] ? 1 : -1;
	} );
	
	$i = 10;
	
	foreach( $fields as $key => $field ) {
		$fields[ $key ][ 'priority' ] = $i;
		$i += 10;
	}
	
	return $fields;
}
// woo address end

/**
 * Load custom translation file for the Bravis Addons plugin.
 *
 * This function loads the Persian (fa_IR) translation file for the 'pixelart-core' text domain
 * from the theme's languages directory.
 */
function kaitex_load_bravis_addons_translation() {
    // Define the path to the translation file
    $translation_path = get_template_directory() . '/suncode/languages/pixelart-core-fa_IR.mo';
    
    // Check if the translation file exists
    if ( file_exists( $translation_path ) ) {
        // Load the translation file for the 'pixelart-core' text domain
        load_textdomain( 'pixelart-core', $translation_path );
    }
}

// Hook the function to the 'after_setup_theme' action to ensure it runs early
add_action( 'after_setup_theme', 'kaitex_load_bravis_addons_translation' );
/**
 * Add Group Fonts
 */
 /**
 * Add Font Group
 */
add_filter( 'elementor/fonts/groups', function( $font_groups ) {
	$font_groups['theme_fonts'] = __( 'فونت های قالب' );
	return $font_groups;
} );
add_filter( 'elementor/fonts/additional_fonts', function( $additional_fonts ) {
	// Key/value
	//Font name/font group
	$additional_fonts['Yekan_Bakh'] = 'theme_fonts';
	$additional_fonts['Morabba'] = 'theme_fonts';
	$additional_fonts['IRANSansXFaNum'] = 'theme_fonts';
	$additional_fonts['IRANYekanXFaNum'] = 'theme_fonts';
	$additional_fonts['DanaFaNum'] = 'theme_fonts';
	$additional_fonts['Modam'] = 'theme_fonts';
	$additional_fonts['Doran'] = 'theme_fonts';
	$additional_fonts['rokh'] = 'theme_fonts';
	$additional_fonts['PinarOne'] = 'theme_fonts';
	$additional_fonts['PinarTwo'] = 'theme_fonts';
	return $additional_fonts;
} );
function xtra_revslider_data_get_font_familys( $fonts ) {

	$persian_fonts = [];

	//Web Safe fonts

	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'Yekan_Bakh');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'Morabba');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'IRANSansXFaNum');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'IRANYekanXFaNum');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'DanaFaNum');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'Modam');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'Doran');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'rokh');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'PinarOne');
	$persian_fonts[] = array('type' => 'websafe', 'version' => __('Persian Fonts', 'revslider'), 'label' => 'PinarTwo');
	return wp_parse_args( $fonts, $persian_fonts );
}
add_filter( 'revslider_data_get_font_familys', 'xtra_revslider_data_get_font_familys' );

function sc_add_custom_text_box_for_theme() {
    global $pagenow;
    if ($pagenow !== 'themes.php') return;
    ?>
    <style>
    .theme[data-slug="kaitex"] {
        position: relative;
        overflow: hidden; /* Prevent overflow from the ribbon */
    }
    .theme[data-slug="kaitex"] .sc-custom-box {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        padding: 12px 15px;
        background: linear-gradient(135deg, rgba(38,50,56,0.85), rgba(69,90,100,0.75));
        color: #ffffff;
        font-size: 15px;
        text-align: center;
        border-bottom-left-radius: 10px;
        border-bottom-right-radius: 10px;
        box-sizing: border-box;
        transform: translateY(-100%);
        transition: transform 0.3s ease;
        pointer-events: none;
        z-index: 10;
    }
    .theme[data-slug="kaitex"]:hover .sc-custom-box {
        transform: translateY(0);
        pointer-events: auto;
    }
    .theme[data-slug="kaitex"] .sc-custom-box a {
        display: inline-block;
        margin-top: 8px;
        padding: 6px 14px;
        background-color: #8ed557;
        border-radius: 5px;
        color: #ffffff;
        text-decoration: none;
        font-weight: bold;
        transition: background-color 0.2s ease;
    }
    .theme[data-slug="kaitex"] .sc-custom-box a:hover {
        background-color: #7cc747;
    }
    </style>

    <script>
    (function waitForTheme() {
        const themeEl = document.querySelector('.theme[data-slug="kaitex"]');
        if (!themeEl) return setTimeout(waitForTheme, 200);

        if (themeEl.querySelector('.sc-custom-box')) return;

        const box = document.createElement('div');
        box.className = 'sc-custom-box';
        box.innerHTML = `
            قالب کایتکس، بومی شده توسط سان کد<br>
            <a href="https://www.rtl-theme.com/kaitex-wordpress-theme/" target="_blank">
                خرید از راست‌چین
            </a>
        `;
        themeEl.appendChild(box);
    })();
    </script>
    <?php
}
add_action('admin_footer', 'sc_add_custom_text_box_for_theme');
