<?php
/**
 * Customizer settings for the theme.
 */

if ( ! class_exists( 'Eisai_Customizer' ) ) {
	/**
	 * Customizer settings.
	 */
	class Eisai_Customizer {

		public function __construct() {
			add_action( 'customize_register', 			array( $this, 'register' ) );
			add_action( 'customize_preview_init', 		array( $this, 'customizer_preview' ) );
			add_action( 'wp_enqueue_scripts',			array( $this, 'customizer_css' ), 130 );
			add_action( 'enqueue_block_editor_assets',	array( $this, 'block_editor_customizer_css' ) );
		}

        /**
		 * Register customizer options.
		 *
		 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
		 */
		public static function register( WP_Customize_Manager $wp_customize ) {

			/*
			 * Remove Customzier controls/sections that are not relevant to this theme.
			 */
			$wp_customize->remove_control( 'background_color' ); // Theme uses it's own option for background color.
			$wp_customize->remove_section( 'background_image' ); // Theme not uses background image option in it's design for now.

            /**
			 * Site Title.
			 */
			$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';

			/**
			 * Site Logo.
			 */
			$wp_customize->selective_refresh->add_partial(
				'custom_logo',
				array(
					'selector' => '#site-header .uk-logo',
					'render_callback' => 'eisai_site_branding',
				)
			);

            /**
			 * Site background color.
			 */
			$wp_customize->add_setting(
				'site_background_color',
				array(
					'default'           => '#ffffff',
					'capability' 		=> 'edit_theme_options',
					'sanitize_callback' => 'sanitize_hex_color',
					'transport'         => 'postMessage',
				)
			);

            $wp_customize->add_control(
				new WP_Customize_Color_Control(
					$wp_customize,
					'site_background_color',
					array(
						'label'   		=> __( 'Background Color', 'eisai' ),
                        'description' 	=> __( 'Make sure that the contrast is high enough so that the text is readable.', 'eisai' ),
						'section' 		=> 'colors',
					)
				)
			);

			/**
			 * Links color.
			 */
			$wp_customize->add_setting(
				'site_links_color',
				array(
					'default'           => '#3d3d3c',
					'capability'        => 'edit_theme_options',
					'sanitize_callback' => 'sanitize_hex_color',
				)
			);

            $wp_customize->add_control(
				new WP_Customize_Color_Control(
					$wp_customize,
					'site_links_color',
					array(
						'label'   		=> __( 'Links Color', 'eisai' ),
                        'description' 	=> __( 'Make sure that the contrast is high enough and links are readable.', 'eisai' ),
						'section' 		=> 'colors',
					)
				)
			);

            /**
			 * Theme Options
			 */

			// Require custom control classes.
			require_once __DIR__ . '/class-eisai-section-intro-control.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound - Not a template, but a custom control class.

			$wp_customize->add_section(
				'options',
				array(
					'title'      => __( 'Theme Options', 'eisai' ),
					'priority'   => 40,
					'capability' => 'edit_theme_options',
				)
			);

            /**
			 * Add custom control to display section title and description.
			 */
            $wp_customize->add_setting(
				'eisai_blog_settings_intro',
				array(
					'capability' 		=> 'edit_theme_options',
					'sanitize_callback' => '__return_true',
				)
			);

            $wp_customize->add_control(
            	new Eisai_Section_Intro_Control(
            		$wp_customize,
                    'eisai_blog_settings_intro',
            		array(
                        'label' => __( 'Blog Page', 'eisai' ),
            			'section' => 'options',
            		)
            	)
            );

			/**
			 * Show categories in the header section of the blog/category page.
			 */
			$wp_customize->add_setting(
				'show_categories_on_blog_page',
				array(
					'capability'        => 'edit_theme_options',
					'default'           => false,
					'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
				)
			);

			$wp_customize->add_control(
				'show_categories_on_blog_page',
				array(
					'type'     => 'checkbox',
					'section'  => 'options',
					'priority' => 10,
					'label'    => __( 'Show categories in the header section.', 'eisai' ),
				)
			);

            /**
			 * Show post meta on a blog page.
			 */
			$wp_customize->add_setting(
				'show_post_meta',
				array(
					'capability' 		=> 'edit_theme_options',
					'default'           => true,
					'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
				)
			);

			$wp_customize->add_control(
				'show_post_meta',
				array(
					'type'     => 'checkbox',
					'section'  => 'options',
					'priority' => 10,
					'label'    => __( 'Show post meta on a blog page.', 'eisai' ),
				)
			);

            /**
			 * Show post excerpt on a blog page.
			 */
			$wp_customize->add_setting(
				'show_excerpt',
				array(
					'capability'        => 'edit_theme_options',
					'default'           => true,
					'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
				)
			);

			$wp_customize->add_control(
				'show_excerpt',
				array(
					'type'     => 'checkbox',
					'section'  => 'options',
					'priority' => 10,
					'label'    => __( 'Show post excerpt on a blog page.', 'eisai' ),
				)
			);

            /**
			 * Add custom control to display 'Single post' section title and description.
			 */
            $wp_customize->add_setting(
				'eisai_post_settings_intro',
				array(
					'capability' 		=> 'edit_theme_options',
					'sanitize_callback' => '__return_true',
				)
			 );

            $wp_customize->add_control(
            	new Eisai_Section_Intro_Control(
            		$wp_customize,
                    'eisai_post_settings_intro',
            		array(
                        'label' 	=> __( 'Single Post', 'eisai' ),
            			'section' 	=> 'options',
            		)
            	)
            );

            /**
			 * Show post navigation.
			 */
			$wp_customize->add_setting(
				'show_post_navigation',
				array(
					'capability'        => 'edit_theme_options',
					'default'           => true,
					'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
				)
			);

			$wp_customize->add_control(
				'show_post_navigation',
				array(
					'type'     => 'checkbox',
					'section'  => 'options',
					'priority' => 10,
					'label'    => __( 'Show post navigation.', 'eisai' ),
				)
			);

        }

		/**
		 * Enqueue scripts for the Customizer preview.
		 */
		public function customizer_preview() {
			global $eisai_version;

			$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

			wp_enqueue_script( 'eisai-customize-preview', get_theme_file_uri( '/assets/js/customize-preview' . $suffix . '.js' ), array( 'customize-preview', 'jquery' ), $eisai_version, true );
		}

		/**
		 * Add Customizer options CSS to to the <head> section of the website.
		 */
		public function customizer_css() {
			wp_add_inline_style( 'eisai-style', $this->get_customizer_css( 'front-end' ) );
		}

		/**
		 * Add Customizer options CSS to to the <head> section of the website to style Gutenberg blocks.
		 */
		public function block_editor_customizer_css() {
			wp_add_inline_style( 'eisai-block-editor-style', $this->get_customizer_css( 'block-editor' ) );
		}

        /**
		 * Sanitize boolean for checkbox.
		 *
		 * @param bool $checked Whether or not a box is checked.
		 *
		 * @return bool
		 */
		public static function sanitize_checkbox( $checked ) {
			return ( ( isset( $checked ) && true === $checked ) ? true : false );
		}



		/**
		 * Get CSS Built from Customizer Options. This function I took from TwentyTwenty WordPress theme.
		 * Build CSS reflecting colors, fonts and other options set in the Customizer, and return them for output.
		 *
		 * @param string $type Whether to return CSS for the "front-end", "block-editor" or "classic-editor".
		 */
		public function get_customizer_css( $type = 'front-end' ) {

			// Site background.
			$body_bg                  	= sanitize_hex_color( get_theme_mod( 'site_background_color' ) );
			$body_bg_default          	= '#ffffff';

			// Links Color.
			$links_color               	= sanitize_hex_color( get_theme_mod( 'site_links_color' ) );
			$links_default_color       	= '#3d3d3c';

			ob_start();

			// Front-End Styles.
			if ( 'front-end' === $type ) {

				// Site, primary navigation and .comments-wrapper background.
	            if ( $body_bg && $body_bg !== $body_bg_default ) {
					eisai_generate_css( 'html, body', 'background-color', $body_bg );
					eisai_generate_css( '#primary-navigation .uk-offcanvas-bar', 'background-color', $body_bg );
					eisai_generate_css( '.uk-modal .uk-modal-dialog', 'background-color', $body_bg );
					eisai_generate_css( '.comments-wrapper', 'background-color', $body_bg );
					eisai_generate_css( '.post-tags a', 'background-color', $body_bg_default );
					eisai_generate_css( '.post-tags a:hover, .post-tags a:focus', 'background-color', $body_bg_default );
					eisai_generate_css( '.search-query', 'background-color', $body_bg_default );
				}

				// Links color.
				if ( $links_color && $links_color !== $links_default_color ) {
					// Links color.
					eisai_generate_css( 'a, .uk-link', 'color', $links_color );
					eisai_generate_css( 'a:hover, a:focus, .uk-link:hover, .uk-link:focus', 'color', eisai_adjust_color_brightness( $links_color, -50 ) );
					eisai_generate_css( '.wp-block-button__link:not(.has-text-color)', 'color', $links_color );
					eisai_generate_css( '.wp-block-button__link:not(.has-text-color):hover, .wp-block-button__link:not(.has-text-color):focus', 'color', eisai_adjust_color_brightness( $links_color, -50 ) );
				}

			// Block Editor Styles.
			} elseif ( 'block-editor' === $type ) {

				// Background color.
				if ( $body_bg && $body_bg !== $body_bg_default ) {
					eisai_generate_css( '.editor-styles-wrapper', 'background-color', $body_bg );
	            }

				// Links color.
				if ( $links_color && $links_color !== $links_default_color ) {
					// Links color.
					eisai_generate_css( '.editor-styles-wrapper a', 'color', $links_color );
					eisai_generate_css( '.editor-styles-wrapper a:hover, .editor-styles-wrapper a:focus', 'color', eisai_adjust_color_brightness( $links_color, -50 ) );
					eisai_generate_css( '.wp-block-freeform.block-library-rich-text__tinymce a', 'color', $links_color );
					eisai_generate_css( '.wp-block-freeform.block-library-rich-text__tinymce a:hover, .wp-block-freeform.block-library-rich-text__tinymce a:focus', 'color', eisai_adjust_color_brightness( $links_color, -50 ) );
				}

			}

			// Return the results.
			return ob_get_clean();

		}

    }
}

return new Eisai_Customizer();
