( function( $ ) {
	'use strict';

	$( document ).on( 'click', '.fancify-core-install-notice-container .fancify-core-install-button a[href="#"],.fancify-welcome-core-install-container .fancify-core-install-button a[href="#"]', function( event ) {
		event.preventDefault();

		// Install plugins
		let $this = $( this );

		let source = $this.attr( 'data-source' );
		source = source.split( '&' );
		let nonce = source[3].substr( source[3].lastIndexOf( '=' ) + 1, source[3].length );
		let type = source[2].substr( source[2].lastIndexOf( '=' ) + 1, source[2].length );
		type = type.substring( 0, type.indexOf( '-' ) );
		let data = '';

		$( '.fancify-ajax-loader-wrap' ).css( 'display', 'block' );
		$( '.fancify-core-install-button' ).addClass( 'fancify-button-deactive' );
		$( $this ).removeAttr( 'href' );

		// Install plugins
		if ( 'install' === type ) {
			data = {
				'action': 'install_fancify_lite_core_plugin',
				'plugin': 'fancify-core',
				'tgmpa-install': 'install-plugin',
				'tgmpa-nonce': nonce,
				'type': type,
				'source': source,
				'page': 'install-required-plugins'
			};
		}

		// Update plugins
		if ( 'update' === type ) {
			data = {
				'action': 'install_fancify_lite_core_plugin',
				'plugin': 'fancify-core',
				'tgmpa-update': 'update-plugin',
				'tgmpa-nonce': nonce,
				'type': type,
				'source': source,
				'page': 'install-required-plugins'
			};
		}

		// Activate plugins
		if ( 'activate' === type ) {
			data = {
				'action': 'install_fancify_lite_core_plugin',
				'plugin': 'fancify-core',
				'tgmpa-activate': 'activate-plugin',
				'type': type,
				'source': source,
				'tgmpa-nonce': nonce,
				'page': 'install-required-plugins'
			};
		}

		$.ajax({
			type: 'GET',
			url: fancifyObject.adminajax,
			data: data
		}).promise().done( function( data ) {

			// data
			if ( true == data.error ) {
				alert( fancifyObject.plugin_installation_error );
				return;
			}

			// Activate plugin
			data = data.split( '{' );
			let end = data.length;
			let str = '{' + data[end - 1];
			let json = JSON.parse( str );

			// Activate plugin
			if ( false === json.error && 'Activated' === json.action ) {
				$( '.fancify-core-install-button' ).addClass( 'fancify-button-deactive' );
				$this.text( json.action + fancifyObject.redirect );
				window.location.replace( json.url );
				return;
			}

			// Install plugin
			if ( false === json.error || 'install' === json.action || 'update' === json.action ) {
				$this.attr( 'data-source', json.source );
				$this.text( fancifyObject.activate );
				$( '.fancify-ajax-loader-wrap' ).css( 'display', 'none' );
				$( '.fancify-core-install-button' ).removeClass( 'fancify-button-deactive' );
				$( $this ).attr( 'href', '#' );
			}

		}).fail( function( fail ) {
			alert( fancifyObject.plugin_installation_error );
			$( '.fancify-ajax-loader-wrap' ).css( 'display', 'none' );
			$( '.fancify-core-install-button' ).removeClass( 'fancify-button-deactive' );
			$( $this ).attr( 'href', '#' );
		});

	});

	/**
	 * Core plugin dismiss notification.
	 */
	$( document ).on( 'click', '.fancify-core-install-notice-container .notice-dismiss', function( event ) {
		$.ajax({
			type: 'GET',
			url: fancifyObject.adminajax,
			data: {
				action: 'fancify_lite_dismiss_notice',
				security: fancifyObject.nonce
			}
		});
	});


}( jQuery ) );
