<?php
/**
 * Basho theme helper functions.
 */

if ( ! function_exists( 'basho_unique_id' ) ) {
    /**
     * Get unique ID.
     *
     * This is a PHP implementation of Underscore's uniqueId method. A static variable
     * contains an integer that is incremented with each call. This number is returned
     * with the optional prefix. As such the returned value is not universally unique,
     * but it is unique across the life of the PHP process.
     *
     * @see wp_unique_id() Themes requiring WordPress 5.0.3 and greater should use this instead.
     *
     * @staticvar int $id_counter
     *
     * @param string $prefix Prefix for the returned ID.
     * @return string Unique ID.
     */
    function basho_unique_id( $prefix = '' ) {
    	static $id_counter = 0;
    	if ( function_exists( 'wp_unique_id' ) ) {
    		return wp_unique_id( $prefix );
    	}
    	return $prefix . (string) ++$id_counter;
    }
}

if ( ! function_exists( 'basho_get_social_icon_id' ) ) {
    /**
     * Get icon attribute by menu item url passed to this function.
     *
     * @param string $url Menu item url.
     * @return string $id Uikit svg icon identifier.
     */
    function basho_get_social_icon_id( $url ) {

        $ids = array(
            'facebook.com'      => 'icon: basho-facebook; ratio: .75',
            'm.me'              => 'icon: basho-facebook; ratio: .75',
            'twitter.com'       => 'icon: basho-twitter; ratio: .75',
            'whatsapp.com'      => 'whatsapp',
            'wa.me'             => 'whatsapp',
            'youtube.com'       => 'youtube',
            'dribbble.com'      => 'dribbble',
            'github.com'        => 'github',
            'flickr.com'        => 'flickr',
            'behance.net'       => 'behance',
            'foursquare.com'    => 'foursquare',
            'google.com'        => 'google',
            'plus.google.com'   => 'google-plus',
            'instagram.com'     => 'instagram',
            'linkedin.com'      => 'linkedin',
            'pinterest.com'     => 'pinterest',
            'soundcloud.com'    => 'soundcloud',
            'tripadvisor.com'   => 'tripadvisor',
            'tumblr.com'        => 'tumblr',
            'vimeo.com'         => 'vimeo',
            'youtube'           => 'youtube',
            'wordpress.org'     => 'wordpress',
            'xing.com'          => 'xing',
            'yelp.com'          => 'yelp',
            't.me'              => 'icon: basho-telegram; ratio: .75',
            'vk.com'            => 'icon: basho-vk; ratio: .8',
            'odnoklassniki.ru'  => 'icon: basho-odnoklassniki; ratio: .75',
        );

        foreach ( $ids as $site => $uikit_icon_id ) {
            if ( strpos( $url, $site ) ) {
                return $uikit_icon_id;
            }
        }

        return '';
    }
}

if ( ! function_exists( 'basho_generate_css' ) ) {
	/**
	 * Generate CSS. This function I took from TwentyTwenty WordPress theme.
	 *
	 * @param string $selector The CSS selector.
	 * @param string $style The CSS style.
	 * @param string $value The CSS value.
	 * @param string $prefix The CSS prefix.
	 * @param string $suffix The CSS suffix.
	 * @param bool   $echo Echo the styles.
	 */
	function basho_generate_css( $selector, $style, $value, $prefix = '', $suffix = '', $echo = true ) {

		$return = '';

		/*
		 * Bail early if we have no $selector elements or properties and $value.
		 */
		if ( ! $value || ! $selector ) {

			return;
		}

		$return = sprintf( '%s { %s: %s; }', $selector, $style, $prefix . $value . $suffix );

		if ( $echo ) {

			echo $return; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		}

		return $return;

	}
}

if ( ! function_exists( 'basho_adjust_color_brightness' ) ) {
    /**
     * Adjust a hex color brightness.
     * Allows to create hover styles for custom link colors.
     *
     * @param  strong  $hex   hex color e.g. #111111.
     * @param  integer $steps factor by which to brighten/darken ranging from -255 (darken) to 255 (brighten).
     * @return string  brightened/darkened hex color
     */
    function basho_adjust_color_brightness( $hex, $steps ) {
    	// Steps should be between -255 and 255. Negative = darker, positive = lighter.
    	$steps = max( -255, min( 255, $steps ) );

    	// Format the hex color string.
    	$hex = str_replace( '#', '', $hex );

    	if ( 3 === strlen( $hex ) ) {
    		$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
    	}

    	// Get decimal values.
    	$r = hexdec( substr( $hex, 0, 2 ) );
    	$g = hexdec( substr( $hex, 2, 2 ) );
    	$b = hexdec( substr( $hex, 4, 2 ) );

    	// Adjust number of steps and keep it inside 0 to 255.
    	$r = max( 0, min( 255, $r + $steps ) );
    	$g = max( 0, min( 255, $g + $steps ) );
    	$b = max( 0, min( 255, $b + $steps ) );

    	$r_hex = str_pad( dechex( $r ), 2, '0', STR_PAD_LEFT );
    	$g_hex = str_pad( dechex( $g ), 2, '0', STR_PAD_LEFT );
    	$b_hex = str_pad( dechex( $b ), 2, '0', STR_PAD_LEFT );

    	return '#' . $r_hex . $g_hex . $b_hex;
    }
}

if ( ! function_exists( 'basho_is_woocommerce_activated' ) ) {
    /**
     * Check if WooCommerce plugin is active.
     */
    function basho_is_woocommerce_activated()
    {
        return class_exists( 'WooCommerce' ) ? true : false;
    }
}
