<?php
/**
 * JM_Web_Development\Giornalismo\Customizer\Component class
 *
 * @package giornalismo
 */

namespace JM_Web_Development\Giornalismo\Customizer;

use JM_Web_Development\Giornalismo\Component_Interface;
use function JM_Web_Development\Giornalismo\giornalismo;
use WP_Customize_Manager;
use function add_action;
use function bloginfo;
use function wp_enqueue_script;
use function get_theme_file_uri;
use function get_theme_file_path;

/**
 * Class for managing Customizer integration.
 */
class Component implements Component_Interface {

	/**
	 * Gets the unique identifier for the theme component.
	 *
	 * @return string Component slug.
	 */
	public function get_slug() : string {
		return 'customizer';
	}

	/**
	 * Adds the action and filter hooks to integrate with WordPress.
	 */
	public function initialize() {
		add_action( 'customize_register', array( $this, 'action_customize_register' ) );
		add_action( 'customize_preview_init', array( $this, 'action_enqueue_customize_preview_js' ) );
	}

	/**
	 * Adds postMessage support for site title and description, plus a custom Theme Options section.
	 *
	 * @param WP_Customize_Manager $wp_customize Customizer manager instance.
	 */
	public function action_customize_register( WP_Customize_Manager $wp_customize ) {

		$wp_customize->add_panel(
			'giornalismo_main_options',
			array(
				'priority'       => 10,
				'capability'     => 'edit_theme_options',
				'theme_supports' => '',
				'title'          => esc_html__( 'Giornalismo Options', 'giornalismo' ),
			)
		);

		$wp_customize->add_section(
			'giornalismo_basic_options',
			array(
				'title'    => esc_html__( 'Basic Options', 'giornalismo' ),
				'priority' => 130, // Before Additional CSS.
				'panel'    => 'giornalismo_main_options',
			)
		);

		$wp_customize->add_section(
			'giornalismo_homepage_options',
			array(
				'title'    => esc_html__( 'Homepage Options', 'giornalismo' ),
				'priority' => 130, // Before Additional CSS.
				'panel'    => 'giornalismo_main_options',
			)
		);

		$wp_customize->add_section(
			'giornalismo_social_media_options',
			array(
				'title'    => esc_html__( 'Social Media Options', 'giornalismo' ),
				'priority' => 130, // Before Additional CSS.
				'panel'    => 'giornalismo_main_options',
			)
		);

		/**
		 * Basic options.
		 */

		$wp_customize->add_setting(
			'giornalismo-layout',
			array(
				'default'           => 'right-sidebar',
				'sanitize_callback' => array( $this, 'sanitize_select' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-layout',
			array(
				'label'     => esc_html__( 'Layout', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'select',
				'choices'   => array(
					'right-sidebar' => esc_html__( 'Right Sidebar', 'giornalismo' ),
					'left-sidebar'  => esc_html__( 'Left Sidebar', 'giornalismo' ),
					'two-sidebars'  => esc_html__( 'Two Sidebars', 'giornalismo' ),
				),
			)
		);

		$wp_customize->add_setting(
			'giornalismo-color-theme',
			array(
				'default'           => 'right-sidebar',
				'sanitize_callback' => array( $this, 'sanitize_select' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-color-theme',
			array(
				'label'     => esc_html__( 'Color Theme', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'select',
				'choices'   => array(
					'default'   => esc_html__( 'Default', 'giornalismo' ),
					'blue'      => esc_html__( 'Blue', 'giornalismo' ),
					'green'     => esc_html__( 'Green', 'giornalismo' ),
					'purple'    => esc_html__( 'Purple', 'giornalismo' ),
					'red'       => esc_html__( 'Red', 'giornalismo' ),
				),
			)
		);

		$wp_customize->add_setting(
			'giornalismo-top-menu',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-top-menu',
			array(
				'label'     => esc_html__( 'Top Menu', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-todays-date',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-todays-date',
			array(
				'label'     => esc_html__( 'Show Today\'s Date', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-author-bio',
			array(
				'default'           => '1',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-author-bio',
			array(
				'label'     => esc_html__( 'Display the Author\'s Bio After Posts', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-author-position',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-author-position',
			array(
				'label'     => esc_html__( 'Display the Author\'s Position', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-author-byline',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-author-byline',
			array(
				'label'     => esc_html__( 'Display the Author\'s Byline', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		//* Post Comments
		$wp_customize->add_setting(
			'giornalismo-post-comments',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-post-comments',
			array(
				'label'     => esc_html__( 'Display the Number of Comments on a Post', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-breadcrumbs',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-breadcrumbs',
			array(
				'label'     => esc_html__( 'Display Breadcrumbs', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-related-stories',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-related-stories',
			array(
				'label'     => esc_html__( 'Show Related Stories', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-latest-stories',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-latest-stories',
			array(
				'label'     => esc_html__( 'Show latest posts from a category on the single post template', 'giornalismo' ),
				'section'   => 'giornalismo_basic_options',
				'type'      => 'checkbox',
			)
		);

		/**
		 * Homepage options.
		 */
		$cats             = get_categories();
		$cat_args['none'] = esc_html__( 'None', 'giornalismo' );
		foreach ( $cats as $cat ) {
			$cat_args[ $cat->term_id ] = $cat->name;
		}

		$wp_customize->add_setting(
			'giornalismo-top-story-category',
			array(
				'default'           => 'None',
				'sanitize_callback' => array( $this, 'sanitize_category' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-top-story-category',
			array(
				'label'     => esc_html__( 'Top Story Category', 'giornalismo' ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'select',
				'choices'   => $cat_args,
			)
		);

		$wp_customize->add_setting(
			'giornalismo-column-one-category',
			array(
				'default'           => 'None',
				'sanitize_callback' => array( $this, 'sanitize_category' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-column-one-category',
			array(
				'label'     => esc_html__( 'Column One Category', 'giornalismo' ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'select',
				'choices'   => $cat_args,
			)
		);

		$wp_customize->add_setting(
			'giornalismo-column-one-count',
			array(
				'default'           => '5',
				'sanitize_callback' => array( $this, 'sanitize_num' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-column-one-count',
			array(
				'label'     => esc_html__( 'Number of Stories in the First Column', 'giornalismo' ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-column-two-category',
			array(
				'default'           => 'None',
				'sanitize_callback' => array( $this, 'sanitize_category' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-column-two-category',
			array(
				'label'     => esc_html__( 'Column Two Category', 'giornalismo' ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'select',
				'choices'   => $cat_args,
			)
		);

		$wp_customize->add_setting(
			'giornalismo-column-two-count',
			array(
				'default'           => '5',
				'sanitize_callback' => array( $this, 'sanitize_num' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-column-two-count',
			array(
				'label'     => esc_html__( 'Number of Stories in the Second Column', 'giornalismo' ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-column-three-category',
			array(
				'default'           => 'None',
				'sanitize_callback' => array( $this, 'sanitize_category' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-column-three-category',
			array(
				'label'     => esc_html__( 'Column Three Category', 'giornalismo' ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'select',
				'choices'   => $cat_args,
			)
		);

		$wp_customize->add_setting(
			'giornalismo-column-three-count',
			array(
				'default'           => '6',
				'sanitize_callback' => array( $this, 'sanitize_num' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-column-three-count',
			array(
				'label'     => esc_html__( 'Number of Stories in the Third Column', 'giornalismo'  ),
				'section'   => 'giornalismo_homepage_options',
				'type'      => 'text',
			)
		);

		/**
		 * Social media options.
		 */
		$wp_customize->add_setting(
			'giornalismo-facebook',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-facebook',
			array(
				'label'     => esc_html__( 'Facebook Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-twitter',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-twitter',
			array(
				'label'     => esc_html__( 'Twitter Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-youtube',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-youtube',
			array(
				'label'     => esc_html__( 'YouTube Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-linkedin',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-linkedin',
			array(
				'label'     => esc_html__( 'LinkedIn Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-tumblr',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-tumblr',
			array(
				'label'     => esc_html__( 'Tumblr Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-instagram',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-instagram',
			array(
				'label'     => esc_html__( 'Instagram Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-pinterest',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-pinterest',
			array(
				'label'     => esc_html__( 'Pinterest Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-rss-feed',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_link' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-rss-feed',
			array(
				'label'     => esc_html__( 'RSS Feed Link', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'text',
			)
		);

		$wp_customize->add_setting(
			'giornalismo-twitter-handle',
			array(
				'default'           => '',
				'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
			)
		);

		$wp_customize->add_control(
			'giornalismo-twitter-handle',
			array(
				'label'     => esc_html__( 'Show Author\'s Twitter Handle', 'giornalismo' ),
				'section'   => 'giornalismo_social_media_options',
				'type'      => 'checkbox',
			)
		);
	}

	/**
	 * Enqueues JavaScript to make Customizer preview reload changes asynchronously.
	 */
	public function action_enqueue_customize_preview_js() {
		wp_enqueue_script(
			'giornalismo-customizer',
			get_theme_file_uri( '/assets/js/customizer.min.js' ),
			array( 'customize-preview' ),
			giornalismo()->get_asset_version( get_theme_file_path( '/assets/js/customizer.min.js' ) ),
			true
		);
	}

	public function sanitize_link( $input ) {
		return esc_url_raw( $input );
	}

	public function sanitize_select( $input, $setting ) {
		$input = sanitize_key( $input );
		$choices = $setting->manager->get_control( $setting->id )->choices;
		return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
	}

	public function sanitize_checkbox( $input ) {
		return ( ( isset( $input ) && true == $input ) ? 1 : 0 );
	}

	public function sanitize_category( $input, $setting ) {
		$input = sanitize_key( $input );
		$choices = $setting->manager->get_control( $setting->id )->choices;
		return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
	}

	public function sanitize_num( $input, $setting ) {
		$input = absint( $input );
		return ( $input ? $input : $setting->default );
	}

	public function sanitize_text( $input ) {
		return wp_filter_nohtml_kses( $input );
	}
}
