<?php
/**
 * Perch functions and definitions
 */

if ( ! function_exists( 'perch_support' ) ) {

	// Sets up theme defaults and registers support for various WordPress features.
	function perch_support() {

		// Add support for block styles.
		add_theme_support( 'wp-block-styles' );

		// Enqueue editor styles.
		add_editor_style( 'style.css' );

	}
}
add_action( 'after_setup_theme', 'perch_support' );

if ( ! function_exists( 'perch_styles' ) ) {
	// Enqueue styles.
	function perch_styles() {

		// Register theme stylesheet.
		wp_register_style(
			'perch-style',
			get_template_directory_uri() . '/assets/css/theme.min.css',
			array(),
			wp_get_theme()->get( 'Version' )
		);

		// Add styles inline.
		wp_add_inline_style( 'perch-style', perch_get_font_face_styles() );

		// Enqueue theme stylesheet.
		wp_enqueue_style( 'perch-style' );

	}
}
add_action( 'wp_enqueue_scripts', 'perch_styles' );

if ( ! function_exists( 'perch_editor_styles' ) ) {
	// Enqueue editor styles.
	function perch_editor_styles() {
		// Add styles inline.
		wp_add_inline_style( 'wp-block-library', perch_get_font_face_styles() );

		add_editor_style(
			get_template_directory_uri() . '/assets/css/theme.min.css'
		);
	}
}
add_action( 'admin_init', 'perch_editor_styles' );

// Get font face styles.
if ( ! function_exists( 'perch_get_font_face_styles' ) ) {
	function perch_get_font_face_styles() {
		return "
		@font-face{
			font-family: 'Rubik';
			font-weight: 100;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Light.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 200;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Light.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 300;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Light.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 400;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Regular.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 500;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Medium.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 600;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-SemiBold.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 700;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Bold.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 800;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-ExtraBold.woff2' ) . "') format('woff2');
		}
		@font-face{
			font-family: 'Rubik';
			font-weight: 900;
			font-style: normal;
			font-stretch: normal;
			font-display: swap;
			src: url('" . get_theme_file_uri( 'assets/fonts/Rubik-Black.woff2' ) . "') format('woff2');
		}
		";
	}
}

// Add script to Editor
if ( ! function_exists( 'perch_admin_add_scripts' ) ) {
	function perch_admin_add_scripts( $hook ){
		if ( 'appearance_page_perch' != $hook ) {
			return;
		}
		
		wp_register_style( 'perch-settings-css',  get_template_directory_uri() . '/assets/css/admin.min.css' , '1.0.0' );
		wp_enqueue_style( 'perch-settings-css');
	
	}
}
add_action( 'admin_enqueue_scripts', 'perch_admin_add_scripts');

// Add admin page content
get_template_part( 'inc/theme' );

// Add admin page
if ( ! function_exists( 'perch_create_menu' ) ) {
	add_action( 'admin_menu', 'perch_create_menu' );
	// Adds our dashboard menu item
	function perch_create_menu() {
		add_theme_page( 'Perch WordPress Theme', 'Perch', 'edit_theme_options', 'perch', 'perch_admin_theme_page' );
	}
}