/* global jQuery, flo_swish_settings */
jQuery( document ).ready(
	function ( $ ) {
		if ( $( '#swish-connection-test-result' ).length ) {
			/**
			 * Swish connection settings switching.
			 */
			const $swish_environment_select = $( '#woocommerce_swish_pay_gateway_swish_environment' );

			const $swish_test_settings = {
				prod: {},
				sandbox: {},
				mss: {}
			};

			$swish_test_settings.prod.heading1                      = $( '#woocommerce_swish_pay_gateway_swish_prod_settings' );
			$swish_test_settings.prod.desc1                         = $swish_test_settings.prod.heading1.next( 'p' );
			$swish_test_settings.prod.server_certs_input            = $( '#woocommerce_swish_pay_gateway_swish_user_server_certs' );
			$swish_test_settings.prod.server_certs_container        = $swish_test_settings.prod.server_certs_input.closest( 'table' );
			$swish_test_settings.prod.heading2                      = $( '#woocommerce_swish_pay_gateway_swish_prod_settings_generate_csr' );
			$swish_test_settings.prod.desc2                         = $swish_test_settings.prod.heading2.next( 'p' );
			$swish_test_settings.prod.table                         = $swish_test_settings.prod.desc2.length ? $swish_test_settings.prod.desc2.next( 'table' ) : $swish_test_settings.prod.heading2.next( 'table' );
			$swish_test_settings.prod.public_cert_upload            = $( '#woocommerce_swish_pay_gateway_prod_cert_file' );
			$swish_test_settings.prod.public_cert_upload_container  = $swish_test_settings.prod.public_cert_upload.parents( 'tr' );
			$swish_test_settings.prod.private_cert_upload_container = $( '#woocommerce_swish_pay_gateway_prod_priv_cert_file' ).parents( 'tr' );
			$swish_test_settings.prod.private_key_container         = $( '#woocommerce_swish_pay_gateway_live_private_key' ).parents( 'tr' );

			$swish_test_settings.sandbox.heading                       = $( '#woocommerce_swish_pay_gateway_swish_sandbox_settings' );
			$swish_test_settings.sandbox.desc                          = $swish_test_settings.sandbox.heading.next( 'p' );
			$swish_test_settings.sandbox.table                         = $swish_test_settings.sandbox.desc.length ? $swish_test_settings.sandbox.desc.next( 'table' ) : $swish_test_settings.sandbox.heading.next( 'table' );
			$swish_test_settings.sandbox.public_cert_upload            = $( '#woocommerce_swish_pay_gateway_sandbox_cert_file' );
			$swish_test_settings.sandbox.private_cert_upload_container = $( '#woocommerce_swish_pay_gateway_sandbox_priv_cert_file' ).parents( 'tr' );
			$swish_test_settings.sandbox.private_key_container         = $( '#woocommerce_swish_pay_gateway_sandbox_private_key' ).parents( 'tr' );

			$swish_test_settings.mss.heading = $( '#woocommerce_swish_pay_gateway_swish_mss_settings' );
			$swish_test_settings.mss.desc    = $swish_test_settings.mss.heading.next( 'p' );
			$swish_test_settings.mss.table   = $swish_test_settings.mss.desc.length ? $swish_test_settings.mss.desc.next( 'table' ) : $swish_test_settings.mss.heading.next( 'table' );

			function toggle_swish_environment_settings() {
				const swish_environment = $swish_environment_select.val();
				let $to_show            = [];
				let $to_hide            = [];

				switch ( swish_environment ) {
					case 'prod':
						$to_show.push( $swish_test_settings.prod );
						$to_hide.push( $swish_test_settings.sandbox, $swish_test_settings.mss );
						break;
					case 'sandbox':
						$to_show.push( $swish_test_settings.sandbox );
						$to_hide.push( $swish_test_settings.prod, $swish_test_settings.mss );
						break;
					case 'mss':
						$to_show.push( $swish_test_settings.mss );
						$to_hide.push( $swish_test_settings.prod, $swish_test_settings.sandbox );
						break;
				}

				hide_and_show( $to_hide, $to_show );

				toggle_server_certificates( false );
			}

			function toggle_server_certificates( show = true ) {
				const use_server_certs = $swish_test_settings.prod.server_certs_input.is( ':checked' );
				let $to_show           = [];
				let $to_hide           = [];

				if ( use_server_certs ) {
					$to_hide.push(
						$swish_test_settings.prod.public_cert_upload_container,
						$swish_test_settings.prod.private_cert_upload_container,
						$swish_test_settings.prod.private_key_container,
						$swish_test_settings.prod.heading2,
						$swish_test_settings.prod.desc2
					);
				} else if ( show ) {
					$to_show.push(
						$swish_test_settings.prod.public_cert_upload_container,
						$swish_test_settings.prod.private_cert_upload_container,
						$swish_test_settings.prod.private_key_container,
						$swish_test_settings.prod.heading2,
						$swish_test_settings.prod.desc2
					);
				}

				hide_and_show( $to_hide, $to_show );
			}

			$swish_environment_select.on( 'change', toggle_swish_environment_settings );
			$swish_test_settings.prod.server_certs_input.on( 'change', toggle_server_certificates );

			toggle_swish_environment_settings();

			function hide_and_show( $to_hide, $to_show ) {
				$.each(
					$to_show,
					function ( index, obj ) {
						if ( obj instanceof jQuery ) {
							slide_elem( obj, 'down' );
						} else {
							$.each(
								obj,
								function ( index, $elem ) {
									slide_elem( $elem, 'down' );
								}
							);
						}
					}
				);

				$.each(
					$to_hide,
					function ( index, obj ) {
						if ( obj instanceof jQuery ) {
							slide_elem( obj, 'up' );
						} else {
							$.each(
								obj,
								function (index, $elem) {
									slide_elem( $elem, 'up' );
								}
							);
						}
					}
				);
			}

			function slide_elem( elem, direction ) {
				if ( elem instanceof jQuery ) {
					if ( 'up' === direction ) {
						elem.slideUp( 300 );
					} else {
						elem.slideDown( 300 );
					}
				}
			}

			/**
			 * Swish connection test.
			 */
			if ( $( '#woocommerce_swish_pay_gateway_swish_connection_test_enabled' ).is( ':checked' ) ) {
				$.post(
					flo_swish_settings.ajax_url,
					{
						action: 'swish_pay_test_connection',
						nonce: flo_swish_settings.nonce,
					},
					function ( response ) {
						let message = '';
						if ( 'undefined' !== typeof response.data.message ) {
							message = response.data.message
						} else if ( 'undefined' !== typeof response.data ) {
							message = response.data
						} else {
							message = flo_swish_settings.connection_test_error_message
						}
						$( '#swish-connection-test-result' ).html( message );

						if ( 'undefined' !== typeof response.data.payment_id ) {
							setTimeout(
								function () {
									$.post(
										flo_swish_settings.ajax_url,
										{
											action: 'swish_pay_abort_test_payment',
											nonce: flo_swish_settings.abort_nonce,
											payment_id: response.data.payment_id,
										}
									)
								},
								10000
							);
						}
					}
				);
			}

			/**
			 * Generate new certificate.
			 */
			$( '#nm-generate-new-cert-open' ).on(
				'click',
				function ( e ) {
					e.preventDefault();

					$( "#nm-generate-cert-modal-container" )
					.css( "display", "flex" )
					.hide()
					.fadeIn(
						300,
						function () {
							$( "#nm-generate-new-cert-city" ).focus();
						}
					);
				}
			);

			$( '#nm-generate-new-cert-cancel' ).on(
				'click',
				function ( e ) {
					e.preventDefault();
					$( "#nm-generate-cert-modal-container" ).fadeOut();
				}
			);

			$( '#nm-generate-new-cert' ).on(
				'click',
				function ( e ) {
					e.preventDefault();

					let data = {
						action: 'swish_pay_generate_new_certificate',
						nonce: flo_swish_settings.generate_cert_nonce,
					}

					data.city         = $( '#nm-generate-new-cert-city' ).val();
					data.common_name  = $( '#nm-generate-new-cert-common-name' ).val();
					data.email        = $( '#nm-generate-new-cert-email' ).val();
					data.organization = $( '#nm-generate-new-cert-organization' ).val();

					$.post(
						flo_swish_settings.ajax_url,
						data,
						function ( response ) {
							if ( 'undefined' !== typeof response.success && response.success  ) {
								// Update the certificate fields.
								$swish_test_settings.prod.private_key_container.find( 'input' ).val( response.data.private_key );
								$swish_test_settings.prod.public_cert_upload.val( '' );
								$swish_test_settings.prod.public_cert_upload.parents( 'fieldset' ).find( '.description a' ).remove();
								$swish_test_settings.prod.private_cert_upload_container.find( 'input' ).val( '' );
								// Trigger CSR download.
								window.open( response.data.download, '_blank' );
								// Close modal.
								$( "#nm-generate-cert-modal-container" ).fadeOut();
							} else {
								alert( flo_swish_settings.cert_generation_error_message + '\n\n' + response.data.join( '\n' ) );
							}
						}
					);
				}
			);
		}
	}
);
