<?php
/**
 * Customizer info main class.
 *
 * @package Shopwell
 * @since Shopwell 1.0.0
 */

/**
 * Pro customizer section.
 *
 * @since  1.0.0
 * @access public
 */
class Shopwell_Customizer_Control_Generic_Notice extends WP_Customize_Section {

	/**
	 * The type of customize section being rendered.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $type = 'shopwell-generic-notice';

	/**
	 * Label text to output.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $section_text = '';

	/**
	 * Plugin slug for which to create install button.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $slug = '';

	/**
	 * Hide notice.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $hide_notice = false;

	/**
	 * Screen reader text on dismiss button.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $button_screenreader = '';

	/**
	 * Control options.
	 *
	 * Ex: redirect link after install
	 *
	 * @var array
	 */
	public $options = array();


	/**
	 * Id of customizer hiding control.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var integer
	 */
	public $id;

	/**
	 * Shopwell_Hiding_Section constructor.
	 *
	 * @param WP_Customize_Manager $manager Customizer Manager.
	 * @param string               $id Control id.
	 * @param array                $args Arguments.
	 */
	public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
		parent::__construct( $manager, $id, $args );
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue' ), 10, 1 );
	}


	/**
	 * Enqueue function.
	 */
	public function enqueue() {
		Shopwell_Customizer_Plugin_Install_Helper::instance()->enqueue_scripts();
	}

	/**
	 * Add custom parameters to pass to the JS via JSON.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return array
	 */
	public function json() {
		$json                          = parent::json();
		$json['section_text']          = $this->section_text;
		$json['hide_notice']           = $this->hide_notice;
		$json['slug']                  = $this->slug;
		$json['plugin_install_button'] = $this->create_plugin_install_button( $this->slug, $this->options );

		return $json;
	}

	/**
	 * Outputs the Underscore.js template.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */
	protected function render_template() {
		?>
		<# if ( !data.hide_notice ) { #>
		<li id="accordion-section-{{ data.id }}"
				class="shopwell-notice accordion-section control-section control-section-{{ data.type }} cannot-expand"
				data-notice-id="{{ data.id }}" style="position:relative;">
			<button type="button" class="notice-dismiss" style="z-index: 1;"></button>
			<div class="accordion-section-title" style="padding-right: 36px; display: block; background-color: #f0f0f1;">
				<# if ( data.slug ) { #>
				<h3 style="margin-top: 0;margin-bottom: 0;">{{{data.slug.split('-').map(function(word) { return word.charAt(0).toUpperCase() + word.slice(1); }).join(' '	)}}}</h3>
				<# } #>

				<# if ( data.section_text ) { #>
				<p>{{{data.section_text}}}</p>
				<# } #>

				<# if ( data.plugin_install_button ) { #>
				{{{data.plugin_install_button}}}
				<# } #>
			</div>

		</li>
		<# } #>
		<?php
	}

	/**
	 * Check plugin state.
	 *
	 * @param string $slug plugin slug.
	 *
	 * @return bool
	 */
	public function create_plugin_install_button( $slug, $settings = array() ) {
		return Shopwell_Customizer_Plugin_Install_Helper::instance()->get_button_html( $slug, $settings );
	}
}
