<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

if ( ! function_exists( 'dorje_setup' ) ) {
	add_action( 'after_setup_theme', 'dorje_setup' );
	// Sets up theme defaults and registers support for various WordPress features.
	function dorje_setup() {
		
		add_editor_style( 'style.css' );
		
	}
}

// Overwrite parent theme background defaults and registers support for WordPress features.
add_action( 'after_setup_theme', 'martanda_background_setup' );
function martanda_background_setup() {
	add_theme_support( "custom-background",
		array(
			'default-color' 		 => 'ffffff',
			'default-image'          => '',
			'default-repeat'         => 'repeat',
			'default-position-x'     => 'left',
			'default-position-y'     => 'top',
			'default-size'           => 'auto',
			'default-attachment'     => '',
			'wp-head-callback'       => '_custom_background_cb',
			'admin-head-callback'    => '',
			'admin-preview-callback' => ''
		)
	);
}

// Replace default fonts from parent theme
function martanda_get_font_face_styles() {
	return "
	@font-face{
		font-family: 'Work Sans';
		font-weight: 100;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-Thin.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 200;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-ExtraLight.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 300;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-Light.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 400;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-Regular.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 500;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-Medium.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 600;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-SemiBold.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 700;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-Bold.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 800;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-ExtraBold.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Work Sans';
		font-weight: 900;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/WorkSans-Black.woff2') format('woff2');
	}
	@font-face{
		font-family: 'Rubik Mono One';
		font-weight: 400;
		font-style: normal;
		font-stretch: normal;
		font-display: swap;
		src: url('" . get_stylesheet_directory_uri() . "/fonts/RubikMonoOne-Regular.woff2') format('woff2');
	}
	";
}

function martanda_font_family_css() {
	// Get our settings
	$martanda_settings = wp_parse_args(
		get_option( 'martanda_settings', array() ),
		martanda_get_defaults()
	);

	// Initiate our class
	$css = new martanda_css;
	
	$og_defaults = martanda_get_defaults( false );
	
	$bodyclass = 'body';
	if ( is_admin() ) {
		$bodyclass = '.editor-styles-wrapper';
	}
	
	$bodyfont = $martanda_settings[ 'font_body' ];
	if ( $bodyfont == 'inherit' ) { $bodyfont = 'Work Sans'; }
	
	$font_site_title = $martanda_settings[ 'font_site_title' ];
	if ( $font_site_title == 'inherit' ) { $font_site_title = 'Rubik Mono One'; }
	$font_navigation = $martanda_settings[ 'font_navigation' ];
	if ( $font_navigation == 'inherit' ) { $font_navigation = 'Work Sans'; }
	$font_buttons = $martanda_settings[ 'font_buttons' ];
	if ( $font_buttons == 'inherit' ) { $font_buttons = 'Work Sans'; }
	$font_heading_1 = $martanda_settings[ 'font_heading_1' ];
	if ( $font_heading_1 == 'inherit' ) { $font_heading_1 = 'Work Sans'; }
	$font_heading_2 = $martanda_settings[ 'font_heading_2' ];
	if ( $font_heading_2 == 'inherit' ) { $font_heading_2 = 'Work Sans'; }
	$font_heading_3 = $martanda_settings[ 'font_heading_3' ];
	if ( $font_heading_3 == 'inherit' ) { $font_heading_3 = 'Work Sans'; }
	$font_heading_4 = $martanda_settings[ 'font_heading_4' ];
	if ( $font_heading_4 == 'inherit' ) { $font_heading_4 = 'Work Sans'; }
	$font_heading_5 = $martanda_settings[ 'font_heading_5' ];
	if ( $font_heading_5 == 'inherit' ) { $font_heading_5 = 'Work Sans'; }
	$font_heading_6 = $martanda_settings[ 'font_heading_6' ];
	if ( $font_heading_6 == 'inherit' ) { $font_heading_6 = 'Work Sans'; }
	$font_footer = $martanda_settings[ 'font_footer' ];
	if ( $font_footer == 'inherit' ) { $font_footer = 'Work Sans'; }
	$font_fixed_side = $martanda_settings[ 'font_fixed_side' ];
	if ( $font_fixed_side == 'inherit' ) { $font_fixed_side = 'Work Sans'; }
	
	$css->set_selector( $bodyclass );
	$css->add_property( '--martanda--font-body', esc_attr( $bodyfont ) );
	$css->add_property( '--martanda--font-site-title', esc_attr( $font_site_title ) );
	$css->add_property( '--martanda--font-navigation', esc_attr( $font_navigation ) );
	$css->add_property( '--martanda--font-buttons', esc_attr( $font_buttons ) );
	$css->add_property( '--martanda--font-heading-1', esc_attr( $font_heading_1 ) );
	$css->add_property( '--martanda--font-heading-2', esc_attr( $font_heading_2 ) );
	$css->add_property( '--martanda--font-heading-3', esc_attr( $font_heading_3 ) );
	$css->add_property( '--martanda--font-heading-4', esc_attr( $font_heading_4 ) );
	$css->add_property( '--martanda--font-heading-5', esc_attr( $font_heading_5 ) );
	$css->add_property( '--martanda--font-heading-6', esc_attr( $font_heading_6 ) );
	$css->add_property( '--martanda--font-footer', esc_attr( $font_footer ) );
	$css->add_property( '--martanda--font-fixed-side', esc_attr( $font_fixed_side ) );
	
	// Allow us to hook CSS into our output
	do_action( 'martanda_font_family_css', $css );

	return apply_filters( 'martanda_font_family_css_output', $css->css_output() );
}

// Overwrite theme URL
function martanda_theme_uri_link() {
	return 'https://wpkoi.com/dorje-wpkoi-wordpress-theme/';
}

// Extra cutomizer functions
if ( ! function_exists( 'dorje_customize_register' ) ) {
	add_action( 'customize_register', 'dorje_customize_register' );
	function dorje_customize_register( $wp_customize ) {
				
		// Add Dorje customizer section
		$wp_customize->add_section(
			'dorje_layout_effects',
			array(
				'title' => __( 'Dorje Effects', 'dorje' ),
				'priority' => 24,
			)
		);
		
		// Menu stroke
		$wp_customize->add_setting(
			'dorje_settings[button_shadow]',
			array(
				'default' => 'enable',
				'type' => 'option',
				'sanitize_callback' => 'dorje_sanitize_choices'
			)
		);

		$wp_customize->add_control(
			'dorje_settings[button_shadow]',
			array(
				'type' => 'select',
				'label' => __( 'Button Shadow', 'dorje' ),
				'choices' => array(
					'enable' => __( 'Enable', 'dorje' ),
					'disable' => __( 'Disable', 'dorje' )
				),
				'settings' => 'dorje_settings[button_shadow]',
				'section' => 'dorje_layout_effects',
				'priority' => 10
			)
		);
		
		// Dorje effect colors
		$wp_customize->add_setting(
			'dorje_settings[dorje_color_1]', array(
				'default' => '#000000',
				'type' => 'option',
				'sanitize_callback' => 'dorje_sanitize_hex_color',
			)
		);

		$wp_customize->add_control(
			new WP_Customize_Color_Control(
				$wp_customize,
				'dorje_settings[dorje_color_1]',
				array(
					'label' => __( 'Effect color 1', 'dorje' ),
					'section' => 'dorje_layout_effects',
					'settings' => 'dorje_settings[dorje_color_1]',
					'priority' => 45
				)
			)
		);
		
	}
}

//Sanitize choices.
if ( ! function_exists( 'dorje_sanitize_choices' ) ) {
	function dorje_sanitize_choices( $input, $setting ) {
		// Ensure input is a slug
		$input = sanitize_key( $input );

		// Get list of choices from the control
		// associated with the setting
		$choices = $setting->manager->get_control( $setting->id )->choices;

		// If the input is a valid key, return it;
		// otherwise, return the default
		return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
	}
}

// Sanitize colors. Allow blank value.
if ( ! function_exists( 'dorje_sanitize_hex_color' ) ) {
	function dorje_sanitize_hex_color( $color ) {
	    if ( '' === $color ) {
	        return '';
		}

	    // 3 or 6 hex digits, or the empty string.
	    if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
	        return $color;
		}

	    return '';
	}
}

// Dorje effects colors css
if ( ! function_exists( 'dorje_effect_colors_css' ) ) {
	function dorje_effect_colors_css() {
		// Get Customizer settings
		$dorje_settings = get_option( 'dorje_settings' );
		
		$dorje_color_1	 = isset( $dorje_settings['dorje_color_1'] ) ? $dorje_settings['dorje_color_1'] : '#000000';
		
		$dorje_extracolors = 'body {--martanda--dorje-color-1: ' . esc_attr( $dorje_color_1 ) . ';}';
		
		return $dorje_extracolors;
	}
}

// The dynamic styles of the parent theme added inline to the parent stylesheet.
// For the customizer functions it is better to enqueue after the child theme stylesheet.
if ( ! function_exists( 'dorje_remove_parent_dynamic_css' ) ) {
	add_action( 'init', 'dorje_remove_parent_dynamic_css' );
	function dorje_remove_parent_dynamic_css() {
		remove_action( 'wp_enqueue_scripts', 'martanda_enqueue_dynamic_css', 50 );
	}
}

// Enqueue this CSS after the child stylesheet, not after the parent stylesheet.
if ( ! function_exists( 'dorje_enqueue_parent_dynamic_css' ) ) {
	add_action( 'wp_enqueue_scripts', 'dorje_enqueue_parent_dynamic_css', 50 );
	function dorje_enqueue_parent_dynamic_css() {
		$css = martanda_get_font_face_styles() . martanda_font_family_css() . martanda_base_css() . dorje_effect_colors_css();

		// escaped secure before in parent theme
		wp_add_inline_style( 'martanda-child', $css );
	}
}

//Adds custom classes to the array of body classes.
if ( ! function_exists( 'dorje_body_classes' ) ) {
	add_filter( 'body_class', 'dorje_body_classes' );
	function dorje_body_classes( $classes ) {
		// Get Customizer settings
		$dorje_settings = get_option( 'dorje_settings' );
		
		$button_shadow 	 = isset( $dorje_settings['button_shadow'] ) ? $dorje_settings['button_shadow'] : 'enable';
		
		// Menu stroke
		if ( $button_shadow != 'disable' ) {
			$classes[] = 'dorje-button-shadow';
		}
		
		return $classes;
	}
}